Crontab Not Executing a Python Script

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.

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

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

CRON not running python script - Debian/RPi

The way how run your script causes the problem. add python command ahead.

35 10 * * 1-5 python /home/pi/Desktop/priceChange.py

Cron, execute bash script as root, but one part (Python script) as user

Well, if the dummy folders did get created, that means the sudo statements work, so i'd say theres a 99%+ chance that python was infact started.

I'm guessing the problem is that you havent specified the path for the python file, and your working directory likely isn't what you're expecting it to be.

change:

sudo -u user_name /usr/bin/python3 python_script.py --arg01 --arg02

to something like

sudo -u user_name /usr/bin/python3 /path/to/your/python_script.py --arg01 --arg02

then test.

If that didn't solve the problem , then enable some logging, change the line to:

sudo -u user_name /usr/bin/python3 /path/to/your/python_script.py --arg01 --arg02 \
1> /home/user_name/projects/dummy_folder_00/log.txt 2>&1 ;

and test again, it should log STDOUT and STDERR to that file then.



Related Topics



Leave a reply



Submit