【问题标题】:systemctl status shows inactive deadsystemctl status 显示非活动死机
【发布时间】:2017-02-13 18:36:51
【问题描述】:

我正在尝试编写自己的(简单的)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

这是我的 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

由于某种原因,服务没有出现,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

一直试图找出过去 2 天出了什么问题。

【问题讨论】:

  • 日志文件中是否有任何内容?你确定你可以写信到那个位置吗?
  • 你是否让chmod +x myScript的脚本可执行?祝你好运。
  • @l0b0 是的,有写访问权限。
  • @shellter 我猜脚本有写权限。

标签: linux sh systemd systemctl


【解决方案1】:
  • 您已设置Type=Forking,但您的服务不起作用。试试 Type=oneshot
  • 您的ExecStart 行有一个“&”,这不是必需的。
  • 该服务是disabled,这意味着它不是enabled 在启动时启动的。您应该运行 systemctl enable hello 将其设置为在启动时启动。

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

【讨论】:

  • 非常感谢您的帮助。 [:)] Type=oneshot 和 systemctl enable 成功了。我使用的是模拟器环境,当我为服务创建符号链接时,它运行顺利。 :)
  • “服务被禁用,这意味着它没有在启动时启动” - 这不一定是真的。 Systemd 似乎偶尔会丢失配置信息,例如将服务或计时器设置为启用,因此计时器或服务会在机器启动时启动。另见Systemd timer is lost after reboot?
  • @PhilipRego 您应该创建一个新的完整问题来说明您的完整问题。但是使用unix.stackexchange.com——它更适合systemd问题。
【解决方案2】:

几点:

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

  2. 在您的情况下,Type=simple 和没有& 的 ExecStart 将起作用。

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

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

【讨论】:

    猜你喜欢
    • 2021-06-04
    • 1970-01-01
    • 2018-03-14
    • 1970-01-01
    • 2019-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-15
    相关资源
    最近更新 更多