简体   繁体   中英

Running a python script in every 30 minutes using Crontab?

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM