CentOS 8 启动时自动执行命令

CentOS 8 和 CentOS7 有点不同,如果你要找CentOS 7 的话,请看这文章

1. 编辑/etc/rc.d/rc.local文件

nano /etc/rc.d/rc.local

2. 在rc.local 写入你要运行的脚本,以下这个是自动git pull的案例

sh -c 'cd /var/www/html/dir && git pull'

3. 加入可执行属性

chmod +x /etc/rc.d/rc.local

4. 配置rc.local服务

nano /usr/lib/systemd/system/rc-local.service

内容如下:

[Unit]
Description=/etc/rc.d/rc.local Compatibility
Documentation=man:systemd-rc-local-generator(8)
ConditionFileIsExecutable=/etc/rc.d/rc.local
After=network.target

[Service]
Type=forking
ExecStart=/etc/rc.d/rc.local start
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no

[Install]
WantedBy=multi-user.target 

5. 启动

systemctl daemon-reload
systemctl start rc-local
systemctl enable rc-local

6. 做完对rc.local的配置后重启机器

reboot

7. 重启服务器了,查看是否有active

systemctl status rc-local

Loading

Facebook评论