Program.service ExecStart失败但程序本身运行

时间:2017-01-11 22:05:28

标签: service centos7

我正在测试如何在CentOS7上使用.service文件运行脚本。

脚本是一个非常简单的循环,只是为了确保它运行:

if [ "$1" == "start" ] || [ "$1" == "cycle" ]
then
    /u/Test/Bincustom/haltrun_wrap.sh run &
    echo $!  /u/Test/Locks/start.pid
    exit
elif [ "$1" == "stop" ] || [ "$1" == "halt" ]
then
    killall -q -9 haltrun_wrap.sh
    echo " "  /u/Test/Locks/start.pid
elif [ "$1" == "run" ]
then
    process_id=$(pidof haltrun_wrap.sh)
    #echo $process_id  /u/Test/Locks/start.pid
    while [ 1 ]
    do
        CurTime=$(date)
        echo $CurTime  /u/Test/Logs/log
        sleep 30s
    done
else
    cat /u/Test/Locks/start.pid
    cat /u/Test/Logs/log
fi

如果我手动启动,该脚本可以作为root用户或测试用户正常运行。

Program.service文件如下所示:

[Unit]
Description=Program

[Service]
Type=forking
RemainAfterExit=yes
PIDFile=/u/Test/Locks/start.pid
EnvironmentFile=/u/Test/Config/environ
Environment="Base="sudo -u sirsi '/u/Test/Bincustom/Program " "Stop=halt force'" "Start=cycle force'""
ExecStart=/bin/sh $Base$Start
ExecStop=/bin/sh $Base$Stop

[Install]
WantedBy=multi-user.target
WantedBy=WebServices
WantedBy=BCA

错误总是:

● Program.service - Program
   Loaded: loaded (/usr/lib/systemd/system/Program.service; enabled; vendor preset: disabled)
   Active: failed (Result: resources) since Wed 2017-01-11 14:53:10 MST; 1s ago
  Process: 12014 ExecStart=/bin/sh $Base$Start (code=exited, status=0/SUCCESS)

Jan 11 14:53:09 localhost.localdomain systemd[1]: Starting Program...
Jan 11 14:53:10 localhost.localdomain systemd[1]: PID file /u/Test/Locks/start.pid not readable (yet?) after start.
Jan 11 14:53:10 localhost.localdomain systemd[1]: Failed to start Program.
Jan 11 14:53:10 localhost.localdomain systemd[1]: Unit Program.service entered failed state.
Jan 11 14:53:10 localhost.localdomain systemd[1]: Program.service failed.

显然我在.service中做错了但是对于我的生活,我仍然错过了它。

1 个答案:

答案 0 :(得分:0)

问题在于:

  

环境=" Base =" sudo -u sirsi' / u / Test / Bincustom / Program" "停止=停止'" "开始=循环力'""   ExecStart = / bin / sh $ Base $ Start   ExecStop = / bin / sh $ Base $ Stop

显然.service文件无法识别变量。

我也遇到了sudo不能运行我的测试脚本的问题。 我不得不将sudo添加到测试脚本中。