【问题标题】:Converting watch into a unit file systemd将 watch 转换为单元文件 systemd
【发布时间】:2017-01-19 15:48:56
【问题描述】:

我有一个shell脚本如下

ss.sh

#!/bin/bash
opFile="custom.data"
sourceFile="TestOutput"
./fc app test > $sourceFile
grep -oP '[0-9.]+(?=%)|[0-9.]+(?=[A-Z]+ of)' "$sourceFile" | tr '\n' ',' > $opFile
sed -i 's/,$//' $opFile

要求是我需要将此脚本与 watch 命令一起使用。我想把它变成一个 systemctl 服务。我就是这样做的。

sc.sh

#!/bin/bash
watch -n 60 /root/ss.sh

在我的 /etc/systemd/system 中,

log_info.service

[Unit]

Description="Test Desc"
After=network.target

[Service]
ExecStart=/root/sc.sh
Type=simple

[Install]
WantedBy=default.target

当我运行 systemctl start log_info.service 时,它​​运行但不是按照我想要的方式连续运行。

关于运行 sytemctl status log_info.service,

info_log.service - "Test Desc"
   Loaded: loaded (/etc/systemd/system/info_log.service; disabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Mon 2016-09-12 08:17:02 UTC; 2min 18s ago
  Process: 35555 ExecStart=/root/sc.sh (code=exited, status=1/FAILURE)
 Main PID: 35555 (code=exited, status=1/FAILURE)

Sep 12 08:17:02 mo-b428aa6b4 systemd[1]: Started "Test Desc".
Sep 12 08:17:02 mo-b428aa6b4 sc.sh[35654]: Error opening terminal: unknown.
Sep 12 08:17:02 mo-b428aa6b4 systemd[1]: info_log.service: Main process exited, code=exited, status=1/FAILURE
Sep 12 08:17:02 mo-b428aa6b4 systemd[1]: info_log.service: Unit entered failed state.
Sep 12 08:17:02 mo-b428aa6b4 systemd[1]: info_log.service: Failed with result 'exit-code'.

关于它为什么运行不正确的任何想法?任何帮助将不胜感激!

【问题讨论】:

    标签: linux shell systemd


    【解决方案1】:

    所以我(从超级用户那里)得知这个失败的原因正是我的错误控制台中的内容,即

    Error opening terminal: unknown
    

    Watch 只能从终端执行,因为它需要访问终端,而服务没有该访问权限。

    一种可能的替代方法是使用不需要终端的命令,例如screentmux。或者,另一个对我有用的替代方案,由超级用户的grawity建议,是

    # foo.timer
    [Unit]
    Description=Do whatever
    
    [Timer]
    OnActiveSec=60
    OnUnitActiveSec=60
    
    [Install]
    WantedBy=timers.target
    

    这背后的逻辑是,需要每 60 秒运行一次脚本,而不是使用 watch。因此,grawity 建议我使用一个计时器单元文件,改为每 60 秒调用一次服务文件。如果服务单元与计时器单元的名称不同,则可以使用[Timer] Unit=

    希望这对您有所帮助,并为超级用户的 grawity 和 Eric Renouf +1 提供答案!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-29
      • 2017-05-06
      • 2018-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-15
      • 2012-02-22
      相关资源
      最近更新 更多