systemctl status显示非活动死亡

nan*_*tor 15 linux sh systemd systemctl

我正在尝试编写自己的(简单的)systemd服务,它可以做一些简单的事情.(就像使用shell脚本将数字1到10写入文件一样).我的服务文件如下所示.

[Unit]
Description=NandaGopal
Documentation=https://google.com
After=multi-user.target

[Service]
Type=forking  
RemainAfterExit=yes
ExecStart=/usr/bin/hello.sh &

[Install]
RequiredBy = multi-user.target
Run Code Online (Sandbox Code Playgroud)

这是我的shell脚本.

#!/usr/bin/env bash

source /etc/profile
a=0
while [ $a -lt 10 ]
do
   echo $a >> /var/log//t.txt
        a=`expr $a + 1`
done
Run Code Online (Sandbox Code Playgroud)

由于某种原因,服务没有出现,systemctl显示以下输出.

root@TARGET:~ >systemctl status -l hello
* hello.service - NandaGopal
   Loaded: loaded (/usr/lib/systemd/system/hello.service; disabled; vendor     preset: enabled)
   Active: inactive (dead)
    Docs: https://google.com
Run Code Online (Sandbox Code Playgroud)

一直想弄清楚过去2天出了什么问题.有人可以帮我吗?

问候,nandanator

Mar*_*erg 21

  • 您已设置Type=Forking,但您的服务无效.尝试 Type=oneshot
  • 你有一条"&"你的ExecStart线,这是没有必要的.
  • 服务是disabled,这意味着它不是enabled在启动时启动.您应该运行systemctl enable hello以将其设置为在启动时启动.

您可以检查man systemd.directives以查找可在unit文件中使用的所有指令的索引.

  • “ systemctl enable hello”帮助了我,谢谢 (2认同)

Vad*_*S J 5

几点:

  1. 如果使用Type=forking,建议指定PidFile。

  2. 在您的情况下,Type=simple和,则ExecStart &不起作用。

  3. 用于systemctl start service-name启动服务

  4. 然后使用systemctl status service-name来检查其状态。如果未启动服务,则状态将为非活动/无效。