服务介绍
systemctl是linux中的服务管理,是一个systemd的工具,主要负责控制systemd系统和服务管理器
在ubuntu 、centos等一系列发行版中可用。可以方便的管理需要启动的服务等。可以实现开机自启动、出错重启和定时重启等等功能。
常用命令
以firewalld为例
#启动
systemctl start firewalld.service
#停止
systemctl stop firewalld.service
#重启
systemctl restart firewalld.service
#查看状态
systemctl status firewalld.service
#开机自启
systemctl enable firewalld.service
#禁用开机自启
systemctl disable firewalld.service
#加载配置
systemctl daemon-reload
服务文件讲解
首先看一下服务模板-以华为云自带服务为例
[Unit]
#描述:重置密码服务
Description=cloudResetPwdAgent
#在syslog.target 服务启动后在启动
After=syslog.target
#在network-online.target服务启动后在启动
After=network-online.target
[Service]
#以fork() 子进程执行
Type=forking
#启动命令
ExecStart=/CloudrResetPwdAgent/bin/./cloudResetPwdAgent.script start sysd
#停止命令
ExecStop=/CloudrResetPwdAgent/bin/./cloudResetPwdAgent.script stop sysd
[Install]
#配置路径
WantedBy=multi-user.target
下面分别介绍一下
- Unit-定义启动的顺序以及依赖的关系
可选项 | 含义 |
---|---|
Description | 主要用来描述服务 |
After | 指定在什么服务启动后启动 |
Before | 指定在什么服务启动前启动 |
Documentation | 指定帮助文档位置 |
Wants | 可以指定服务依赖于哪些服务“弱依赖”,即使依赖服务故障也不会影响 |
Requires | 可以指定服务依赖于哪些服务“强依赖”,依赖服务故障会影响 自己 |
- Service-定义启动、关闭和重启服务等操作时的方式
可选项 | 含义 |
---|---|
EnvironmentFile | 用来指定当前服务启动的环境变量 以路径的形式在文件中用key=value 的形式写入 读取时可以使用$key来读取 |
ExecStart | 指定服务启动时执行的命令 |
ExecStop | 指定服务停止时执行的命令 |
ExecReload | 指定服务重启时执行的命令 |
ExecStartPre | 指定服务启动之前执行的命令 |
ExecStartPost | 指定服务启动之后执行的命令 |
ExecStopPost | 指定服务停止之后执行的命令 |
RestartSec | 指定服务在重启前等待的时间(单位为 “秒”) |
Type | 指定启动类型,Type又分为:(1)simple指定ExecStart字段的进程为主进程 (2)forking:指定以fork() 子进程执行ExecStart字段的进程 (3)oneshot:执行一次 (4) notify:启动后发送会发送通知信号通知systemd(5)idle:等其他任务结束后才运行 |
Restart | 指定重启类型,Restart又分为:(1)no:退出后不会重启(2):on-success:当进程正常退出时(退出码为0) 执行重启(3)on-failure:当进程不正常退出时(退出码不为0) 执行重启(4)on-abnormal:当被信号终止和超时执行重启(5)on-abort:当收到没有捕捉到的信号终止时执行重启(6):on-watchdog:当看门狗超时时执行重启(7):always:一直重启 |
KillMode | 指定停止类型,KillMode 又分为:(1)control-group:杀掉当前进程中所有的进程(2)process:杀掉当前进程的主进程(3):mixed:主进程将收到 SIGTERM 信号,子进程收到 SIGKILL 信号(4)none:不杀掉任何进程 |
- Install-定义安装配置文件(开机自启动必备)
可选项 | 含义 |
---|---|
WantedBy | 表示该服务所在的 Target(一般写 multi-user.target) |
自己制作system服务
以tomcat为例
- 首先安装jdk
yum -y install java-1.8.0-openjdk.x86_64
- 下载tomcat8安装包
wget https://dlcdn.apache.org/tomcat/tomcat-8/v8.5.78/bin/apache-tomcat-8.5.78.tar.gz
- 配置service文件
```bash
vim /etc/systemd/system/tomcat8.5.78.service
[Unit]
Description=tomcat8.5.78 service
After=network.target
[Service]
Type=forking
ExecStart=/home/apache-tomcat-8.5.78/bin/startup.sh
ExecStop=/home/apache-tomcat-8.5.78/bin/shutdown.sh
Restart=on-abort
[Install]
WantedBy=multi-user.target
4. 赋权
```bash
chmod a+x /etc/systemd/system/tomcat8.5.78.service
- 测试
[root@0010 system]# service tomcat8.5.78.service start
Redirecting to /bin/systemctl start tomcat8.5.78.service
[root@0010 system]# service tomcat8.5.78.service status
Redirecting to /bin/systemctl status tomcat8.5.78.service
● tomcat8.5.78.service - tomcat8.5.78 service
Loaded: loaded (/etc/systemd/system/tomcat8.5.78.service; disabled; vendor preset: disabled)
Active: active (running) since Mon 2022-04-25 15:08:45 CST; 2s ago
Process: 15283 ExecStart=/home/apache-tomcat-8.5.78/bin/startup.sh (code=exited, status=0/SUCCESS)
Main PID: 15297 (java)
Tasks: 33
Memory: 104.1M
CGroup: /system.slice/tomcat8.5.78.service
└─15297 /usr/bin/java -Djava.util.logging.config.file=/home/apache-tomcat-8...
Apr 25 15:08:45 0010 systemd[1]: Starting tomcat8.5.78 service...
Apr 25 15:08:45 0010 startup.sh[15283]: Tomcat started.
Apr 25 15:08:45 0010 systemd[1]: Started tomcat8.5.78 service
- 加载到systemd程序
systemctl daemon-reload
- 使用systemctl管理
[root@0010 system]# systemctl status tomcat8.5.78.service
● tomcat8.5.78.service - tomcat8.5.78 service
Loaded: loaded (/etc/systemd/system/tomcat8.5.78.service; disabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Mon 2022-04-25 15:10:54 CST; 9s ago
Process: 15437 ExecStop=/home/apache-tomcat-8.5.78/bin/shutdown.sh (code=exited, status=0/SUCCESS)
Process: 15283 ExecStart=/home/apache-tomcat-8.5.78/bin/startup.sh (code=exited, status=0/SUCCESS)
Main PID: 15297 (code=exited, status=143)
Apr 25 15:08:45 0010 systemd[1]: Starting tomcat8.5.78 service...
Apr 25 15:08:45 0010 startup.sh[15283]: Tomcat started.
Apr 25 15:08:45 0010 systemd[1]: Started tomcat8.5.78 service.
Apr 25 15:10:54 0010 systemd[1]: Stopping tomcat8.5.78 service...
Apr 25 15:10:54 0010 systemd[1]: tomcat8.5.78.service: main process exited, code=ex...n/a
Apr 25 15:10:54 0010 systemd[1]: Stopped tomcat8.5.78 service.
Apr 25 15:10:54 0010 systemd[1]: Unit tomcat8.5.78.service entered failed state.
Apr 25 15:10:54 0010 systemd[1]: tomcat8.5.78.service failed.
Hint: Some lines were ellipsized, use -l to show in full.
[root@0010 system]# systemctl start tomcat8.5.78.service
[root@0010 system]# systemctl status tomcat8.5.78.service
● tomcat8.5.78.service - tomcat8.5.78 service
Loaded: loaded (/etc/systemd/system/tomcat8.5.78.service; disabled; vendor preset: disabled)
Active: active (running) since Mon 2022-04-25 15:11:09 CST; 2s ago
Process: 15437 ExecStop=/home/apache-tomcat-8.5.78/bin/shutdown.sh (code=exited, status=0/SUCCESS)
Process: 15489 ExecStart=/home/apache-tomcat-8.5.78/bin/startup.sh (code=exited, status=0/SUCCESS)
Main PID: 15503 (java)
Tasks: 33
Memory: 101.1M
CGroup: /system.slice/tomcat8.5.78.service
└─15503 /usr/bin/java -Djava.util.logging.config.file=/home/apache-tomcat-8...
Apr 25 15:11:09 0010 systemd[1]: Starting tomcat8.5.78 service...
Apr 25 15:11:09 0010 startup.sh[15489]: Tomcat started.
Apr 25 15:11:09 0010 systemd[1]: Started tomcat8.5.78 service.
- 设置开启启动
systemctl enable tomcat8.5.78.service
- 查看开启自启服务
[root@0010 system]# systemctl list-unit-files|grep enable|grep tomcat
tomcat8.5.78.service enabled
码字不易,感谢阅读!