定时与自启任务

定时执行任务

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值