Press "Enter" to skip to content

centos7 系统服务启动程序 开机启动 systemd.service

之前自己机器上有很多程序,希望开机启动,就搞了个启动脚本,然后加入到/etc/rc.local里面。查资料发现,这已经过世了,应该将服务加入到/etc/systemd/system里的服务。

frp的默认安装包会提供一个systemd的目录,里面放的就是类似服务配置文件,可以借鉴

例如:/frp_0.33.0_linux_amd64/systemd/frpc.service

[Unit]
Description=Frp Client Service
After=network.target

[Service]
Type=simple
User=nobody
Restart=on-failure
RestartSec=5s
ExecStart=/usr/bin/frpc -c /etc/frp/frpc.ini
ExecReload=/usr/bin/frpc reload -c /etc/frp/frpc.ini

[Install]
WantedBy=multi-user.target

ExecStart 是启动脚本

ExecReload 是重启脚本,这个是可选的,如果你不配,他默认帮你实现了个杀进程,重新启动的脚本。

User 是你启动的用户,如果你脚本需求,可以配置成root。

然后就把写好的脚本放到 /etc/systemd/system/ 这个目录下面,文件名为 xxxx.service。

然后就是熟悉的启动命令 systemctl start xxxx , 重启就是 systemctl restart xxxx ,看状态 system status xxxx。

开机启动就是 systemctl enable xxxx。 这个xxxx就是你取名字的占位符。

这里只是简单使用,如果想深入可以参考更多的文档来学习。

https://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-part-two.html

https://www.freedesktop.org/software/systemd/man/systemd.service.html

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注