learning_record_doc/ubuntu/开机自启动.md
2022-02-28 23:07:00 +08:00

35 lines
978 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

**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
```