Home > Back-end >  Run Python Script in VM (Ubuntu) via Cron Job
Run Python Script in VM (Ubuntu) via Cron Job

Time:09-30

I have a problem where I want to run (daily at 2pm) the Python code in my VM, but the cron job didn't work as expected.

Here is the code that i tried in crontab -e:

#1    
0 14 * * * /home/try/myf.py
#2
0 14 * * * /home/try/miniconda3/bin/python /home/try/myf.py
#3
0 14 * * * python /home/try/myf.py
#4
0 14 * * * /home/try/miniconda3/bin/python myf.py

All of this code didn't work for me. Can anyone explain or advice me on how to do the code? If possible, how can I check the log of this cron job?

CodePudding user response:

Checklist:

  1. Check cron daemon running ? (ps aux | grep cron) or service cron status Service cron start or restart

  2. check if cron is working ? contab -e and add below line

            • ( /bin/date && /etc/echo "cronjob works" ) >> /tmp/file

    Then cat /tmp/file

There are other posts explaining checks and tests in detail, refer those

CodePudding user response:

#2 is correct, it works with miniconda3 on ubuntu instance.
Replace the code in myf.py with print('hello world') and try running ​ /home/try/miniconda3/bin/python /home/try/myf.py manually.
If it works, then it's a crontab issue. There's a great troubleshooting guide here.
If it doesn't work, then your miniconda installation is corrupted. Try reinstalling it following these instructions. Remember to close the terminal and re-open it after the installation, otherwise it won't work even if the installation has been successful.

  • Related