Linux 添加开机启动方法(服务/脚本)
    		       		warning:
    		            这篇文章距离上次修改已过442天,其中的内容可能已经有所变动。
    		        
        		                
                在Linux中添加开机启动项通常有以下几种方法:
- 使用
systemd服务(推荐用于现代系统): 
首先,创建一个新的systemd服务单元文件,例如/etc/systemd/system/my_service.service:
[Unit]
Description=My custom startup service
 
[Service]
ExecStart=/path/to/your/script.sh
 
[Install]
WantedBy=multi-user.target然后,启用并启动服务:
sudo systemctl enable my_service.service
sudo systemctl start my_service.service- 使用
crontab的@reboot: 
使用crontab -e命令编辑当前用户的计划任务,然后添加一行:
@reboot /path/to/your/script.sh保存并退出编辑器,这样脚本就会在重启后运行。
- 使用
rc.local(对于旧的Debian或基于Debian的系统): 
编辑/etc/rc.local文件,在exit 0语句之前添加你的脚本路径:
/path/to/your/script.sh
exit 0确保script.sh是可执行的,可以通过chmod +x /path/to/your/script.sh来设置。
请根据你的Linux发行版和需求选择合适的方法。
评论已关闭