35 lines
978 B
Markdown
35 lines
978 B
Markdown
|
**1 添加 systemd 配置文件:**
|
|||
|
`vim /usr/lib/systemd/system/myservice.service`
|
|||
|
文件内容如下:
|
|||
|
```
|
|||
|
[Unit]
|
|||
|
Description=My Test App
|
|||
|
After=syslog.target
|
|||
|
|
|||
|
[Service]
|
|||
|
ExecStart=/opt/test/test.sh
|
|||
|
Restart=on-failure
|
|||
|
RestartSec=5
|
|||
|
|
|||
|
[Install]
|
|||
|
WantedBy=multi-user.target
|
|||
|
```
|
|||
|
>ExecStart表示服务执行的命令,可以直接是shell命令也可以是shell脚本,这里以shell脚本为例
|
|||
|
>Restart 如果设置为on-failure,则当进程以非零退出代码退出,由信号终止,当操作(例如服务重新加载)超时,以及何时触发配置的监视程序超时时,将重新启动服务。
|
|||
|
>RestartSec 等待5秒然后启动服务。
|
|||
|
|
|||
|
**2. 设置开机启动**
|
|||
|
```shell
|
|||
|
# 刷新服务列表
|
|||
|
systemctl daemon-reload
|
|||
|
# 设置开机自启
|
|||
|
systemctl enable myservice
|
|||
|
# 设置开机关闭
|
|||
|
systemctl disable myservice
|
|||
|
# 启动服务
|
|||
|
systemctl start myservice
|
|||
|
# 关闭服务
|
|||
|
systemctl stop myservice
|
|||
|
# 查看服务状态
|
|||
|
systemctl status myservice
|
|||
|
```
|