6

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?

DarkLeafyGreen
  • 69,338
  • 131
  • 383
  • 601

7 Answers7

4

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?

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • ok, when I put an echo at the top of my script I get an email with the output, so this seems to work but why does touch and curl not work? – DarkLeafyGreen Sep 11 '11 at 09:06
  • Try the other things I suggested. If you got the output of the `echo` by e-mail, then you ignored my advice to redirect the output to a file in `/tmp`. Your cron job, as you showed it to us, is designed to run only once. `touch` and `curl` should work fine; you're just not invoking them. – Keith Thompson Sep 11 '11 at 09:11
  • I check if sync.txt exists, for now it doesnt and the code in the if block should be executed, sync should be created but it doesnt, the curl command which should be executed starts a spider which after finishing deletes the sync file so that another spider can start, I do this to ensure that only one spider is active at a time, this works fine from command line. I tries your echo command but it seems to be not valid, there is a " missing somewhere? – DarkLeafyGreen Sep 11 '11 at 09:15
  • I just realized that touch works, the file is created in root – DarkLeafyGreen Sep 11 '11 at 09:24
  • @ArtWorkAD: Yes, there was a missing `"`; I've fixed it. – Keith Thompson Sep 11 '11 at 16:46
2

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 MAILTO=your@email.address 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).

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
Friek
  • 1,533
  • 11
  • 13
1

A better syntax for you CRON is

*/5 * * * * /scr_temp/scheduleSpider.sh

Also, check the authority of your scheduleSpider.sh file. Cron runs under a different user than the one you are likely executing your program interactively, so it may be that cron does not have authority. Try chmod 777 for now, just to check.

Codemwnci
  • 54,176
  • 10
  • 96
  • 129
  • @Codemwnci: No, cron jobs run under the same account as the user who executed the `crontab` command. The cron daemon itself runs as root, but it switches to the appropriate user account when it invokes the commands. – Keith Thompson Sep 10 '11 at 19:34
  • indeed. I am used to a corporate environment, where the crontab is created by a user different to the one running the command on the command line. However, reading the question again, I suspect you are right, and the user is likely the same. – Codemwnci Sep 10 '11 at 20:04
1

I suggest to:

  • check that /scr_temp/scheduleSpider.sh has executable bit
  • set PATH properly inside your script or use absolute path to command (/bin/touch instead of touch)
  • specify absolute path to sync.txt file (or calculate it relatively to script)
Slava Semushin
  • 14,904
  • 7
  • 53
  • 69
0

You should add /bin/sh before the absolute path of your script.

*/5 * * * * /bin/sh /scr_temp/scheduleSpider.sh
Juampa
  • 2,035
  • 2
  • 25
  • 35
0

Have you added the comand via crontab -e or just by editing the crontab file? You should use crontab -e to get it correctly updated.

Nornagest
  • 176
  • 5
0

Set the working directory in the cron script, it probably doesn't execute the things where you think it should.

Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176