首页 > 解决方案 > Program fails as Linux systemctl service but runs when called directly

问题描述

I have a script (a web listener) which runs on its own without problem:

> /usr/bin/python3 /home/[user]/scripts/CrossPlatform/workflow_trigger.py 

I already have a different service that works well. So, I copied that file under a new name /etc/systemd/system/my_workflow.service:

Description=My Service
After=network.target
StartLimitIntervalSec=0
[Service]
Type=simple
Restart=always
RestartSec=1
User=root
ExecStart=/usr/bin/python3 /home/[user]/scripts/CrossPlatform/workflow_trigger.py

[Install]
WantedBy=multi-user.target

The ExecStart line is a direct copy of the command that runs successfully.

I then run:

systemctl daemon-reload
systemctl start my_service.service 

It runs without a message. Unfortunately:

systemctl status my_service.service 

Shows:

my_service.service - My Service
   Loaded: loaded (/etc/systemd/system/my_service.service; enabled; vendor preset: enabled)
   Active: activating (auto-restart) (Result: exit-code) since Wed 2020-07-01 10:57:35 CDT; 958ms ago
  Process: 2722 ExecStart=/usr/bin/python3 /home/[user]/scripts/CrossPlatform/workflow_trigger.py (code=exited, status=1/FAILURE)
 Main PID: 2722 (code=exited, status=1/FAILURE)

The OS is:

NAME="Ubuntu"
VERSION="18.04.4 LTS (Bionic Beaver)"

What might be a fix? Many thanks, Bogdan

标签: linuxservicesystemctl

解决方案


首先在 [service] 之后移动 ExecStart 并从服务中删除 user=root。还提到了服务中的 ExecStop。

Description=My Service
After=network.target
StartLimitIntervalSec=0

[Service]
ExecStart=/usr/bin/python3 /home/[user]/scripts/CrossPlatform/workflow_trigger.py
Type=simple
Restart=always
RestartSec=1

[Install]
WantedBy=multi-user.target

推荐阅读