任务计划书写格式linux
[root@linux-5 ~]# cat /etc/crontab SHELL=/bin/bash //定义shell变量 PATH=/sbin:/bin:/usr/sbin:/usr/bin //定义环境变量(命令的路径,不在此路径内的命令若是不使用绝 对路径执行,则有可能不会生效) MAILTO=root //发送邮件给谁 # For details see man 4 crontabs # Example of job definition: # .---------------- minute (0 - 59) # | .------------- hour (0 - 23) # | | .---------- day of month (1 - 31) # | | | .------- month (1 - 12) OR jan,feb,mar,apr ... # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat # | | | | | # * * * * * user-name command to be executed //编辑格式 5个*表明5个时间单位:分 时 日 月 周 “*”还表明所有的意思,如在第三位使用*表明每日,第四位使用“*”则表明每个月 user-name 用户名(不写用户名,默认是root) command 想要执行的命令 分范围0-59,时范围0-23,日范围1-31,月范围1-12,星期1-6(星期日可用0或7表示) 可用格式1-5表示一个范围1到5 可用格式1,2,3表示1或者2或者3 可用格式*/2表示被2整除的数字,好比小时,那就是每隔2小时
• 定义任务计划shell
crontab -e
注:定义任务计划时,编辑须要执行的命令的格式,最好均采用绝对路径,不然易出现问题vim
• 例:每一个偶数月的1-10号中的周二和周五的03时00分执行脚本123.sh,并将正确日志追加剧定向到/tmp/123.log,错误日志追加剧定向到/tmp/456.logcentos
0 3 1-10 */2 2,5 /bin/bash /user/loacl/sbin/123.sh >>/tmp/123.log 2>>/tmp/456.log
• 要保证服务是启动状态,才能保证任务计划可以正常使用bash
systemctl start crond
• 检查服务是否正常启动socket
[root@linux-5 ~]# ps aux |grep cron root 566 0.0 0.0 126232 1664 ? Ss 12:42 0:00 /usr/sbin/crond -n root 1707 0.0 0.0 112676 984 pts/0 S+ 13:52 0:00 grep --color=auto cron [root@linux-5 ~]# systemctl status crond ● crond.service - Command Scheduler Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled) Active: active (running) since 五 2018-05-11 12:42:52 CST; 1h 11min ago //已经启动 Main PID: 566 (crond) CGroup: /system.slice/crond.service └─566 /usr/sbin/crond -n 5月 11 12:42:52 linux-5 systemd[1]: Started Command Scheduler. 5月 11 12:42:52 linux-5 systemd[1]: Starting Command Scheduler... 5月 11 12:42:52 linux-5 crond[566]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 93% if used.) 5月 11 12:42:52 linux-5 crond[566]: (CRON) INFO (running with inotify support)
• 列出任务计划ide
crontab -l
• 任务计划的文件位置(备份任务计划时,可直接拷贝该文件)工具
/var/spool/cron/username
• 指定用户centos7
crontab -u username
• 删除任务计划spa
crontab -r
chkconfig:centos6以及6之前版本所使用的服务管理工具
• 列出全部服务
[root@linux-5 ~]# chkconfig --list 注:该输出结果只显示 SysV 服务,并不包含 原生 systemd 服务。SysV 配置数据 可能被原生 systemd 配置覆盖。 要列出 systemd 服务,请执行 'systemctl list-unit-files'。 查看在具体 target 启用的服务请执行 'systemctl list-dependencies [target]'。 netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关 network 0:关 1:关 2:开 3:开 4:开 5:开 6:关
7个启动级别(centos6及之前):
0级别:关机
1级别: 单用户
2级别:多用户模式(不带nfs服务)
3级别: 多用户模式(不带图形)
4级别: 保留级别
5级别: 多用户(带有图形)
6级别: 重启
• 指定某一服务的某个级别开启或关闭
chkconfig --level 级别数 服务名称 on/off
• 指定某一服务的多个级别开启或关闭
chkconfig --level 345 服务名称 on/off //345级别开启或关闭
• 添加某一服务到服务列表
chkconfig --add network
• 从服务列表删除某一服务
chkconfig --del network
注:自定义服务文件必须放置在/etc/init.d目录下,且文件内容有必定的限制
[root@linux-5 init.d]# vim !$ vim network #! /bin/bash # # network Bring up/down networking # # chkconfig: 2345 10 90 //不能丢 # description: Activates/Deactivates all network interfaces configured to \ //不能丢 # start at boot time. //不能丢 # ### BEGIN INIT INFO # Provides: $network # Should-Start: iptables ip6tables NetworkManager-wait-online NetworkManager $network-pre # Short-Description: Bring up/down networking # Description: Bring up/down networking ### END INIT INFO # Source function library.
systemd:centos7的服务管理机制
• 列出全部类型为service的units
systemctl list-units --all --type=service
注:--all会显示出inactive的service
• 让服务开机启动
[root@linux-5 ~]# systemctl enable crond.service //.service可加可不加 Created symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service.
注:让服务开机启动会使系统生成一个软连接/etc/systemd/system/multi-user.target.wants/crond.service,真正的配置文件目录位于/usr/lib/systemd/system/crond.service.
• 不让服务开机启动
[root@linux-5 ~]# systemctl disable crond Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.
注:不让服务开机启动,会将软连接清除
• 查看状态
systemctl status crond
• 中止服务
systemctl stop crond
• 启动服务
systemctl start crond
• 重启服务
systemctl restart crond
• 检查服务是否开机启动
[root@linux-5 ~]# systemctl is-enabled crond enabled
• unit的类型
ls /usr/lib/systemd/system //系统全部unit
service 系统服务
target 多个unit组成的组
device 硬件设备
mount 文件系统挂载点
automount 自动挂载点
path 文件或路径
scope 不是由systemd启动的外部进程
slice 进程组
snapshot systemd快照
socket 进程间通讯套接字
swap swap文件
timer 定时器
• 列出正在运行的unit
systemctl list-units
• 列出全部,包括失败的或者inactive的unit
systemctl list-units --all
• •列出inactive的unit
systemctl list-units --all --state=inactive
• 列出状态为active的service
systemctl list-units --type=service
• 查看某个服务是否为active/enable
systemctl is-active/enable 服务名称
target:系统为了方便管理用target来管理unit,一个service属于unit的一种类型,多个unit组成了一个target。
• 列出unit文件中的target文件
systemctl list-unit-files --type=target
• 查看指定target下面有哪些unit
systemctl list-dependencies 服务名称.target
• 查看系统默认的target
systemctl get-default
• 修改系统默认的target(可修改系统的启动等级)
systemctl set-default multi-user.target
注:修改完成后一样会生成软连接文件
• 查看service属于哪个target
cat /usr/lib/systemd/system/服务名称.service //看[install]部分