Linux添加服务到开机自动启动

Systemd 是 Linux 系统中最新的初始化系统,Systemd 服务文件以 .service 结尾。一些使用包管理工具安装的软件会自动建立 .service 服务文件,路径在 /lib/systemd/system/ 下,但自行建立及管理的文件建议放在 /etc/systemd/system/ 目录下。内容以 supervisor 为例:

[Unit]
Description=Supervisor process control system for UNIX
Documentation=http://supervisord.org
After=network.target

[Service]
ExecStart=/usr/bin/supervisord -n -c /etc/supervisor/supervisord.conf
ExecStop=/usr/bin/supervisorctl $OPTIONS shutdown
ExecReload=/usr/bin/supervisorctl -c /etc/supervisor/supervisord.conf $OPTIONS reload
KillMode=process
Restart=on-failure
RestartSec=50s

[Install]
WantedBy=multi-user.target

参数说明:

[Unit]
Description:描述服务
Documentation:参考资料
After:描述服务类别

[Service]
Type:是后台运行的形式
ExecStart:服务的具体运行命令
ExecReload:重启命令
ExecStop:停止命令
KillModedaemon终止时所关闭的程序
Restart:触发重启
RestartSec:重启等待时间
TimeoutSec:无法顺利启动强制关闭时间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径

[Install]
运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3

如过想要某些服务开机启动,例如以 php-fpm 为例,编写service:

sudo vi /etc/systemd/system/php7.1-fpm.service
[Unit]
Description=The PHP 7.1 FastCGI Process Manager
Documentation=man:php-fpm7.1(8)
After=network.target

[Service]
Type=notify
PIDFile=/run/php/php7.1-fpm.pid
ExecStart=/usr/sbin/php-fpm7.1 -R --nodaemonize --fpm-config /etc/php/7.1/fpm/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID

[Install]
WantedBy=multi-user.target

随后如果 php-fpm 在运行则先将其关闭,运行:

systemctl daemon-reload 
systemctl start php7.1-fpm.service

测试能够成功开启服务了,就可以将服务设置为开机启动:

systemctl enable php7.1-fpm.service

常用命令

systemctl daemon-reload
systemctl list-units --type=service
systemctl list-unit-files --type=service

systemctl start unit.service
systemctl stop unit.service
systemctl restart unit.service

systemctl enable unit.service
systemctl disable unit.service

systemctl is-enable unit.service
systemctl is-active unit.service
systemctl status unit.service
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值