linux 添加服务到systemctl

linux 添加服务到systemctl

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
#文件参数说明:
#[Unit]:服务的说明
#Description:描述服务

#After:描述服务类别
#[Service]服务运行参数的设置
#Type=forking是后台运行的形式
    Type:定义启动时的进程行为。它有以下几种值。
    Type=simple:默认值,执行ExecStart指定的命令,启动主进程
    Type=forking:以 fork 方式从父进程创建子进程,创建后父进程会立即退出
    Type=oneshot:一次性进程,Systemd 会等当前服务退出,再继续往下执行
    Type=dbus:当前服务通过D-Bus启动
    Type=notify:当前服务启动完毕,会通知Systemd,再继续往下执行
    Type=idle:若有其他任务执行完毕,当前服务才会运行
#ExecStart为服务的具体运行命令
#ExecReload为重启命令
#ExecStop为停止命令
#PrivateTmp=True表示给服务分配独立的临时空间
#注意:启动、重启、停止命令全部要求使用绝对路径

案例二

[Unit]
Description=lovenet_blog

[Service]
Type=simple
ExecStart=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.262.b10-0.el8_2.x86_64/jre/bin/java -server -Xms256m -Xmx256m -jar /lczze/net/lovenet.jar
KillMode=/bin/kill -s QUIT $MAINPID
Restart=always
RestartSec=syslog
[Install]
WantedBy=multi-user.target

修改权限

chmod 755 nginx.service
现在systemctl可以使用nginx了:
systemctl stop/start/reload nginx

posted @ 2022-04-29 15:47  liwenchao1995  阅读(750)  评论(0编辑  收藏  举报