为什么从 Debian 服务启动 shell 脚本不能像命令行一样运行?

Why launching a shell script from Debian service does not behave as command line?

我有一个 python 机器人,我在命令行中启动它并要求我登录以开始工作。

为了跳过这一步,我添加了一个管道来直接在命令中插入登录名,如下所示,它起作用了:

printf "login" | python_module-py

现在,我想安排 bot 的启动,而不必自己启动它,避免 bot 需要我的电脑一直开着。

所以我买了一个 Debian VPS 并尝试创建一个 systemd 服务。我把命令放在 shell 中。这是我的服务:(假设我的脚本在 /home/user 中并且我拥有 rwx 的所有权利)

[Unit]
Description=Description
After=network.target

[Service]
Type=simple
User=user
Group=group
WorkingDirectory=/home/user
ExecStart=./script.sh

[Install]
WantedBy=multi-user.target

我试图启动它,但由于这个错误而失败了:

TypeError: unsupported operand type(s) for /: 'NoneType' and 'int'

我几乎可以肯定那是因为登录没有通过管道传递,我想知道为什么。

请提前原谅我的英语。

[已测试]

使用 /bin/sh -c 将命令保留在服务中也给出相同的错误:

ExecStart=/bin/sh -c '/usr/bin/printf "login" | /usr/bin/python3.7 -m python_module'

管道不会直接在例如ExecStart 语句,相反你应该使用类似 ExecStart=/bin/sh -c 'printf "login" | python_module-py' 的东西让 /bin/sh 处理管道。

您还应该能够通过设置 StandardInput.

将文件作为标准输入传递给您的服务

我解决了问题。

在我的例子中,我必须使用 xargs 命令来确保管道中粘贴的文本是真正粘贴的。

printf "login" | xargs python_module-py