Monday, 23 June 2014

Crontab error - syntax error, bad username

I had what I thought was a really simple crontab file in /etc/cron.d which simply called a bash script in my home directory. I realised it wasn't running and looked into /var/log/syslog to find the following error:

Jun 23 09:39:27 cron[19563]: Error: bad command; while reading /etc/cron.d/backup-files
Jun 23 09:39:27 cron[19563]: (*system*backup-files) ERROR (Syntax error, this crontab file will be ignored)

The file was definitely correctly written, I even ran the command line through an online checker and it passed. Since I was running a script directly, I tried to add /bin/bash as an additional argument to the script, and got a similar but different error:

Jun 23 09:45:24 cron[19591]: Error: bad username; while reading /etc/cron.d/backup-files
Jun 23 09:45:24 cron[19591]: (*system*backup-files) ERROR (Syntax error, this crontab file will be ignored)

Really annoying, I ended up trawling the net for answer, of which there were many, and...

The Solution

The system crontab has an additional column (the crontab that is run by root from /etc/crontab and /etc/crontab/cron.*) which specified the user to run the script as. Since I had copied the crontab comments from my user crontab, they didn't mention this!

So I ended up changing:

0 23 * * 1-5 /bin/bash /home/ubuntu/backup-files

to

0 23 * * 1-5 root /bin/bash /home/ubuntu/backup-files

and it was all happy again!


Post a Comment