linux下设置计划任务执行python脚本

时间:2022-05-24 18:34:18

linux下设置计划任务执行python脚本

简介

crontab命令被用来提交和管理用户的需要周期性执行的任务,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自动启动crond进程,crond进程每分钟会定期检查是否有要执行的任务,如果有要执行的任务,则自动执行该任务。

crontab语法

crontab (选项)(参数)
-e:编辑该用户的计时器设置;
-l:列出该用户的计时器设置;
-r:删除该用户的计时器设置;
-u<用户名称>:指定要设定计时器的用户名称。

cron目录

Linux下的任务调度分为两类:系统任务调度和用户任务调度。

/var/spool/cron/存放用户任务

/etc/cron.deny     该文件中所列用户不允许使用crontab命令
/etc/cron.allow 该文件中所列用户允许使用crontab命令
/var/spool/cron/ 所有用户crontab文件存放的目录,以用户名命名

/etc/crontab存放系统任务

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

前四行是用来配置crond任务运行的环境变量,第一行SHELL变量指定了系统要使用哪个shell,这里是bash,第二行PATH变量指定了系统执行命令的路径,第三行MAILTO变量指定了crond的任务执行信息将通过电子邮件发送给root用户,如果MAILTO变量的值为空,则表示不发送任务执行信息给用户,第四行的HOME变量指定了在执行命令或者脚本时使用的主目录。

/etc/cron.d/存放需要执行的文件

cron格式

minute hour day month week command      顺序:分 时 日 月 周
  • minute:表示分钟,可以是从0到59之间的任何整数
  • hour:表示小时,可以是从0到23之间的任何整数
  • day:表示日期,可以是从1到31之间的任何整数
  • month:表示月份,可以是从1到12之间的任何整数
  • week:表示星期几,可以是从0到7之间的任何整数,这里的0或7代表星期日
  • command:要执行的命令,可以是系统命令,也可以是自己编写的脚本文件

特殊符号

*代表所有可能的值

,指定一个列表范围,如1,2,3

-指定一个整数范围,如1-3表示1,2,3

/指定频率,如*/5,如果用在minute字段,表示每五分钟执行一次

例子:每周四的下午15点15分执行test.py,并导出错误日志到log文件

15 15 * * 4 python /home/*/test.py >> /home/*/error.log 2>&1

cron服务

# 启动服务
root@0484627f5c69:/# service cron start
[ ok ] Starting periodic command scheduler: cron.
# 关闭服务
root@0484627f5c69:/# service cron stop
[ ok ] Stopping periodic command scheduler: cron.
# 重启服务
root@0484627f5c69:/# service cron restart
[....] Restarting periodic command scheduler: cron[....] Stopping periodic comma[ ok heduler: cron.
[ ok ] Starting periodic command scheduler: cron.
# 重新载入配置
root@0484627f5c69:/# service cron reload
[ ok ] Reloading configuration files for periodic command scheduler: cron.
# 查看服务状态
root@0484627f5c69:/# service cron status
[ ok ] cron is running.

查看crontab服务是否已设置为开机启动

ntsysv

加入开机自动启动

chkconfig -level 35 crond on

实战

每周二的2点40分,执行python脚本test.py,输出crontestout.txt,脚本代码如下

with open('/crontest/out.txt','w') as f:
f.write('crontest')

同步系统时间

root@0484627f5c69:/# date
Tue Jan 29 07:02:50 UTC 2019
root@0484627f5c69:/# tzselect
Please identify a location so that time zone rules can be set correctly.
Please select a continent, ocean, "coord", or "TZ".
1) Africa
2) Americas
3) Antarctica
4) Asia
5) Atlantic Ocean
6) Australia
7) Europe
8) Indian Ocean
9) Pacific Ocean
10) coord - I want to use geographical coordinates.
11) TZ - I want to specify the time zone using the Posix TZ format.
#? 4
Please select a country whose clocks agree with yours.
1) Afghanistan 18) Israel 35) Palestine
2) Armenia 19) Japan 36) Philippines
3) Azerbaijan 20) Jordan 37) Qatar
4) Bahrain 21) Kazakhstan 38) Russia
5) Bangladesh 22) Korea (North) 39) Saudi Arabia
6) Bhutan 23) Korea (South) 40) Singapore
7) Brunei 24) Kuwait 41) Sri Lanka
8) Cambodia 25) Kyrgyzstan 42) Syria
9) China 26) Laos 43) *
10) Cyprus 27) Lebanon 44) Tajikistan
11) East Timor 28) Macau 45) Thailand
12) Georgia 29) Malaysia 46) Turkmenistan
13) * 30) * 47) United Arab Emirates
14) India 31) Myanmar (Burma) 48) Uzbekistan
15) Indonesia 32) Nepal 49) Vietnam
16) Iran 33) Oman 50) Yemen
17) Iraq 34) Pakistan
#? 9
Please select one of the following time zone regions.
1) Beijing Time
2) * Time
#? 1 The following information has been given: China
Beijing Time Therefore TZ='Asia/Shanghai' will be used.
Local time is now: Tue Jan 29 15:03:26 CST 2019.
Universal Time is now: Tue Jan 29 07:03:26 UTC 2019.
Is the above information OK?
1) Yes
2) No
#? 1 You can make this change permanent for yourself by appending the line
TZ='Asia/Shanghai'; export TZ
to the file '.profile' in your home directory; then log out and log in again. Here is that TZ value again, this time on standard output so that you
can use the /usr/bin/tzselect command in shell scripts:
Asia/Shanghai
root@0484627f5c69:/# TZ='Asia/Shanghai';
root@0484627f5c69:/# export TZ
root@0484627f5c69:/# date
Tue Jan 29 15:04:56 CST 2019