[root@localhost ~]# systemctl | egrep abrt
abrt-ccpp.service loaded active exited Install ABRT coredump hook
abrt-oops.service loaded active running ABRT kernel log watcher
abrt-xorg.service loaded active running ABRT Xorg log watcher
abrtd.service loaded active running ABRT Automated Bug Reporting Tool
systemctl 列出來的 loaded active exited 是什麼意思?
查了一下,原來是因為它沒有對應的 daemon 在執行:
root@localhost / # cat /usr/lib/systemd/system/abrt-ccpp.service
#####################
[Unit]
Description=Install ABRT coredump hook
After=abrtd.service
Requisite=abrtd.service
[Service]
Type=oneshot
ExecStart=/usr/sbin/abrt-install-ccpp-hook install
ExecStop=/usr/sbin/abrt-install-ccpp-hook uninstall
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
########################
注意上面的 ExecStart 裡是去執行 這個指令,當執行完畢之後就結束了,因此對 systemd 來說是有成功執行 (active),而只要 ExecStart 執行的指令結束後,就會變成 exited 了!
ptables 也是一個類似的例子,ExecStart 的指令是執行 iptables.init start,執後完後就結束了,沒有 daemon 會持續持行,因此狀態也會是 active (exited)。
[root@localhost ~]# systemctl status iptables.service
● iptables.service - IPv4 firewall with iptables
Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled; vendor preset: disabled)
Active: active (exited) since 三 2017-07-26 00:37:40 CST; 6s ago
Process: 3372 ExecStart=/usr/libexec/iptables/iptables.init start (code=exited, status=0/SUCCESS)
Main PID: 3372 (code=exited, status=0/SUCCESS)
7月 26 00:37:40 localhost.localdomain systemd[1]: Starting IPv4 firewall with iptables...
7月 26 00:37:40 localhost.localdomain iptables.init[3372]: iptables: Applying firewall rules: [ 确定 ]
7月 26 00:37:40 localhost.localdomain systemd[1]: Started IPv4 firewall with iptables.
[root@localhost ~]# grep ExecStart /usr/lib/systemd/system/iptables.service
ExecStart=/usr/libexec/iptables/iptables.init start
如何在启动的时候,运行自定义的一个脚本?
在/etc/systemd/system中新建一个文件(名称可以为myscript.service) 然后在其中写入如下内容:
[Unit]
Description=My script
[Service]
ExecStart=/usr/bin/my-script
[Install]
WantedBy=multi-user.target
然后开启该守护进程
# systemctl enablemyscript.service
本例是说当目标multi-usr载入的时候,会启动你这个自定义脚本,脚本需要有可执行权限。
注意:如果要启动 shell 脚本,请把#!/bin/sh加到脚本的第一行,下面的做法不会成功ExecStart=/bin/sh /path/to/script.sh。
.service 状态显示绿色的 "active (exited)" (例如 iptables)
这很正常,本例中的 iptables 并不是守护进程,而是由内核控制,所以装载完规则后自动退出了。
通过下面命令检查规则是否正确加载:
# iptables --list
參考資料:
https://stackoverflow.com/questions/42607771/jenkins-active-exited
https://wiki.archlinux.org/index.php/Systemd_FAQ_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)
It seems you are running a system with systemd yet you are using sysV commands. Did you create a sysV init script or a systemd unit file?
State active (exited) means that systemd has successfully run the commands but that it does not know there is a daemon to monitor.
If there is you must define it in the unit file by configuring the Type and ExecStart options appropriately according to whether the process you start is the main proces, forks child processes and exits etc.
Check the different systemd man pages or update your question and post the unit file or init script.