linux开机启动程序的几种方法

方法一:最简单的方法,通过crontab -e
@reboot 脚本路径即可

方法二:编辑/etc/rc.local文件

  1. 打开/etc/rc.local文件:sudo vim /etc/rc.local
  2. 在文件的末尾添加要启动的进程命令,例如:/path/to/your/process。请将/path/to/your/process替换为要启动的进程的实际路径。
  3. 保存文件并退出编辑器。
  4. 授予/etc/rc.local文件可执行权限:sudo chmod +x /etc/rc.local

现在,每次系统启动时,/etc/rc.local文件中添加的命令都会自动运行,进程也会自动启动。

方法三:注册为系统服务

  1. 打开终端并以root用户身份运行命令systemctl enable 服务名称,例如:systemctl enable my_service。这将把服务添加到开机启动项并设置为自动启动。
  2. 输入命令systemctl start 服务名称来启动服务,例如:systemctl start my_service。
  3. 如果要停止服务,可以使用命令systemctl stop 服务名称,例如:systemctl stop my_service。
  4. 如果要查看服务的状态,可以使用命令systemctl status 服务名称,例如:systemctl status my_service。

发表评论