Ad

Running A Python Script In Every 30 Minutes Using Crontab?

- 1 answer

I want to run a python script for every 30 minutes. For this, I am using crontab. I am new to crontab, I read and to run a script for 30 mins I have to use a query something like this:

*/30 * * * * python filename.py

But where exactly I have to fire this command.

I tried,

crontab -e

and change the file into,

*/30 * * * * python filename.py

Can someone explain how can I use crontab properly?

PS: I want to run a script for every 30 mins on a server that I have created on AWS ec2 instance, is there any alternate solution?

I am using Ubuntu 16.04

Ad

Answer

Suppose I have a python file test.py with the contents

print "hello"

To schedule it to run every 30 minutes, use

crontab -e

Then edit to add

*/30 * * * * python /path-to-file/test.py

To check if cron ran succesfully

grep CRON /var/log/syslog

Here, you will see in logs, lines like

May 31 14:25:01 shivam-PC CRON[17805]: (shivam) CMD (python /home/shivam/test.py)

Note: print statement might not show in logs, so use

*/30 * * * * python /path-to-file/test.py >> /path-to-file/out.txt

and then check out.txt for print logs.

An alternate solution would be to use Celery.

Ad
source: stackoverflow.com
Ad