ubuntu regularly executes python script instance code
- 2021-01-18 06:55:40
- OfStack
The original link: https: / / vien tech/article / 157
preface
In this article, we will introduce how to execute shell scripts and python scripts on a regular basis. ubuntu has a scheduled task manager crontab. We just need to edit the scheduled task and restart the scheduled task service.
crontab
Edit Timing Task
crontab -e
Parameter definition:
-- u lists the user task plan, -r delete user task, -e Edit user tasksIntroduction to English:
[
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
Chinese interpretation:
format
[
m h dom mon dow command
Above is abbreviation, here provides full spelling contrast:
[
minute (m), hour (h), day of month (dom), month (mon), day of week (dow)
It means as follows:
m performs this task every minute of the hour What hour of the day does h perform this task On what day of the month does dom perform this task Which month of the year does mon carry out this mission What day of the week does dow perform this task - command specifies the program to execute [
Minute hour day month week order
0-59 0-23 1-31 1-12 0-6 command
Other:
Midweek 0 stands for Sunday. The * represents any time, such as the first minute, and the * represents every minute of every hour Minus is the interval, like 1 minus 3 , if the interval is not continuous, you can use, for example, 1,3,6 after editing ES106en save exit
Restart the service
service cron restart
Matters needing attention
Notice that 1 has to be an absolute path. Otherwise, the execution may fail.
For example, we're going to execute
python bwh.py
So the first thing you need to do is
which python
To see the actual path of the python command
root@ubuntu:~# which python
/root/.pyenv/shims/python
Then, check the full path of bwh.py in the folder where bwh.py is located
pwd
/app/python/blog
And then the path is zero
/app/python/blog/bwh.py
So the entire record should be edited like this
0 9 * * * /root/.pyenv/shims/python /app/python/blog/bwh.py > /tmp/new_blog_bwh.log
The record above refers to the daily execution at 9 o 'clock sharp
/root/.pyenv/shims/python /app/python/blog/bwh.py
And outputs the print log to
/tmp/new_blog_bwh.log
conclusion