Python Script Not Working via Cron

Crontab not executing a Python script?

What happens when you type

/home/me/project/myscript.py into the shell?

Can you explicitly use /usr/bin/python in your crontbb command?

Can you either use an absolute path to your test.db or cd to the correct directory then execute your python script?

This is helpful to have debug statements in your python and log some data. Crontab can be very tricky to debug.

Not able to run a python script after every 5mins using cron

I assume you are trying to automatically run below script every 5 minutes:

/home/anikde/Documents/pythonProjects/python_scripts/test/write.py

First, determine the location of your Python executable, using which python command. In below examples I assume the returned path to be /usr/bin/python.

  1. If editing your own crontab (crontab -e) try this command:
*/5 * * * * /usr/bin/python /home/anikde/Documents/pythonProjects/python_scripts/test/write.py

If no user is specified, the job is run as the user that owns the crontab file, using his environment. But you can also try adding the username

*/5 * * * * anikde /usr/bin/python /home/anikde/Documents/pythonProjects/python_scripts/test/write.py


  1. If editing root crontab (sudo crontab -e)
*/5 * * * * anikde /usr/bin/python /home/anikde/Documents/pythonProjects/python_scripts/test/write.py

crontab does not run my python script

You should change you crontab line as such to get stdout and stderr saved to the file:

*/1 *  * * *    gg   /usr/bin/python /home/gg/vida.py >> /home/gg/out1.txt 2>&1

Simply read out1.txt after crontab has run the line to see what's wrong

Edit after your comment:
Based on the error you've shared, I believe you're not actually writing anything in the /home/gg/sil.sil file:

doystr = 'doy ' + str(tnow.year) + ' ' + str(tnow.month) + ' ' + str(tnow.day) + ' ' + '> /home/gg/sil.sil'
os.system(doystr)

doystr does not evaluate to a shell command, I think you need to write the variable as below to write to the file.

doystr = 'echo "doy ' + str(tnow.year) + ' ' + str(tnow.month) + ' ' + str(tnow.day) + '" ' + '> /home/gg/sil.sil'

Crontab not executing my python script every 5 minutes

Finally the problem was with the paths inside my code. Crontab needs the absolute paths.

As I see it doesn't has problems to arrive to the user, but if then you have different subfolders in your main folder it will crash.

What I did to solve it, is obtain the main path, cwd,

import os
cwd = os.getcwd()

And define all paths adding cwd at the beggining, for example in my case:

csv_data = cwd + '/folder-name-path/data/myData.csv'
json_data = cwd + '/folder-name-path/temp/myTemp.json'

Also I added __init__.py in my lib folder not sure if it was necessary but it helps to python to understand this folder has scripts.

Thanks!!

Python print function not working with crontab

The standard output of a command executed from crontab is not sent to the standard output of your shell. (It wouldn't make much sense for it to do so; a cron job will continue to run after you've logged out.)

Typically crontab will (attempt to)email the output to you. This is configurable.man 5 crontab` for more information.

Rather than modifying your Python script to redirect its own output, you can redirect the output in the crontab entry itself:

* * * * * /usr/bin/python3 /home/my_user_folder/Desktop/test/myscript.py > output.txt


Related Topics



Leave a reply



Submit