linux任务计划cron
crontab
-u :指定某个用户,不加-u选项则为当前用户;
-e :制定计划任务;
-l :列出计划任务;
-r :删除计划任务。
格式:分 时 日 月 周 user command
可用格式1-5表示一个范围1到5。如1-5月或者1-5点等
可用格式1,2,3表示1或者2或者3。如1,2,3月份或者1点,2点,3点或者星期一,星期二,星期三。
可用格式*/2表示被2整除的数字,比如小时,那就是每隔2小时;月份,那就是偶数月2月,4月…..。
使用 crontab -e 来进行编写任务计划,这实际上是使用vim工具打开了crontab的配置文件。crontab -e 实际上是打开了 “/var/spool/cron/username” (如果是root则打开的是/var/spool/cron/root)这个文件
分范围0-59,时范围0-23,日范围1-31,月范围1-12,周0-6(0表示周日,也可以用7表示,也都可以用英文简写sun,mon,tue,wed,thu,fri,sat)
启动计划任务服务:
systemctl start crond
停止任务计划服务:
systemctl stop crond
检查服务是否启动:
systemctl status crond
有绿色的running就是启动了
或者查看进程ps aux | grep cron
有下列框子的进程说明启动了
要保证服务是启动状态
systemctl start crond.service
练习题:
1. 每天凌晨1点20分清除/var/log/slow.log这个文件
2. 每周日3点执行 “/bin/sh /usr/local/sbin/backup.sh”
3. 每月14号4点10分执行 “/bin/sh /usr/local/sbin/backup_month.sh”
4. 每隔8小时执行 “ntpdate time.windows.com”
5. 每天的1点,12点,18点执行 “/bin/sh /usr/local/sbin/test.sh”
6. 每天的9点到18点执行 “/bin/sh /usr/local/sbin/test2.sh”
习题答案:
1. 20 1 * * * echo “” >/var/log/slow.log
2. 0 3 * * 0 /bin/sh /usr/local/sbin/backup.sh
3. 10 4 14 * * /bin/sh /usr/local/sbin/backup_month.sh
4. 0 */8 * * * ntpdate time.windows.com
5. 0 1,12,18 * * * /bin/sh /usr/local/sbin/test.sh
6. 0 9-18 * * * /bin/sh /usr/local/sbin/test2.sh
chkconfig工具
查看使用chkconfig这个工具的服务端有哪些:
chkconfig –list
0:关机状态
1:单用户
2:无NFS支持的多用户模式
3:完全多用户模式
4:保留给用户自定义
5:图形登录方式
6:重启
ls /etc/init.d/ //服务端的启动脚本都放在这个目录下,只有启动脚本放在这个目录下才能加入系统服务中。把network的2,3,4,5级别打开/关闭:
chkconfig network on/off把network的3级别关闭/打开:
chkconfig –level 3 network off/on把network的3,4,5级别都关闭/打开:
chkconfig –level 345 network off/on删除network服务端:
chkconfig –del network添加network服务端(在/etc/init.d/目录下添加了启动脚本后需要用这个命令才能把服务加入到系统服务中):
chkconfig –add network
systemd管理服务
查看所有的系统服务(包含很多,会比较乱):
systemctl list-unit-files
列出所有的service的服务情况(如果不加all,不激活状态的就不会列出来):
systemctl list-units –all –type=service
几个常用的服务相关的命令
让某个服务开机启动(.service可以省略):
systemctl enable crond.service
不让开机启动:
systemctl disable crond
查看状态:
systemctl status crond
停止服务:
systemctl stop crond
启动服务:
systemctl start crond
重启服务:
systemctl restart crond
检查服务是否开机启动:
systemctl is-enabled crond
unit介绍
- 系统所有unit,分为以下类型:
ls /usr/lib/systemd/system
service:系统服务
target:多个unit组成的组
device:硬件设备
mount:文件系统挂载点
automount:自动挂载点
path:文件或路径
scope:不是由systemd启动的外部进程
slice:进程组
snapshot:systemd快照
socket:进程间通信套接字
swap:swap文件
timer:定时器
以上每种类型的文件都为一个unit,正式这些unit才组成了系统的各个资源(各个服务,各个设备等)。
unit相关的命令
列出正在运行(active)的unit:
systemctl list-units列出所有,包括失败的或者inactive的:
systemctl list-units –all列出inactive的unit:
systemctl list-units –all –state=inactive列出所有状态的service:
systemctl list-units –all –type=service列出状态为active的service:
systemctl list-units –type=service查看某个服务是否为active:
systemctl is-active crond.service
target介绍
系统为了方便管理用target来管理unit
列出系统所有的target:
systemctl list-unit-files –type=target
查看指定target下面有哪些unit,如下列的multi-user:
systemctl list-dependencies multi-user.target查看系统默认的target:
systemctl get-default设置默认的target:
systemctl set-default multi-user.target一个service属于一种类型的unit
多个unit组成了一个target
一个target里面包含了多个service
cat /usr/lib/systemd/system/sshd.service //看[install]部分,定义了该service属于哪一个target
扩展
1. anacron http://blog.csdn.net/strikers1982/article/details/4787226
2. xinetd服(默认机器没有安装这个服务,需要yum install xinetd安装) http://blog.sina.com.cn/s/blog_465bbe6b010000vi.html
3. systemd自定义启动脚本 http://www.jb51.net/article/100457.htm