Ubuntu 将程序注册为 service 并开机自启#


  1. 在 /etc/systemd/system/ 新建一个 [app].service 文件,填入下面内容
INI
[Unit]Description=[APP DESCRIPTION]After=network.target [Service]Type=forking                                 (以子进程方式启动, 可不写)ExecStart=带绝对路径的启动程序 参数(可选)     (就和你正常启动这个程序一样,只不过需要绝对路径)Restart=always                               (启用自动重启)RestartSec=3                                 (重启等待时间为3秒) [Install]   WantedBy=multi-user.target   (运行级别)Alias=[APP-alias].service   (服务别名)

配置项解析:

  1. 启用服务
Bash
systemctl enable [app].service (文件名)
  1. 启动服务
Bash
systemctl start [app].service

这里也可以直接重启系统, 等系统重新启动之后,如果配置正确,你的服务应该已经自动启动了。

  1. 查看服务是否成功启动
Bash
systemctl status [app].service
  1. 重新加载 service 文件 (比如你要更改服务别名之类的需要用到)
Bash
systemctl daemon-reload

分类

Published at:
October 20, 2026
Keywords:
service
Ubuntu