Crontab syntax error

I am trying to setup a cron job but when I try to edit my crontab and add this line

*/5 * * * * /usr/local/bin/check_desktop >/dev/null 2>&1

I get the following error :

"-":2: premature EOF
Invalid crontab file. Syntax issues were found.

I have googled but nowhere on the internet is their any mention of “-”:2: anywhere.

Can someone please explain what I am doing wrong ?
I have a linefeed at the end of the line that I have added to the crontab file.

               Thank You.

The script that I am trying to run is to check if xfdesktop is running and if not then to start it. Because xfdesktop keeps disappearing with the following error in xsession-errors.

** (xfdesktop:36497): CRITICAL **: 21:49:29.252: build_property_prefix: assertion 'first_xfwmonitor != NULL' failed

If someone could help me fix this error too I would be very grateful.
I am on an X86_64 machine running xfce desktop.

            Thanks.

This confuses me and at least to my eye, can’t be right. Where did you get this suggestion? :thinking:

I got it from man 5 crontab.

it seems to be o.k. - syntax wise

Such example is even in man crontab:

Step values can be used in conjunction with ranges. Following a
range with “/” specifies skips of the number’s value
through the range.

Could the “culprit” be in the file that is to be executed?

/usr/local/bin/check_desktop

perhaps try without redirecting the output?

2 Likes

That was my thought; remove the bit I quoted?

I have removed the redirection from the crontab and the result is the same. It does not run and I get the same syntax error message. The check_desktop script runs fine from the command prompt and works as intended.

In other words, it’s an XY problem. You are asking about an attempted solution — not that your crontab issue doesn’t warrant further investigation — rather than about the real solution, which is to find out why the xfdesktop process dies, and prevent it from happening again.

4 Likes

Could it be similar to this? https://stackoverflow.com/questions/11304533/cant-install-cron-job
Add a blank line to the end of the file?

1 Like

It would surprise me if that would help.
A newline/line break … ok - maybe
I often do it out of habit.

but a whole new, empty line to “make it work”?
… I have never seen that …

I’m just wondering why a systemd timer couldn’t be used instead of cron to run the job every 5 minutes?

Here’s a quick example I put together (I’m assuming it runs as the user, not root):

First, you need to create the service file:

touch ~/.config/systemd/user/checkdesktop.service

The contents of that file will be:

[Unit]
Description=A job to check if xfdesktop is running and if not then to start it

[Service]
Type=simple
ExecStart=/usr/local/bin/check_desktop

And then create the timer file:

touch ~/.config/systemd/user/checkdesktop.timer

With the following contents:

[Unit]
Description=A job to check if xfdesktop is running and if not then to start it

[Timer]
OnStartupSec=1min
OnUnitActiveSec=5min

[Install]
WantedBy=timers.target

Enable & start the timer:

systemctl --user enable --now checkdesktop.timer

The timer will now start the service 1 minute after logging in, and every 5 minutes after that.

You can check the status of the timer with:

systemctl --user status checkdesktop.timer

And the status of the service with:

systemctl --user status checkdesktop

That last command will display any errors the service encountered when running the job.

Important: If you make any edits to the service or timer files after the timer is enabled, you should run the following command afterwards, so that systemd is aware of the changes:

systemctl --user daemon-reload

Edit: If you need to run the service & timer as root, place the 2 files in the /usr/lib/systemd/system/ directory, and run the commands with the --user part removed, for example:

systemctl enable --now checkdesktop.timer

There’s no need to use sudo as systemctl will prompt you for your password via polkit when activating or modifying system services.

2 Likes

To investigate why xfdesktop is dying, run:

journalctl -b | grep -i xfdesktop

That searches the current boot’s journal for anything related to xfdesktop. A few useful variations:

# Show logs from previous boot (if it crashed/restarted)
journalctl -b -1 | grep -i xfdesktop

# Follow live as it happens
journalctl -f | grep -i xfdesktop

# Broader — check for any session/desktop errors
journalctl -b | grep -iE "xfce|xfdesktop|session"

Share these output so we can investigate the real problem.

Probably better to create a new topic :wink:

3 Likes

It seems to me that your added line is ok. Try this:

Save the active crontab to a file:

crontab -l > ctab.tmp

Then edit ctab.tmp and add your line. Save the file and call
“crontab ctab.tmp” to activate it as new crontab (is crontab file still invalid?)

Problems with your script (bash?) are another thing. Sytax of bash scripts can be checked before using them by “bash -n script-file” or by command shellcheck (package shellcheck).

I found the reason of it. It has to do with setting of test parameter for crontab binary.

The parameter -T (testing syntax) may be called with a file name (containing crontab lines) or according to man page of crontab by reading crontab content from standard input (same as for activation), by parameter " - ". But STDIN doesn’t work here. Man page appears to be incorrect or crontab binary is faulty:

crontab [-T] <file | ->

Verifying in my environment:

$ crontab -l > f
# syntax check with file name ok:
$ crontab -T f
No syntax issues were found in the crontab file.
# But not from STDIN
$ cat f |crontab -T -
"-":3: premature EOF
Invalid crontab file. Syntax issues were found.
# Same without " - ":
 cat f |crontab -T
"-":3: premature EOF
Invalid crontab file. Syntax issues were found.
# Same error with input from active crontab output:
$ crontab -l |crontab -T -
"-":3: premature EOF
Invalid crontab file. Syntax issues were found.
# activation ok from STDIN:
$ cat f |crontab -
Backup of ......... previous crontab saved to /home/.........
3 Likes

It’s interesting to note that crontab -T only works with files and not with stdin. I checked my crontab using

[david@darkstar 01:10 am [ ~ ]$ crontab -T '/var/spool/cron/david'
No syntax issues were found in the crontab file.
[david@darkstar 01:10 am [ ~ ]$ 

and as you can see there’s no syntax errors.
And thank you @scotty65 for the systemd timers method of running my script. It works perfectly.

2 Likes

You can edit the crontab file indirectly by “crontab -e” with inbuilt syntax check when saving:

Invalid crontab file, can't install.
Do you want to retry the same edit? (Y/N)

But I prefer to use a “master file” for editing created by “crontab -l > crontab.master”, which serves as documentation and backup as well. This file can be transfered and activated by “crontab crontab.master” to /var/spool/cron/.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.