I have issue with my crontab - cron is not launching one of my scripts

this is top part of my crontab (root)

SHELL=/bin/bash

#---------------------------------------
# Items availability
#---------------------------------------


# Daily offer import + daily sync everything
30 7 * * *      /var/www/import/download_offers.sh > /var/logs/download_offers_cron.log

# Updates availability
# 7:45 mass update happens
*/5 0-6 * * *   /var/www/import/check_availability.sh
0,5,10,15,20,25 7 * * * /var/www/import/check_availability.sh
*/5 8-23 * * * /var/www/import/check_availability.sh

#---------------------------------------
# Sales import + sync
#---------------------------------------

# Import EBAY + WWW sales. Run sales cleaner
*/15 * * * *    /var/www/import/import_all.sh
*/5 * * * *     /var/www/import/import_www.sh
55 13 * * *     /var/www/import/import_all.sh
58 13 * * *     /var/www/import/import_www.sh

... more stuff 7KB total ...

it used to work perfect for long time but after last week reboot this line

30 7 * * *      /var/www/import/download_offers.sh > /var/log/download_offers_cron.log

stopped working. contents of download_offers.sh:

#!/bin/sh

echo "Working..."

echo "Download offers started" > /var/log/import/OFFER_START.log

... some private stuff ; just bunch of wget's and echo's ...

file /var/log/import/OFFER_START.log is not created

what I have tried so far

  • I changed time to 7:30, 8:00 (it used to be 7:45 originaly)
  • I added > /var/logs/download_offers_cron.log but file hasn't been created
  • I browsed root emails, all scripts are launched except that one
  • file is +x executable

is there some known bug? is there any other option to debug crontab except mails? why script doesn't launch at all? it used to work fine after last week reboot....

Please help I am losing my mind

Edit:

I am using CentOS release 6.6 (Final)

this is /var/log/cron

Mar 10 07:30:01 serverpro1 CROND[11291]: (root) CMD (/var/www/import/download_offers.sh > /var/logs/download_offers_cron.log)

looks like cron entry is fine but there is no /var/log/download_offers_cron.log file anyway?

  • 3
    Run the scrip manually and see if there are any errors – Manula Waidyanatha Mar 10 '15 at 11:59
  • @ManulaWaidyanatha it works fine – Peter Mar 10 '15 at 13:00
  • 3
    If you run it manually and it works, but doesn't work in cron, then there probably is an environment variable issue. Cron by default only provides a very minimal shell environment. Pay particular attention to PATH and perhaps LD_LIBRARY_PATH varaiables. Best to manually set all required variables in your script than depend on cron to provide. One way to tell what cron provides is to add a very simple cron entry 1 * * * * env > /tmp/cron.env (make sure you delete this entry after it's run as it will keep running at 1 minute after the hour forever if you don't) – Cosmo F Mar 10 '15 at 14:11
  • @CosmoF i dont think thats not a case, my "script" is just bunch of echos and wgets – Peter Mar 10 '15 at 15:32
  • 1
    NB: your crontab says SHELL=/bin/bash, then in your script you use #!/bin/sh. Your cron is really running as root so it can write to the directories where your logs go? Can you add a ( date ; set ) >>/tmp/cron.log as 2nd line to your script? – ott-- Mar 10 '15 at 17:22
up vote 1 down vote accepted

it magically works again

only thing i changed was

30 7 * * *      /var/www/import/download_offers.sh > /var/logs/download_offers_cron.log
                                                             ^^ here

to

30 7 * * *      /var/www/import/download_offers.sh > /var/log/download_offers_cron.log
                                                             ^^ here

why it caused error and script didnt work AT ALL? I dont know

and why script stopped works in first place is unsolved mystery

  • Because try running /var/www/import/download_offers.sh > /var/logs/download_offers_cron.log manually and you'll see it should fail with an error like "bash: /var/logs/download_offers_cron.log: No such file or directory". – iyrin Mar 17 '15 at 10:06
  • 3
    And the reason it fails is because the redirect operation > is performed before the command is executed. gnu.org/software/bash/manual/bashref.html#Shell-Operation – iyrin Mar 17 '15 at 10:08

Check what your /var/log/syslog file says about cron job. Grep the word "cron" from the logs and check what is the error. Use below command to check the same.

grep -i cron /var/log/syslog

Otherwise everything you mentioned is good.

  • i am using centos, I checked /var/log/messages but there is nothing there – Peter Mar 10 '15 at 13:03
  • Which version you are using ? Cron logs on CentOS 6 are located in /var/log/cron by default. – Nitesh B. Mar 10 '15 at 13:12
  • I am using CentOS release 6.6 (Final). see my edit – Peter Mar 10 '15 at 15:31

Your crontab says to use bash, but when you run you script you actually shell out again to the 'sh' shell, thus you lose all your environment variables. I just had this problem last week. My problem was I had no path variable when the script ran in cron, but the script ran fine when I ran it manually.

You can test this by doing this right at the top of your script, after the #!/bin/sh:

echo $PATH
echo $path
sleep 10

in your script when it is run by cron. I can't recall which path is used by 'sh' so echo both.

Maybe do not shell out to 'sh' in your script?

Or, for each utility you run, enter in the full path to the utility, each input, and each output file.

So if your script says:

wget ...

You would replace it with the full path:

/usr/bin/wget ...

or whatever the correct path is.

(I can't add comments yet so I won't be able to reply to comments. Sorry.)

It's seems to be a typo mistake.
In the first script you use /var/log but in the new one it's /var/logs. Log is the standard directory but you seems to use logs

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.