Systemd - Python script Main process exited, code=exited, status=1/FAILURE

8.8k Views Asked by At

Hi everyone the situation is the following:
I am connetected to an ubuntu-based EC2 machine of AWS ( and I am currently working on Ubuntu 18.04). I can manage files inside the machine and run scripts from terminal but I want the machine to run a script even when I am not connected. So I thought about a service.

Using systemd I located this file on /lib/systemd/system :

[Unit] Description=Test Service After=network.target
[email protected]

[Service] Type=simple ExecStart=/usr/bin/python3 /home/ubuntu/GreenHouse/Catalog/Catalog_REST.py
StandardInput=tty-force

[Install] WantedBy=multi-user.target

I started the service,ut when I check the status:

 sudo systemctl status CATALOG_REST-py.service

I receive this error:

● CATALOG_REST-py.service - Test Service Loaded: loaded (/lib/systemd/system/CATALOG_REST-py.service; enabled; vendor preset: enabled) Active: failed (Result: exit-code) since Sun 2021-08-22 16:54:13 UTC; 1s ago Process: 23968 ExecStart=/usr/bin/python3 /home/GreenHouse/Catalog/Catalog_REST.py (code=exited, status=1/FAILURE) Main PID: 23968 (code=exited, status=1/FAILURE)

Aug 22 16:54:13 ip-172-31-13-245 systemd[1]: Started Test Service. Aug 22 16:54:13 ip-172-31-13-245 systemd[1]: CATALOG_REST-py.service: Main process exited, code=exited, status=1/FAIL Aug 22 16:54:13 ip-172-31-13-245 systemd[1]: CATALOG_REST-py.service: Failed with result 'exit-code'.

I want to say that if I run in terminal python3 /home/GreenHouse/Catalog/Catalog_REST.py , it works.

Do you know some ways to obtain a better explanation of the error?

1

There are 1 best solutions below

2
Simone C. On

As better described in here, the issue may be related to the runtime environment.

In that case, a possible quick solution in my experience is to replace your service with the following one (it only differs for the ExecStart line):

[Unit] 
Description=Test Service After=network.target
[email protected]

[Service] 
Type=simple 
ExecStart=/bin/sh -c "/usr/bin/python3 /home/ubuntu/GreenHouse/Catalog/Catalog_REST.py"
StandardInput=tty-force

[Install] 
WantedBy=multi-user.target

Then reload the systemd manager configuration and restart your service:

sudo systemctl daemon-reload
sudo systemctl restart CATALOG_REST-py.service