Systemctl 状态显示成功,但服务处于非活动状态

时间:2021-03-05 18:26:22

标签: linux bash debian systemd systemctl

我有这个简单的 /etc/systemd/system/test.service 文件

[Unit]
Description=SkriptA
After=network.target auditd.service

[Service]
Type=simple
ExecStart=/opt/test.sh

[Install]
WantedBy=multi-user.target

当我运行 systemctl start test.service 然后 systemctl status test.service 显示服务成功但无效,请问这是为什么?

● test1.service - SkriptA
   Loaded: loaded (/etc/systemd/system/test1.service; disabled; vendor preset: enabled)
   Active: inactive (dead)

Mar 05 12:17:55 hro0078 systemd[1]: Started SkriptA.
Mar 05 12:17:55 hro0078 systemd[1]: test1.service: Succeeded.

ExecStart 脚本就像服务一样简单

#!/usr/bin/bash

echo Hi > /tmp/test1
date >> /tmp/test1

我使用:Linux version 4.19.0-13-amd64 (debian-kernel@lists.debian.org) (gcc version 8.3.0 (Debian 8.3.0-6)) #1 SMP Debian 4.19.160-2 (2020-11-28)

1 个答案:

答案 0 :(得分:2)

您的程序已成功启动并直接终止。

要让您的服务 active,它需要保持运行。示例:

#!/usr/bin/bash

while [ 1 ]; do
    echo Hi > /tmp/test1
    date >> /tmp/test1
    sleep 60
done