7

几个开机重启执行脚本的方法

 3 years ago
source link: https://www.bboy.app/2021/07/12/%E5%87%A0%E4%B8%AA%E5%BC%80%E6%9C%BA%E9%87%8D%E5%90%AF%E6%89%A7%E8%A1%8C%E8%84%9A%E6%9C%AC%E7%9A%84%E6%96%B9%E6%B3%95/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

几个开机重启执行脚本的方法

发表于

2021-07-12 更新于 2021-07-13 分类于 linux

20210712155325.png

经常碰到机器断电之后需要重启一大堆服务,为了防止这种事情发生,设置开机自启的脚本十分的重要,我习惯性的做法就是设置一个重启脚本,然后使用crontab去调用他

crontab

crontab可以使用@reboot来执行主机启动之后的命令

crontab -e

@reboot /data/restart.sh

这样就可以在重启的时候执行restart.sh脚本了,这是我最喜欢的做法

systemd

编辑一个脚本比如

vim /lib/systemd/system/restart.service

[Unit]
Description=restart
After=default.target

[Service]
ExecStart=/data/restart.sh

[Install]
WantedBy=default.target

systemctl daemon-reload

systemctl enable restart.service

这样这个服务就可以开机自启了

rc.local

/etc/rc.local赋予可执行权限

chmod +x /etc/rc.local

之后在里面加入

bash /data/restart.sh

这个脚本就会开机自启了

欢迎关注我的博客www.bboy.app

Have Fun

欢迎关注我的其它发布渠道


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK