首页 > 解决方案 > SHC 编译脚本无法使用 systemctl 运行

问题描述

我有一个非常简单的 bash 脚本,叫做 test。

#!/bin/bash

while true
do
    currdate=$(date +%d/%m/%Y-%H:%M:%S)
    echo -e "\n$currdate ..."
    sleep 1
done

从命令行运行它完全符合预期。

我用 SHC 编译的如下: shc -r -U -f test.sh -o test

从命令行运行它按预期工作。

我已经设置使用 systemctl 运行它,添加了我的配置文件/etc/init.d和这个引用test

如果test是我未编译的 bash 脚本,则systemclt start test可以正常工作。如果我交换测试并将其替换为已编译的版本,则它会失败。

systemctl 状态显示:

test.service - LSB: Test
   Loaded: loaded (/etc/init.d/test; generated; vendor preset: disabled)
   Active: failed (Result: exit-code) since Tue 2020-02-18 15:38:05 GMT; 6s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 18089 ExecStart=/etc/init.d/test start (code=exited, status=7)
    Tasks: 2
   CGroup: /system.slice/test.service
           ├─18096 /home/rocket/test -c                                                                                                                                                                                                  >
           └─18142 sleep 1

Feb 18 15:38:05 linux-c0a9 systemd[1]: test.service: Control process exited, code=exited status=7
Feb 18 15:38:05 linux-c0a9 systemd[1]: Failed to start LSB: Test.
Feb 18 15:38:05 linux-c0a9 systemd[1]: test.service: Unit entered failed state.
Feb 18 15:38:05 linux-c0a9 systemd[1]: test.service: Failed with result 'exit-code'.

journalctl 显示:

Feb 18 15:38:05 linux-c0a9 systemd[1]: Starting LSB: Test...
Feb 18 15:38:05 linux-c0a9 test[18089]: Starting test
Feb 18 15:38:05 linux-c0a9 test[18089]: 18/02/2020-15:38:05 ...
Feb 18 15:38:05 linux-c0a9 test[18089]: ..failed
Feb 18 15:38:05 linux-c0a9 systemd[1]: test.service: Control process exited, code=exited status=7
Feb 18 15:38:05 linux-c0a9 systemd[1]: Failed to start LSB: Test.
Feb 18 15:38:05 linux-c0a9 systemd[1]: test.service: Unit entered failed state.
Feb 18 15:38:05 linux-c0a9 systemd[1]: test.service: Failed with result 'exit-code'.

有谁知道为什么会失败以及如何阻止它失败?

我只是使用 SHC 来阻止用户编辑脚本,而不是为了任何安全等。

谢谢

标签: bashsystemctl

解决方案


我想我可能已经解决了这个问题。

我在 /etc/init.d 中的脚本正在调用/home/rocket/test

当它是一个 bash 文件时它很好,当它被编译时它停止工作。

为了让它工作,我将 test 更改为始终是一个 bash 文件,该文件调用位于/home/rocket/bin/test


推荐阅读