开发者

Crontab no error but doesn't execute script

开发者 https://www.devze.com 2022-12-16 17:25 出处:网络
I\'m trying to execute a shell script from cron on Freebsd. To test whether crontab is working at all, I wrote the line

I'm trying to execute a shell script from cron on Freebsd.

To test whether crontab is working at all, I wrote the line

  * * * * * echo "Hello" > /home/myuser/logile  

and it work fine.

But when trying to execute any script it doesn't do anything, not even an error. (In the script I tried to run is just the same echo command) Below is the output of crontab -l:

SHELL=/bin/sh  
PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin  
HOME=开发者_如何学C/home/myuser  
MAILTO=myuser  
* * * * * /home/myuser/shellscript.sh > /home/myuser/logfile  

Why is the script not getting executed, although crontab is obviously running? Permission for all files are set to rwxr-xr-x.


* * * * * /bin/sh /home/myuser/shellscript.sh

or

* * * * * /bin/bash /home/myuser/shellscript.sh

worked for me in Macosx 10.6 as rootuser


Have you checked that the command line has a linefeed/CR at the end of the line? I struggled for hours trying to find a reason for non-executing php script on cron when I simply hadn't pressed enter at the end of the line when I edited the cron jobs with crontab -e :-)


Have you checked /var/log/cron for clues?


Have you tried

* * * * * /bin/sh /home/myuser/shellscript.sh > /home/myuser/logfile 


cron sends any errors via email to owner of the crontab file (often "root" so you might check that account's email). To have any errors mailed to "crontabOnFreebsd" put:

MAILTO=crontabOnFreebsd

in your crontab (near the top).

For more info issue this command:

man 5 crontab


If you are getting an error, then your logfile might not capture it, try this:

* * * * * /home/myuser/shellscript.sh > /home/myuser/logfile 2> /home/myuser/errorfile

Its been a while since I did any cron stuff; but things that always used to get me:

  1. Environment variables not been set: generally I found it necessary to set up full paths (even to things like 'cat') to all commands [or at least set ENV variables within the script itself].

  2. Ensure the user who owns the script etc is really the user which is running the script: this might not be the same user when you test from the interactive shell. Same goes for the directories/files that the script might write to.

  3. Also: check the email for the root user - you might find that the errors have been diverted to the inbox, which may help you troubleshoot this further.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号