Reputation: 19
Crontab task doesn't work when I edit crontab by vim instead of "crontab -e"
on docker container ubuntu18.04
step 1:
Use docker run a container, the image is ubuntu18.04
os.
step 2:
vim /var/spool/cron/crontabs/root
and Write content to root file as following:
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
*/1 * * * * . /etc/profile; /bin/sh /test_cron/xx.sh 2>&1
step 3:
cd /
mkdir test_cron
cd test_cron
step 4: Edit xx.sh in /test_cron/xx.sh, the content as following:
echo "cron job has start" >> /test_cron/run.log
step 5:
service cron restart
step 6:
There is no run.log
in /test_cron/
, that’s to say, the crontab task doesn't work. But if I using "crontab -e"
to open the /var/spool/cron/crontabs/root
file and don't make anything modify. Just open and close the /var/spool/cron/crontabs/root
, I can see the run.log
file in /test_cron/
, amazing, the crontab task worked. Could you tell me the reason?
Upvotes: 0
Views: 1038
Reputation: 1157
Few points,
The corntab -e
makes sure certain formatting and error tracking to an extent.
crontab file should contain one empty line at the end.
There are certain permission to set to the crontab chmod 600
After completing these I could see the manual entry was working. However it is not recommended to directly edit the crontab file and the best practice is to use crontab -e
EDIT: Actually emptyline
should be corrected as newline character or % as per the man page
The "sixth" field (the rest of the line) specifies the command to be run. The entire command portion of the line, up to a newline or % character, will be executed by /bin/sh or by the shell specified in the SHELL variable of the cronfile. Percent-signs (%) in the command, unless escaped with backslash (), will be changed into newline characters, and all data after the first % will be sent to the command as standard input.
Upvotes: 1