I use crontab -e
without sudo
to edit cron jobs and add the following line there –
20 22 23 10 * date "+%Y-%m-%d %H:%M:%S" >> ~/Documents/log.txt
then I check the log file and it doesn't contain the date and time.
I tried to edit crontab
as root, but it also doesn't update the file. What could be wrong here? Is there any way to see the log of cron jobs execution?
P.S. Update crontab created under root:
0/5 * * * * /bin/date "+%Y-%m-%d %H:%M:%S" >> /tmp/log.txt
0/5 * * * * echo >> /tmp/log2.txt
log2.txt
is created now.
Best Answer
Your
cron
error message are sent to themail
account of the user that is running thecron
job.Type
mail
at the command line to see the messages. For a list ofmail
commands, see man mail.You should be getting mail with an error message something to the effect of:
As for you command and why it's not working, it's because the "%" sign is a "newline" character, meaning that it tells the shell to "go to the next line." So, after it encounters the first "%" it goes to the next line and it never sees the rest of the command. This is confirmed by the "unexpected EOF while looking for the matching ' " '(double quote).
The local man page for crontab (
man crontab
)is quite sparce with details and I am speculating it's becausecron
has been deprecated forlaunchd
1. However, the crontab manpage from developer.apple.com explains itYour fix:
So, what you need to do, is "escape" the "%" character with a backslash (\) so that it tells it to interpret it literally.
Your command should be:
1 From
man crontab
: (Darwin note: Although cron(8) and crontab(5) are officially supported under Darwin, their functionality has been absorbed into launchd(8), which provides a more flexible way of automatically executing commands. See launchctl(1) for more information.)