What is wrong with my cron job?

22,238

Solution 1

Make sure your command works from the command line first

/usr/bin/python /home/MyName/WeatherProject/HOURLY/windDirExtract.py

Then try the same in the crontab

*/10 * * * * /usr/bin/python /home/MyName/WeatherProject/HOURLY/windDirExtract.py

Solution 2

Your script seems to work fine, running this from cron though will not work as expected. For example:

$ ./windDirExtract.py 

START
2013-09-23 22:32:39.376584
283

Running this script produces the output above. But with cron the output needs to go somewhere. I would suggest writing it out to a file to start, like so via cron:

*/10 * * * * /home/MyName/WeatherProject/HOURLY/windDirExtract.py >> /home/MyName/weather.log

This will capture the output to the log file, weather.log where you can review it to see if the cron is running correctly. Doing the above will continuously append the output every 10 minutes from your script to the log file like this:

START
2013-09-23 22:32:39.376584
283
START
2013-09-23 22:42:39.376584
283
...
Share:
22,238

Related videos on Youtube

Monte Carlo
Author by

Monte Carlo

Updated on September 18, 2022

Comments

  • Monte Carlo
    Monte Carlo over 1 year

    I'm new to linux, and was told I could use a feature called cron job to run a python script I made execute every 10 minutes. Problem is the the python script isn't running. There doesn't seem to be a clear cut way to know if I've formatted the job correctly, but in any case I've tried a number of paths to no avail.

    Because I'm so new I'll show you the steps I took. On my RasPi, I first typed "crontab -e" and get a field like this:

    # Edit this file to introduce tasks to be run by cron.
    #
    # Each task to run has to be defined through a single line
    # indicating with different fields when the task will be run
    # and what command to run for the task
    

    Skipping more help text

    # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
    
    # m h  dom mon dow   command
    

    and I add my line at the bottom, without removing the text above.

    */10 * * * * ~/WeatherProject/HOURLY/windDirExtract.py
    

    The above didn't work, and someone else did something similar to this but still no luck.

    */10 * * * * /home/MyName/WeatherProject/HOURLY/windDirExtract.py
    

    and then I tried different permutations but I've got nothing.

    */10 * * * * /home/MyName/WeatherProject/HOURLY python windDirExtract.py
    */10 * * * * /home/MyName/WeatherProject/HOURLY windDirExtract.py
    

    I'll make any edits if I haven't been clear, but first big question is can I even run short little python scripts with Cron Job, or did I just get bad advice?

    • phemmer
      phemmer over 10 years
      Is your script marked executable (chmod +x), and does it begin with #!/usr/bin/python?
    • slm
      slm over 10 years
      Check the logs, Redhat:sudo grep CRON /var/log/messages or Debian/Ubuntu: sudo grep CRON /var/log/syslog
    • Monte Carlo
      Monte Carlo over 10 years
      If my understanding of what a distro is happens to be correct, I'm running Raspbian.
    • slm
      slm over 10 years
      Do a ps -eaf|grep cron do you get any processes running?
    • Monte Carlo
      Monte Carlo over 10 years
      slm for sudo grep CRON I get a log Sep 24 01:30:01 raspberrypi /USR/SBIN/CRON[13386]: (Oscar) CMD (~/WeatherProject/HOURLY/windDirExtract.py) Sep 24 01:30:01 raspberrypi /USR/SBIN/CRON[13385]: (CRON) info (No MTA installed, discarding output)
    • Monte Carlo
      Monte Carlo over 10 years
      Meanwhile ps -eaf|grep cron gives me this output root 1953 1 0 Sep06 ? 00:00:06 /usr/sbin/cron Bob 13422 13219 0 01:37 pts/0 00:00:00 grep --color=auto cron
    • slm
      slm over 10 years
      OK, good so you have cron and it's running. The MTA is mail transfer agent, cron tries to send an email with the results of what ran in it, this says it wasn't able to do that.
    • slm
      slm over 10 years
      Perfect so we have a functioning cron, that's 1/2 the battle.
    • slm
      slm over 10 years
      Your script is likely not running for something in the script itself then. Can you share that? You can use pastebin.com to share large files. Post link here.
    • slm
      slm over 10 years
      When ready please prefix the comment with @slm - so I know you've replied to me, otherwise I will lose sight of your Q, I'm bouncing to other ones as well.
    • slm
      slm over 10 years
      See my answer, let me know if that helps?