chkconfig
1、任务计划
cron 是一个 linux 下的定时执行工具(相当于 windows 下的 scheduled task),可以在无需人工干预的情况下定时地运行任务 task。cron 服务提供 crontab 命令来设定 cron 服务的。
▎常用参数:
命令 | 作用 |
crontab -u | 设定某个用户的 cron 服务,一般 root 用户在执行这个命令的时候需要此参数 |
crontab -l | 列出某个用户 cron 服务的详细内容 |
crontab -r | 删除某个用户的 cron 服务 |
crontab -e | 编辑某个用户的 cron 服务 |
▎crontab 格式:
- [[email protected] ~]# 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
格式:分 时 日 月 周 user command
每个用户的任务计划文件存在 / var/spool/cron / 目录下同名的文件里
分范围 0-59,时范围 0-23,日范围 0-31,月范围 0-12,周 0-6
可用格式 1-5 表示一个范围 1 到 5
可用格式 1,2,3 表示 1 或者 2 或者 3
可用格式 */2 表示被 2 整除的数字,比如小时,就指每隔 2 小时
要保证服务是启动状态:
①systemctl start crond 启动服务
②systemctl status crond.service 查看状态
测试示例:
- [[email protected] ~]# crontab - e 0 5 1 - 15 * /2 1,3,5 /usr / local / sbin / 1.sh ##0分,5点,1 - 15日,双数月份,周一,周三,周五 执行脚本1.sh[[email protected] ~]# crontab - l 0 5 1 - 15 * /2 1,3,5 /usr / local / sbin / 1.sh[[email protected] ~]# crontab - u juispan - l 0 1 1 - 5 * * / tmp / 123.py[[email protected] ~]# crontab - e0 5 1 - 15 * /2 1,3,5 /usr / local / sbin / 1.sh ##0分,5点,1 - 15日,双数月份,周一,周三,周五 执行脚本1.sh[[email protected] ~]# crontab - l 0 5 1 - 15 * /2 1,3,5 /usr / local / sbin / 1.sh[[email protected] ~]# crontab - u juispan - l 0 1 1 - 5 * * / tmp / 123.py[[email protected] ~]# crontab - r[[email protected] ~]# crontab - l no crontab
- for root[[email protected] ~]# systemctl start crond[[email protected] ~]# systemctl status crond.service● crond.service - Command Scheduler Loaded: loaded (/usr/lib / systemd / system / crond.service; enabled; vendor preset: enabled) Active: active (running) since 五 2017 - 07 - 14 22 : 37 : 31 CST; 6h ago Main PID: 495 (crond) CGroup: / system.slice / crond.service └─495 / usr / sbin / crond - n
- 7月 14 22 : 37 : 31 juispan systemd[1] : Started Command Scheduler.7月 14 22 : 37 : 31 juispan systemd[1] : Starting Command Scheduler...7月 14 22 : 37 : 31 juispan crond[495] : (CRON) INFO (RANDOM_DELAY will be scal....) 7月 14 22 : 37 : 33 juispan crond[495] : (CRON) INFO (running with inotify support) Hint: Some lines were ellipsized,
- use - l to show in full.
2、开机启动
1)chkconfig
chkconfig 是 CentOS 7 以前用的系统服务管理工具。作为过渡,CentOS 7 中还存在着。
- [[email protected] ~]# ls / etc / init.d ##存放服务的目录functions netconsole network README[[email protected] ~]# 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 : 关 [[email protected] ~]# chkconfig --level 3 network off ##关闭3模式下的network服务 [[email protected] ~]# 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 : 关 [[email protected] ~]# chkconfig --level 345 network off ##关闭345模式下的network服务 [[email protected] ~]# 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 : 关 [[email protected] ~]# chkconfig --del network ##删除network服务 [[email protected] ~]# chkconfig --list
- 注意:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生 systemd 配置覆盖。 如果您想列出 systemd 服务,
- 请执行 'systemctl list - unit - files'。 欲查看对特定 target 启用的服务请执行 'systemctl list - dependencies [target]'。
- netconsole 0 : 关 1 : 关 2 : 关 3 : 关 4 : 关 5 : 关 6 : 关 [[email protected] ~]# chkconfig --add network ##增加network服务 [[email protected] ~]# 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 : 关 [[email protected] ~]# chkconfig network off ##关闭network服务 [[email protected] ~]# 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 : 关
2)systemd
systemctl 是 systemd 下的一个工具。该命令是用来替代 service 和 chkconfig 两个命令的。在 systemd 的管理体系里面,以前的运行级别(runlevel)的概念被新的运行目标(target)所取代。tartget 的命名类似于 multi-user.target 等这种形式,比如原来的运行级别 3(runlevel3)就对应新的多用户目标(multi-user.target),run level 5 就相当于 graphical.target。
▎systemd:
- [[email protected] ~]# systemctl enable crond.service ##让服务开机启动 [[email protected] ~]# systemctl status crond ##查看状态● crond.service - Command Scheduler Loaded: loaded (/usr/lib / systemd / system / crond.service; enabled; vendor preset: enabled) Active: active (running) since 五 2017 - 07 - 14 22 : 37 : 31 CST; 7h ago Main PID: 495 (crond) CGroup: / system.slice / crond.service └─495 / usr / sbin / crond - n
- 7月 14 22 : 37 : 31 juispan systemd[1] : Started Command Scheduler.7月 14 22 : 37 : 31 juispan systemd[1] : Starting Command Scheduler...7月 14 22 : 37 : 31 juispan crond[495] : (CRON) INFO (RANDOM_DELAY will be scal....) 7月 14 22 : 37 : 33 juispan crond[495] : (CRON) INFO (running with inotify support) Hint: Some lines were ellipsized,
- use - l to show in full. [[email protected] ~]# systemctl disable crond ##不让开机启动 [[email protected] ~]# systemctl status crond● crond.service - Command Scheduler Loaded: loaded (/usr/lib / systemd / system / crond.service; disabled; vendor preset: enabled) Active: active (running) since 五 2017 - 07 - 14 22 : 37 : 31 CST; 7h ago Main PID: 495 (crond) CGroup: / system.slice / crond.service └─495 / usr / sbin / crond - n
- 7月 14 22 : 37 : 31 juispan systemd[1] : Started Command Scheduler.7月 14 22 : 37 : 31 juispan systemd[1] : Starting Command Scheduler...7月 14 22 : 37 : 31 juispan crond[495] : (CRON) INFO (RANDOM_DELAY will be scal....) 7月 14 22 : 37 : 33 juispan crond[495] : (CRON) INFO (running with inotify support) Hint: Some lines were ellipsized,
- use - l to show in full. [[email protected] ~]# systemctl stop crond ##停止服务 [[email protected] ~]# systemctl status crond● crond.service - Command Scheduler Loaded: loaded (/usr/lib / systemd / system / crond.service; disabled; vendor preset: enabled) Active: inactive (dead)
- 7月 14 22 : 37 : 31 juispan systemd[1] : Started Command Scheduler.7月 14 22 : 37 : 31 juispan systemd[1] : Starting Command Scheduler...7月 14 22 : 37 : 31 juispan crond[495] : (CRON) INFO (RANDOM_DELAY will be scal....) 7月 14 22 : 37 : 33 juispan crond[495] : (CRON) INFO (running with inotify support) 7月 15 06 : 01 : 11 juispan systemd[1] : Stopping Command Scheduler...7月 15 06 : 01 : 11 juispan systemd[1] : Stopped Command Scheduler.Hint: Some lines were ellipsized,
- use - l to show in full. [[email protected] ~]# systemctl start crond ##启动服务 [[email protected] ~]# systemctl restart crond ##重启服务 [[email protected] ~]# systemctl status crond● crond.service - Command Scheduler Loaded: loaded (/usr/lib / systemd / system / crond.service; disabled; vendor preset: enabled) Active: active (running) since 六 2017 - 07 - 15 06 : 02 : 14 CST; 7s ago Main PID: 5962 (crond) CGroup: / system.slice / crond.service └─5962 / usr / sbin / crond - n
- 7月 15 06 : 02 : 14 juispan systemd[1] : Started Command Scheduler.7月 15 06 : 02 : 14 juispan systemd[1] : Starting Command Scheduler...7月 15 06 : 02 : 14 juispan crond[5962] : (CRON) INFO (RANDOM_DELAY will be sca....) 7月 15 06 : 02 : 14 juispan crond[5962] : (CRON) INFO (running with inotify support) 7月 15 06 : 02 : 14 juispan crond[5962] : (CRON) INFO (@reboot jobs will be run....) Hint: Some lines were ellipsized,
- use - l to show in full. [[email protected] ~]# systemctl is - enabled crond ##检查服务是否开机启动disabled
▎unit:
一个 service 属于一种类型的 unit。
- [[email protected] ~]# systemctl list - units ##列出正在运行的unit
- [[email protected] ~]# systemctl list - units --all ##列出所有,包括失败的或者inactive的
- [[email protected] ~]# systemctl list - units --all --state = inactive ##列出inactive的unit
- [[email protected] ~]# systemctl list - units --type = service ##列出状态为active的service
- [[email protected] ~]# systemctl is - active crond.service ##查看某个服务是否为active active
▎target:
多个 unit 组成了一个 target,一个 target 里面包含了多个 service。系统为了方便管理用 target 来管理 unit。
- [[email protected] ~]# systemctl list - unit - files --type = target UNIT FILE STATE basic.target static bluetooth.target static cryptsetup - pre.target static...... [[email protected] ~]# systemctl list - dependencies multi - user.target multi - user.target ##查看指定target下有哪些unit● ├─auditd.service● ├─brandbot.path...... [[email protected] ~]# systemctl set -
- default multi - user.target Removed symlink / etc / systemd / system /
- default.target.Created symlink from / etc / systemd / system /
- default.target to / usr / lib / systemd / system / multi - user.target. [[email protected] ~]# systemctl get -
- default ##查看系统默认的target multi - user.target[[email protected] ~]# cat / usr / lib / systemd / system / sshd.service[Unit] Description = OpenSSH server daemon Documentation = man: sshd(8) man: sshd_config(5) After = network.target sshd - keygen.service Wants = sshd - keygen.service
- [Service] Type = forking PIDFile = /var/run / sshd.pid EnvironmentFile = /etc/sysconfig / sshd ExecStart = /usr/sbin / sshd $OPTIONS ExecReload = /bin/kill - HUP $MAINPID KillMode = process Restart = on - failure RestartSec = 42s
- [Install] WantedBy = multi - user.target
来源: http://www.bubuko.com/infodetail-2216018.html