2

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

Ajay Shewale
  • 159
  • 1
  • 2
  • 13

1 Answers1

9

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.

Shivam Singh
  • 1,584
  • 1
  • 10
  • 9
  • */1 * * * * python /Cron_Job/cronex.py >> /Cron_Job/out.txt For a minute I use this, I am not able to see any changes in crontab. – Ajay Shewale Jun 01 '18 at 10:20
  • You may want to capture stderr to the output file as well with */30 * * * * python /path-to-file/test.py >> /path-to-file/output.txt 2&>1 – DDay Nov 23 '20 at 13:06