linux中实现开机自启服务

1. /etc/rc.local/etc/rc.local 文件中设置

系统启动后会执行/etc/rc.d/rc.local,而/etc/rc.local/etc/rc.d/rc.local的软连接.

vim /etc/rc.local  
bash /root/script/start.sh

或
vim /etc/rc.d/rc.local 
bash /root/script/start.sh

2./etc/rc.d/rc3.d/ 目录添加启动脚本

cd /etc/rc.d/rc3.d/
cat  Sstart.sh
/usr/sbin/nginx

Sstart.sh脚本必须要S开头,否则不会被执行.

3.通过 Crontab 实现

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

crontab -e
@reboot /root/script/start.sh

这个脚本就可以在重启的时候自动执行了。

在启动 5 分钟后运行指定脚本

crontab -e
@reboot sleep 300 && /root/script/start.sh

4.通过 Systemd 实现

编写一个名为 restart 的 Systemd 服务

vim /lib/systemd/system/restart.service  
[Unit]  
Description=restart  
After=default.target  
[Service]  
ExecStart=/root/script/start.sh
[Install]  
WantedBy=default.target 

然后启用这个 Systemd 服务

systemctl daemon-reload  
systemctl enable restart.service 
posted @   aongao  阅读(234)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示