Systemctrl 服务退出 203,即使脚本通过“手动”启动也能工作

Systemctrl 服务退出 203,即使脚本通过“手动”启动也能工作

我正在使用Red Hat Linux 8并想要创建一个 systectrl 命令来启动 Flask 脚本。

如果像下面这样手动启动,脚本运行正常:/bin/bash /data_pyt/ro/python_script/folder_python/scripts/python_script.sh

我的配置如下所示:

adminUser:/etc/systemd/system# sudo vi python_script.service
[Unit]
Description=Product
After=network.target

[Service]
Type=simple
User=flask
Group=flask
ExecStart="/bin/bash /data_pyt/ro/python_script/folder_python/scripts/python_script.sh"
Restart=always
RestartSec=10s
SyslogIdentifier=python_script

[Install]
WantedBy=multi-user.target
~

启动服务时我得到:

adminUser:/etc/systemd/system# sudo systemctl daemon-reload
adminUser:/etc/systemd/system# sudo systemctl restart python_script.service
adminUser:/etc/systemd/system# sudo systemctl status python_script.service
 python_script.service - Product routing
   Loaded: loaded (/etc/systemd/system/python_script.service; disabled; vendor preset: disabled)
   Active: activating (auto-restart) (Result: exit-code) since Tue 2021-08-03 15:26:42 CEST; 4s ago
  Process: 444293 ExecStart=/bin/bash /data_pyt/ro/python_script/folder_python/scripts/python_script.sh (code=exited, status=203/EXEC)
 Main PID: 444293 (code=exited, status=203/EXEC)
adminUser:/etc/systemd/system# sudo systemctl enable python_script.service
Created symlink /etc/systemd/system/multi-user.target.wants/python_script.service → /etc/systemd/system/python_script.service.
adminUser:/etc/systemd/system# sudo systemctl status python_script.service
 python_script.service - Product routing
   Loaded: loaded (/etc/systemd/system/python_script.service; enabled; vendor preset: disabled)
   Active: activating (auto-restart) (Result: exit-code) since Tue 2021-08-03 15:27:13 CEST; 5s ago
  Process: 444404 ExecStart=/bin/bash /data_pyt/ro/python_script/folder_python/scripts/python_script.sh (code=exited, status=203/EXEC)
 Main PID: 444404 (code=exited, status=203/EXEC)
    Tasks: 0 (limit: 101113)
   Memory: 0B
   CGroup: /system.slice/python_script.service
 

目录scripts/python_script.sh*.sh文件有chmod 777

此外,该路径/bin/bash /data_pyt/ro/python_script/folder_python/scripts/python_script.sh在手动启动时有效。

有什么建议可能是错误所在或者如何定位错误吗?

我很感谢你的回复!

答案1

此外,路径 /bin/bash /data_pyt/ro/python_script/folder_python/scripts/python_script.sh 在手动启动时有效。

那不是一条路。这其实是由空格分隔的路径。它不起作用的原因是因为你的双引号告诉 systemd 将其解释为一条路径。

  • 不好(语法不需要引号;相反,它们变成了部分ExecStart 值,并使两个单词被视为单个单词):

    ExecStart="/bin/bash /data_pyt/ro/python_script/folder_python/scripts/python_script.sh"
    
  • 好(无需引用):

    ExecStart=/bin/bash /data_pyt/ro/python_script/folder_python/scripts/python_script.sh
    
  • 更好(如果你的脚本已经是+x,那么解释器就是多余的)

    ExecStart=/data_pyt/ro/python_script/folder_python/scripts/python_script.sh
    
  • 更好的是(整个 shell 脚本通常是多余的):

    WorkingDirectory=/data_pyt/ro/python_script/folder_python
    Environment=PYTHONPATH=/data_pyt/ro/python_script/etc/etc/etc
    Environment=FLASK_APP=whatever
    ExecStart=/usr/bin/flask run
    

如果您在手动启动时对整个内容使用双引号,那么它也不起作用。

$ /bin/bash /tmp/hello.sh
Hello!

$ "/bin/bash /tmp/hello.sh"
-bash: /bin/bash /tmp/hello.sh: No such file or directory

例如,您在哪里需要在 systemd 中引用 - 请注意,引用了特定参数,但不是整个 ExecStart:

Environment=EXAMPLE=whatever "THING=Value with spaces" FLASK_APP=spaces

ExecStart=/bin/bash "/home/Carol/My Projects/Paths With Spaces/script.sh" --daemon

相关内容