3

wouldn't be posting here if I hadn't tried everything I could find on the interwebs...would appreciate any guidance from a seasoned sysadmin.

Here is my problem: cron jobs in /etc/cron.daily, /etc/cron.hourly etc. are running fine on my Slicehost Ubuntu server -- their execution is being logged to both /var/log/syslog and /var/log/cron.log

I've created two test cron jobs, one as root and one as an admin user, through crontab that are supposed to run every minute. Here is the output of crontab -l run as root

$ crontab -l
* * * * * /bin/date > /tmp/unicorns
$ crontab -u neil0 -l
* * * * * /home/neil0/crontab/unicorn.sh >> /home/neil0/logs/crontab.log >&1

EDIT 1: I didn't copy this exactly...there was no newline in the output and that was the source of the problem

here are the contents of unicorn.sh

#!/bin/sh
echo 'UNICORNS'

but neither /home/neil0/logs/crontab.log nor /tmp/unicorns is being updated, and there is no record of these jobs being run in /var/log/syslog or /var/log/cron.log!

before you ask, it does appear that cron is running --

$ ps aux | grep cron
root      1588  0.0  0.1  18616   980 ?        Ss   12:31   0:00 /usr/sbin/cron
root      1798  0.0  0.1   3944   604 pts/0    R+   13:01   0:00 grep cron

in case you were wondering 'unicorn' is my replacement frustration word for 'F***' ever since a certain debugging snafu with a live website sooooo as you can see i'm pretty much at my wits end here. any help would be greatly appreciated.

3
  • Have you creating these as a crontab fragment in /etc/cron.d/?
    – Zoredache
    Oct 7, 2009 at 17:19
  • not sure what that means exactly...I created them using crontab on a file, like the file was called unicorn.cron and was just one line with * * * * * /bin/date > /tmp/unicorns and then i did $ crontab unicorn.cron Oct 7, 2009 at 17:22
  • i should mention that that command created files in /var/spool/cron/crontabs/ do i have to do something to get cron to pick those up? Oct 7, 2009 at 17:26

2 Answers 2

7

uh...huh. so as I'm guessing most of you already know, you need to end your crontab files with a newline. that was the whole issue. hope this helps someone else

6
  • Just curious how this happened in the first place. Were you editing the crontab files directly? Or were you editing them with 'crontab -e'? Oct 7, 2009 at 19:23
  • Well I just created the file (for example unicorn.cron) manually in emacs, put in the line you see above, and then added it to cron with crontab unicorn.cron I had done it this way in the past and it worked but I guess I had always fortuitously put a newline in. I just took a look at crontab -e and I guess that is the standard accepted way to create a new cron job for a user? Will def use in the future Oct 7, 2009 at 19:54
  • 2
    Yes 'crontab -e' is the way to go. It will open the existing cron file in your the program you have set to your EDITOR environment variable and then install it when you're done. It also does some sanity checks before installing the new file so I would think that it would have added the necessary newline. So if you want it to use emacs you just need to set the appropriate env variable. Oct 7, 2009 at 20:56
  • Hi @NeilSarkar, thanks a lot for sharing. May I ask how you can add a newline to crontab -e? I've been searching for a solution for the last 2 hours but no success. Naively putting newline character % at the end of the last command or below it does not work for me unfortunately
    – ericn
    Oct 23, 2012 at 8:54
  • I have 2 commands in my crontab as follows btw: */5 * * * * ec2-automate-backup.sh -v "vol-myvolumeid" -k 3 * * * * * unicorn.sh >> /root/logs/crontab.log
    – ericn
    Oct 23, 2012 at 8:59
0

Remember that in crontab, unlike in shell, you need to escape special characters, i.e.

0 0 * * * backup_command > /var/log/backup-$(date +**\\**%a).log

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .