【9.29】日常运维——系统工具
10.23 linux 任务计划 cron
- crontab 配置文件
[root@arslinux-01 ~]# cat /etc/crontab
SHELL=/bin/bash
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
任务计划配置文件,定义几个环境变量
格式: 分钟 小时 日期 月份 周 用户 命令
- crontab 常用命令选项:
选项 | 含义 |
---|---|
-e | 编辑 crontab 配置文件 |
-l | 列出任务计划 |
-r | 删除任务计划 |
-u | 查看指定用户的任务计划 |
- crontab -e 编辑 crontab 配置文件
[root@arslinux-01 ~]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
0 3 * * * /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log
~
范围1-10号 0 3 1-10 * *
范围每两个月 0 3 1-10 */2
范围周二和周五 0 3 1-10 * 2,5
- systemctl start crond 启动crond服务
[root@arslinux-01 ~]# systemctl start crond
ps aux | grep crond 检查是否启动
systemctl status crond 查看 crond 是否启动或者是否是绿色运行
[root@arslinux-01 ~]# ps aux |grep crond
root 6235 0.0 0.1 126316 1672 ? Ss 19:05 0:00 /usr/sbin/crond -n
root 8389 0.0 0.0 112724 988 pts/0 S+ 22:31 0:00 grep --color=auto crond
[root@arslinux-01 ~]# systemctl status crond
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
Active: active (running) since 三 2019-04-17 19:05:38 CST; 3h 26min ago
Main PID: 6235 (crond)
CGroup: /system.slice/crond.service
└─6235 /usr/sbin/crond -n
4月 17 19:05:38 arslinux-01 systemd[1]: Started Command Scheduler.
4月 17 19:05:38 arslinux-01 crond[6235]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 34% if used.)
4月 17 19:05:38 arslinux-01 crond[6235]: (CRON) INFO (running with inotify support)
4月 17 22:30:01 arslinux-01 crond[6235]: (root) RELOAD (/var/spool/cron/root)
如果任务计划放到配置文件中,但是没有执行,很有可能是脚本用的是一个命令,而不是绝对路径
有两种解决方法:
1、命令写绝对路径;
2、把命令的路径加入到配置文件中的PATH变量中
建议: 每写一个任务计划都要写上追加日志,正确和错误输出
- crontab -l 列出任务计划
[root@arslinux-01 ~]# crontab -e
crontab: installing new crontab
1 10 * 2 * /usr/bin/find /tmp/ -type f -mtime +100 |xargs rm -f
[root@arslinux-01 ~]# crontab -l
1 10 * 2 * /usr/bin/find /tmp/ -type f -mtime +100 |xargs rm -f
-
任务计划会保存在 /var/spool/cron/ 下对应用户名的文件中,备份改文件就可以备份任务计划
-
如果要备份计划任务,可以直接拷贝 /var/spool/cron 下对应文件
-
crontab -r 删除任务计划
[root@arslinux-01 ~]# crontab -r
[root@arslinux-01 ~]# crontab -r
no crontab for root
- crontab -u 查看指定用户的任务计划
[root@arslinux-01 ~]# crontab -u root -l
no crontab for root
参考:http://man.linuxde.net/
10.24 chkconfig 工具
- chkconfig 常用命令选项:
命令 | 含义 |
---|---|
chkconfig --list | 查看所有的服务 |
chkconfig 服务名 off/on | 关闭或打开指定服务 |
chkconfig --level 数字 服务名 on/off | 指定某个级别开启或关闭 |
chkconfig --level 多个数字 服务名 on/off | 指定多个级别的开启关闭(数字不加逗号)(数字 0 和 6 不可用) |
chkconfig --add 服务名 | 自定义服务并加入服务列表 |
chkconfig --del 服务名 | 删除自定义服务 |
- chkconfig --list 查看所有的服务
[root@arslinux-01 ~]# 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:关
- /etc/init.d/ 服务脚本所在位置
[root@arslinux-01 ~]# ls /etc/init.d/
functions netconsole network README
- chkconfig 服务名 off 关闭指定服务
[root@arslinux-01 ~]# chkconfig --list
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 'systemctl list-unit-files'。
查看在具体 target 启用的服务请执行