zl程序教程

您现在的位置是:首页 >  系统

当前栏目

centos7 将服务添加到systemctl

centos7服务 添加 systemctl
2023-09-27 14:26:13 时间

centos7中提供了systemd服务,可以方便的管理各种服务

但是有些通过编译安装的服务systemd里面没有,我们只需要添加一下服务文件即可

以下用nginx作为例子,展示如何添加服务到systemd中

假设nginx安装在/usr/bin/nginx,配置文件在/usr/local/nginx/conf/nginx.conf

vim /usr/lib/systemd/system/nginx.service 

编辑该文件

[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop

[Install]
WantedBy=multi-user.target
[Unit]:服务的说明
Description:描述服务
After:描述服务类别

[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:启动、重启、停止命令全部要求使用绝对路径

[Install]服务安装的相关设置,可设置为多用户
chmod 754 nginx.service

修改权限,现在systemctl可以使用nginx了