荣耀之链论坛

 找回密码
 立即注册
搜索
查看: 523|回复: 3

Linux——systemctl 添加自定义服务

[复制链接]

1326

主题

2373

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
10267
发表于 2023-6-9 09:56 | 显示全部楼层 |阅读模式
https://blog.csdn.net/blueicex2017/article/details/104207946

systemctl是RHEL 7 的服务管理工具中主要的工具,它融合之前service和chkconfig的功能于一体。可以使用它永久性或只在当前会话中启用/禁用服务。

一、server文件位置
systemd有系统和用户区分;系统(/user/lib/systemd/system/)、用户(/etc/lib/systemd/user/),一般系统管理员手工创建的单元文件建议存放在/etc/systemd/system/目录下面。

二、服务文件示例
[root@master ~]# man systemd.service
man systemd.unit
1
2
1.示例1
nginx.service

[root@master ~]# vim /user/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2.示例2
httpd.service

[root@master ~]# vim /usr/lib/systemd/system/httpd.service
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:/usr/local/httpd24/sbin/httpd(8)
Documentation=man:/usr/local/httpd24/sbin/apachectl(8)
[Service]
Type=forking
EnvironmentFile=/usr/local/httpd24/etc/sysconfig/httpd
ExecStart=/usr/local/httpd24/sbin/httpd $OPTIONS -k start
ExecReload=/usr/local/httpd24/sbin/httpd $OPTIONS -k graceful
ExecStop=/usr/local/httpd24/sbin/httpd $OPTIONS -k stop
KillSignal=SIGCONT
PrivateTmp=true
[Install]
WantedBy=multi-user.target

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
3.示例3
在centos7上使用最简单的方法把php脚本做成服务,随开机启动运行

3.1 服务文件
[root@master ~]# vim customservice.service
# copy to /usr/lib/systemd/system
# systemctl enable customservice.service
[Unit]
Description=customservice Service

[Service]
Type=forking
User=root
ExecStart=/etc/init.d/customservice.sh

[Install]
WantedBy=multi-user.target
1
2
3
4
5
6
7
8
9
10
11
12
13
3.2 脚本文件
[root@master ~]#vim customservice.sh
#!/bin/bash
nohup /usr/bin/php /myproject/customservice.php >> /myproject/logs/customservice.log 2>&1 &
1
2
3
3.3 拷贝文件
[root@master ~]# cp customservice.sh /etc/init.d
1
三、服务文件描述
1.[Unit]
Description : 服务的简单描述
Documentation : 服务文档
Before、After:定义启动顺序。Before=xxx.service,代表本服务在xxx.service启动之前启动。After=xxx.service,代表本服务在xxx.service之后启动。
Requires:这个单元启动了,它需要的单元也会被启动;它需要的单元被停止了,这个单元也停止了。
Wants:推荐使用。这个单元启动了,它需要的单元也会被启动;它需要的单元被停止了,对本单元没有影响。

2.[Service]
Type=simple(默认值):systemd认为该服务将立即启动。服务进程不会fork。如果该服务要启动其他服务,不要使用此类型启动,除非该服务是socket激活型。
Type=forking:systemd认为当该服务进程fork,且父进程退出后服务启动成功。对于常规的守护进程(daemon),除非你确定此启动方式无法满足需求,使用此类型启动即可。使用此启动类型应同时指定 PIDFile=,以便systemd能够跟踪服务的主进程。
Type=oneshot:这一选项适用于只执行一项任务、随后立即退出的服务。可能需要同时设置 RemainAfterExit=yes 使得 systemd 在服务进程退出之后仍然认为服务处于激活状态。
Type=notify:与 Type=simple 相同,但约定服务会在就绪后向 systemd 发送一个信号。这一通知的实现由 libsystemd-daemon.so 提供。
Type=dbus:若以此方式启动,当指定的 BusName 出现在DBus系统总线上时,systemd认为服务就绪。
Type=idle: systemd会等待所有任务(Jobs)处理完成后,才开始执行idle类型的单元。除此之外,其他行为和Type=simple 类似。
PIDFile:pid文件路径
ExecStart:指定启动单元的命令或者脚本,ExecStartPre和ExecStartPost节指定在ExecStart之前或者之后用户自定义执行的脚本。Type=oneshot允许指定多个希望顺序执行的用户自定义命令。
ExecReload:指定单元停止时执行的命令或者脚本。
ExecStop:指定单元停止时执行的命令或者脚本。
PrivateTmp:True表示给服务分配独立的临时空间
Restart:这个选项如果被允许,服务重启的时候进程会退出,会通过systemctl命令执行清除并重启的操作。
RemainAfterExit:如果设置这个选择为真,服务会被认为是在激活状态,即使所以的进程已经退出,默认的为假,这个选项只有在Type=oneshot时需要被配置。

3.[Install]
Alias:为单元提供一个空间分离的附加名字。
RequiredBy:单元被允许运行需要的一系列依赖单元,RequiredBy列表从Require获得依赖信息。
WantBy:单元被允许运行需要的弱依赖性单元,Wantby从Want列表获得依赖信息。
Also:指出和单元一起安装或者被协助的单元。
DefaultInstance:实例单元的限制,这个选项指定如果单元被允许运行默认的实例。

四、相关命令
[root@master ~]# systemctl is
is-active          is-enabled         is-failed          isolate            is-system-running
[root@master ~]# systemctl st
start   status  stop   
1
2
3
4
重载服务
journalctl -f 服务名
刚刚配置的服务需要让systemctl能识别,就必须刷新配置
systemctl daemon-reload
查看已启动的服务列表:systemctl list-unit-files|grep enabled
查看启动失败的服务列表:systemctl --failed

————Blueicex 2020/2/7 12:28 blueice1980@126.com
————————————————
版权声明:本文为CSDN博主「blueicex2020」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/blueicex2017/article/details/104207946
回复

使用道具 举报

1326

主题

2373

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
10267
 楼主| 发表于 2023-6-9 09:57 | 显示全部楼层
原来添加启动脚本这么方便
怪不得centos8开始就不用rc.local了

  1. 在服务器上,我们可以使用 systemd 开启 WireGuard 服务,重启后就不用再配置了。
  2. sudo systemctl enable wg-quick@wg0.service
  3. 甚至还可以写成这样 直接写命令  用@代替空格
复制代码

回复 支持 反对

使用道具 举报

1326

主题

2373

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
10267
 楼主| 发表于 2023-6-9 10:03 | 显示全部楼层
回复 支持 反对

使用道具 举报

1326

主题

2373

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
10267
 楼主| 发表于 2023-6-15 11:16 | 显示全部楼层
cd /usr/lib/systemd/system
vi iptables_fenliu.service

[Unit]
Description=iptables_fenliu
After=iptables_fenliu.service

[Service]
#User=root
#Group=root
ExecStart=/root/ssht/ssht.sh

[Install]
WantedBy=multi-user.target



chmod 754 iptables_fenliu.service

systemctl enable iptables_fenliu.service
systemctl disable iptables_fenliu.service

实测失败,并没有执行脚本

回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

荣耀之链

GMT+8, 2025-6-18 05:13 , Processed in 0.012487 second(s), 20 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表