问题
How to run a cron job in ubuntu in such a way that it should initiate a python script?
Please explain with a small example.
回答1:
You can set a simple line to run from x to x time:
e.g.,
0,10,20,30,40,50 * * * * ~/py/my_python_script.py
runs every 10 minuts
STEP BY STEP USING VIM AS THE SELECTED EDITOR
At your terminal, run:
sudo crontab -e
Afterwards, choose you favorite editor (e.g., vim)
type
:i
and hitenter
to insert a new linePast or write the cronjob line
0,10,20,30,40,50 * * * * ~/py/my_python_script.py
and hitenter
and thenreturn
to exit that linetype
:w
and hitenter
, to write the filetype
:q
and hitenter
to exit
Description for the asterisks:
minute (0-59), hour (0-23, 0 = midnight), day (1-31), month (1-12), weekday (0-6, 0 = Sunday), command
In-deep:
To read a more detailed process on how it all works:
CronHowto
VIM Commands Cheat Sheet
回答2:
You can run Crontab in Ubuntu. Simply just copy and paste the following script in the terminal.
crontab -e
There you can write command for run your python script, that will execute the program in a specific time interval.
* * * * * python </path/to/the/file>
You can refer the link for the time interval
You can check the log file here
tail -f /var/log/syslog
来源:https://stackoverflow.com/questions/10720466/cron-jobs-in-ubuntu