Run the command: crontab -e
Add a Cron Job Entry
* * * * * /usr/bin/python3 /path/to/script.py param1 param2 >> /path/to/logfile.log 2>&1
## param1 param2 are the run parameters passed from cronjob to python script
Modify the python script to accept parameter (Optional)
import sys
def main(param):
print(f"Received parameter: {param}")
if __name__ == "__main__":
if len(sys.argv) > 1:
main(sys.argv[1])
else:
print("No parameter provided!")
With Python virtual environment
0 3-11 * * 1-5 /home/user/myproject/venv/bin/python /home/user/myproject/script.py my_param >> /home/user/script.log 2>&1
Sample CronJobs
0 3-11 * * 1-5 /home/user/myproject/venv/bin/python /home/user/myproject/script.py my_param >> /home/user/script.log 2>&1
0 3-11 * * 1-5 /usr/bin/python3 /home/user/script.py my_param >> /home/user/script.log 2>&1
##run the script every hour from 3:00 AM to 11:00 AM UTC (Monday to Friday):
*/30 3-11 * * 1-5 /usr/bin/python3 /home/user/script.py my_param >> /home/user/script.log 2>&1
##run it every 30 minutes:
30 3-11 * * 1-5 /usr/bin/python3 /home/user/script.py my_param >> /home/user/script.log 2>&1
## Monday through Friday (1-5 for weekdays) every hour from 3:30 AM to 11:30 AM UTC:
Note :
Cronjob executes based on UTC. Convert other timezones to UTC before scheduling
Reference
Leave a Reply