Python Script Is Not Running Under Cron, Despite Working When Run Manually

Python script is not running under cron, despite working when run manually

Use fully qualified path for all commands in crontab.

0 */2 * * * /full/path/to/python /home/ec2-user/cronscripts/testscript.py

Also, all fields are *? When do you expect this to be run? The above example would run every other hr.

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

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

Crontab isn't running my python script in macOs although the script works when run manually

I'll suggest to move your script into a bash script first, and add some logs to find the issue. The bash script will look like this:

trash.sh

cd ~/Desktop/Projects/script/Trash
python trash_script.py

Change the execution permissions of your script

chmod +x trash.sh

Modify your cron job to look like this:

0 * * * * sh <path_to_bash_script>/trash.sh >> ~/Desktop/Projects/script/Trash/trash.log 2>&1

This will help to have some output every time the script runs.

Cron Job Running Shell Script to Run Python Not Working

There are a few areas to check if you haven't performed these already,

  1. if your executable permissions set on the script,
    chmod +x <python file>
    in addition permissions for the user to access the directories.

  2. Run the script manually to test the script works from beginning to end, as the user who will be running the script, will be more realistic.

  3. You can test your crontab schedule by temporarily setting every minute for testing, unlike Windows where you can right, click and click Run.



Related Topics



Leave a reply



Submit