5

I have the following systemd service to run my flask app:

[Unit]
Description=My Flask server
After=network.target

[Service]
User=ubuntu
ExecStart=/home/ubuntu/my-server/script.py
WorkingDirectory=/home/ubuntu/my-server/
Restart=on-failure
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

However, when I run

systemctl start my-server.service

I get the following status

my-server.service - My Flask server
   Loaded: loaded (/etc/systemd/system/my-server.service; enabled; vendor preset: enabled)
   Active: active (exited) (Result: exit-code) since Wed 2017-06-21 22:24:18 UTC; 8min ago
 Main PID: 8407 

(code=exited, status=203/EXEC)

How do I prevent the Flask server from exiting? When I run the script directly with

/home/ubuntu/my-server/script.py

it works fine. But if I run it with systemd, the server exits immediately.

2 Answers 2

2

Not sure if this helps but this is the service file that I use to control a flask application for a personal project. I think by default, systemd will run a script with a /bin/sh shell with no personalizations. You might want to try switching to a /bin/sh shell and running your command. There may be an environment variable missing.

Unit file from link:

[Unit]
Description=Persistent Transactions API
After=syslog.target network.target electrum.service

[Service]
User=percy
Group=percy
ExecStart=/bin/bash -c "source /opt/persist_transaction/bin/activate ; /opt/persist_transaction/api.py -c /opt/persist_transaction/config.ini"
Restart=always

[Install]
WantedBy=multi-user.target
2

Fixed my own problem. I had to change the file owner to match the systemd script and then make it executable:

chown ubuntu /home/ubuntu/my-server/script.py
chmod +x /home/ubuntu/my-server/script.py

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .