一: crontab 命令
-u: 指定某个用户, 不加 - u 则为当前用户.
-e: 指定计划任务
-l: 列出计划任务
-r: 删除计划任务
使用 crontab -e 来进行编写任务计划, 实际上是使用 vim 工具打开了 crontab 的配置文件 / var/spool/cron/username, 如果是 root, 打开的就是 / var/spool/cron/root, 但是千万不能直接去编辑这个文件, 可能会出错.
01 10 05 06 3 echo "ok"> /root/cron.log
从左到右分别为分, 时, 日, 月, 周, 命令行.
每天凌晨 1 点 20 分 20 01
每周日 3 点 00 03 7
每月 14 号 4 点 10 分 10 04 14
每隔 8 小时 00 /8
每天 1 点, 12 点, 18 点 00 01,12,18
每天 9 到 18 点 00 9-18 *
设置好计划任务需要查看一下 crond 是否启动
systemctl status crond
如果是停止状态, 则需要启动他
systemctl start crond
二: 服务管理 chkconfig
chkconfig 服务管理工具
linux 系统所有的预设服务可以查看 / etc/init.d / 目录得到
- ls /etc/init.d/
- chkconfig --list
每个服务都有 6 个级别, 0,1,6 运行级别被系统保留, 0 作为 shutdown 的动作, 1 作为重启至单用户模式, 6 为重启.
一般只使用 2,3,4,5 几个, 2 表示无 NFS 支持的多用户模式, 3 表示完全多用户模式 (最常用的),4 保留给用户自定义, 5 表示图形登陆方式.
更改某个级别下是否开启 chkconfig --level 3 crond off
--level 指定级别, 后面你是服务名, 然后是 off 或者 on,--level 后还可以跟多个级别
chkconfig --level 345 crond off
若不指定级别, 则为对 2345 全部操作.
chkconfig 还可以把某个服务加入到系统服务.
chkconfig --add crond
删除 chkconfig --del crond
可以用来把自定义的启动脚本加入到系统服务中.
三: systemd 命令
systemctl list-units --all --type=service
几个常用的服务相关的命令
- systemctl enable crond.service #让服务开机启动
- systemctl disable crond #不让开机启动
- systemctl status crond #查看状态
- systemctl stop crond #停止服务
- systemctl restart crond #重启服务
- systemctl is-enabled crond #检查服务是否开机启动
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 #列出正在运行的 unit
- systemctl list-units --all #列出所有, 包括失败的或者 inactive 的
- systemctl list-units --all --state=inactive #列出 inactive 的 unit
- systemctl list-units --type=service #列出状态为 active 的 service
- systemctl is-active crond.service #查看某个服务是否为 active
用 target 来管理 unit
- systemctl list-unit-files --type=target
- systemctl list-dependencies multi-user.target #查看指定 target 下面有 哪些 unit
- systemctl get-default #查看系统默认的 target
- systemctl set-default multi-user.target
一个 service 属于一种类型的 unit
多个 unit 组成了一个 target
一个 target 里面包含了多个 service
cat /usr/lib/systemd/system/sshd.service #看 install 部分
来源: http://www.bubuko.com/infodetail-2689472.html