定时执行任务
vi /etc/crontab
30 18 * * * root /sbin/shutdown #每天18.30关机
*/3 * * * * root /sbin/nice #每隔3分钟执行命令nice
systemctl restart cron.service #重启服务
ansible all -m lineinfile -a "path=/etc/crontab line='
30 18 * * * root /sbin/shutdown'" #ansible批量定时
开机自启任务
1、建立rc-local.service文件
sudo vi /etc/systemd/system/rc-local.service
2、将下列内容复制进rc-local.service文件
[Unit]
Description=/etc/rc.local Compatibility
Documentation=man:systemd-rc-local-generator(8)
ConditionFileIsExecutable=/etc/rc.local
After=network.target
[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no
[Install]
WantedBy=multi-user.target
Alias=rc-local.service
内容解释:
启动文件主要分成三部分
[Unit] 段: 启动顺序与依赖关系;
[Service] 段: 启动行为,如何启动,启动类型;
[Install] 段: 定义如何安装这个配置文件,即怎样做到开机启动;
3、创建文件rc.local
sudo vi /etc/rc.local
4、将下列内容复制进rc.local文件
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# 要执行的命令或脚本
#
#
exit 0
创建软链接
systemd 默认读取 /etc/systemd/system 下的配置文件, 所以还需要在 /etc/systemd/system 目录下创建软链接
ln -s /lib/systemd/system/rc.local.service /etc/systemd/system/
5、给rc.local加上权限
sudo chmod +x /etc/rc.local
6、启用服务
sudo systemctl enable rc-local