I have a cron job that I want to execute every 5 minutes:
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /scr_temp/scheduleSpider.sh
In /var/spool/cron/crontabs/root
The cron should execute a shell script:
#!/bin/sh
if [ ! -f "sync.txt" ]; then
touch "sync.txt"
chmod 777 /scr_temp
curl someLink
fi
That works fine from command line but not from cron. However the cron itself is startet but the script does not start.
I read about the path problem but I dont really understand it. I setup a cron that writes some env data to a file. This is the output:
HOME=/root
LOGNAME=root
PATH=/usr/bin:/bin
SHELL=/bin/sh
If I execute the env command in command line I get following output for PATH
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
What path do I have to set in my shell script?
Check permissionsAnother user might have created the cron job and you may not be authorized to execute it. Note that the root must own jobs added as files in a /etc/cron. */ directory. That means that if you don't have root permissions, you might not be able to access the job.
*/5 * * * * Execute a cron job every 5 minutes. 0 * * * * Execute a cron job every hour.
To check to see if the cron daemon is running, search the running processes with the ps command. The cron daemon's command will show up in the output as crond. The entry in this output for grep crond can be ignored but the other entry for crond can be seen running as root. This shows that the cron daemon is running.
Your $PATH
is fine; leave it alone. On Ubuntu, all the commands you're invoking (touch
, chmod
, curl
) are in /bin
and/or /usr/bin
.
How did you set up the cron job? Did you run crontab some-file
as root?
It seems that /etc/crontab
is the usual mechanism for running cron commands as root. On my Ubuntu system, sudo crontab -l
says no crontab for root
. Running crontab
as root, as you would for any non-root account, should be ok, but you might consider using /etc/crontab
instead. Note that it uses a different syntax than an ordinary crontab, as explained in the comments at the top of /etc/crontab
:
$ head -5 /etc/crontab
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
Run sudo crontab -l
. Does it show your command?
Temporarily modify your script so it always produces some visible output. For example, add the following right after the #!/bin/sh
:
echo "Running scheduleSpider.sh at \`date\`" >> /tmp/scheduleSpider.sh.log
and see what's in /tmp/scheduleSpider.sh.log
after a few minutes. (You can set the command to run every minute so you don't have to wait as long for results.) If that works (it should), you can add more echo
commands to your script to see in detail what it's doing.
It looks like your script is designed to run only once; it creates the sync.txt
file to prevent it from running again. That could be the root (ahem) of your problem. What that your intent? Did you mean to delete sync.txt
after running the command, and just forgot to do it?
root
's home directory on Ubuntu is /root
. The first time your script runs, it should create /root/sync.txt
. Does that file exist? If so, how old is it?
Note that curl someLink
(assuming someLink
is a valid URL) will just dump the content from the specified link to standard output. Was that your intent (it will show up as e-mail to root
? Or did you just not show us the entire command?
First: you can substitute the first field with */5
(see man 5 crontab)
Second: have cron mail the output to your email address by entering [email protected]
in your crontab. If the script has any output, it'll be mailed. Instead of that, you may have a local mailbox in which you can find the cron output (usually $MAIL).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With