skip to Main Content

I am trying to run python script via a cron job, but have had no luck the last two days, and I am running out of hairs to pull out.

Some info:

–>I have approximately 20 hours experience using Linux, so I might have missed something very basic.

–>Linux server (Ubuntu) on Linode.com

–>Script runs in terminal

–>Script has the permissions “0644”

–>#!/usr/bin/env python3.7
is added to the beginning of the script

–>The script belongs to the user “adamsavage” and I have tried to add it to both this users cron file and the cron file belonging to root using crontab -e and sudo crontab -e respectively

–>The cron files looks like this, and has a newline at the end:

* * * * * /usr/bin/python3 /home/adamsavage/python-scripts/send_new_sessions.py >> /home/adamasavage/log.txt 2>&1

–>sudo grep CRON /var/log/syslog returns this:

Apr  2 15:25:01 noeluddig CRON[7728]: (adamsavage) CMD (/home/adamsavage/python_scripts/send_new_sessions.py >> /home/adamasavage/log.txt)
Apr  2 15:25:01 noeluddig CRON[7729]: (root) CMD (/home/adamsavage/python_scripts/send_new_sessions.py >> /home/adamasavage/log.txt)
Apr  2 15:25:01 noeluddig CRON[7730]: (adamsavage) CMD (echo 'Yo' >> /home/adamsavage/log.txt)
Apr  2 15:25:01 noeluddig CRON[7731]: (root) CMD (echo 'Yo' >> /home/adamsavage/log.txt)
Apr  2 15:25:01 noeluddig CRON[7732]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)

–>I have also tried the following:

* * * * * cd /home/adamsavage/python_scripts/ && /home/adamsavage/python_scripts/send_new_sessions.py >> /home/adamasavage/log.txt

and

* * * * * cd /home/adamsavage/python_scripts/ && /usr/bin/python /home/adamsavage/python_scripts/send_new_sessions.py >> /home/adamasavage/log.txt

-The script is supposed to send me an sms with some info, which again works when it is ran from terminal.

-I should also mention that just having echo "message" >> /home/adamsavage/ouput.txt does actually run and prints “message” to that file.

What am I missing? Suffice to say, help will be greatly appreciated!:)

3

Answers


  1. Chosen as BEST ANSWER

    I have no idea why this is, but what made it work was to remove the absolute path to the log file. If anyone can explain this, that would be great!


  2. You should run your python script with python command:

    * * * * * path/to/python /home/adamsavage/python_scripts/send_new_sessions.py >> /home/adamasavage/log.txt
    
    

    For example:

    * * * * * /usr/bin/python3 /home/adamsavage/python_scripts/send_new_sessions.py >> /home/adamasavage/log.txt
    
    
    Login or Signup to reply.
  3. Try to add 2>&1 at the end of your cron line:

    * * * * * path/to/python /home/adamsavage/python_scripts/send_new_sessions.py >> /home/adamasavage/log.txt 2>&1

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search