1

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!:)

  • first of all: if you do "/home/adamsavage/python_scripts/send_new_sessions.py >> /home/adamasavage/log.txt" from terminal as root/normaluser does it run as expected? – gekigek99 Apr 02 '20 at 15:58
  • Thank you for your reply. No it does not run with the ">> logfile" part. But should it? It runs just using "/home/adamsavage/python-scripts/send_new_sessions.py" – tomatoesarefruit Apr 02 '20 at 16:49
  • take a look at this: https://stackoverflow.com/questions/4675728/redirect-stdout-to-a-file-in-python – gekigek99 Apr 03 '20 at 10:09

3 Answers3

0

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

Gabio
  • 9,126
  • 3
  • 12
  • 32
  • Thank you for your reply. "which python" returns "/usr/bin/python", so now the cronfile looks like this, but it will still not work. "* * * * * /usr/bin/python /home/adamsavage/python-scripts/send_new_sessions.py >> /home/adamasavage/log.txt" – tomatoesarefruit Apr 02 '20 at 16:50
  • could you run the script on the server without crontab and without appending the output to file and check it returns any errors? – Gabio Apr 02 '20 at 16:57
  • Yes, it runs both if I cd into the directory and then run "python3.7 send_new_sessions.py" and if I just run "/home/adamsavage/python-scripts/send_new_sessions.py" – tomatoesarefruit Apr 02 '20 at 17:52
0

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

Gabio
  • 9,126
  • 3
  • 12
  • 32
  • Hi, sorry I forgot to mention in my post that I have also tried this. But here is the "result" `Apr 2 18:25:01 noeluddig CRON[11735]: (root) CMD (/usr/bin/python3 /home/adamsavage/python-scripts/send_new_sessions.py >> /home/adamasavage/log.txt 2>&1) Apr 2 18:25:01 noeluddig CRON[11736]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)` – tomatoesarefruit Apr 02 '20 at 18:27
  • So I ran out of ideas :/ It may be a silly question but are you sure that your code prints something in each run? Another idea that came up to me as we speak - is the same behaviour happens if you schedule it in another frequency? Maybe your job is run more than one minute and then another starts and they have race condition when trying to write to the file... – Gabio Apr 02 '20 at 18:50
  • At this point nothing is silly.. :p Yes, when I run it from terminal, it both prints out a comment, as well as add a line to a separate log file. And the process takes approximately 2 seconds (its calling a sms api) so it should not run two at the same time. Anyway, thanks for your time and input! I sometimes see something like "PATH=" in cron examples. Do you know what that is, and is it something I should care about? – tomatoesarefruit Apr 02 '20 at 20:03
  • I don't think that PATH is related here. I would try to remove #!/usr/bin/env python3.7 from the file. I tried to reproduce it on my ubuntu machine and it works (with the /usr/bin/env and with python command). – Gabio Apr 02 '20 at 20:08
  • Could you elaborate how you used "/usr/bin/env"? Or could you show me how your cron file looks like? – tomatoesarefruit Apr 02 '20 at 20:23
  • I mean to remove `#!/usr/bin/env python3.7` from the beginning of your script – Gabio Apr 02 '20 at 20:27
  • Aight, tried that as well. I think I have to leave this for a while and come back to it :p – tomatoesarefruit Apr 02 '20 at 20:36
0

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!