Reputation: 515
I'm trying to create an autostart service for my python-flask-socketio server.
I need to start a python script through systemd. Here's my service code:
[Unit]
Description=AppName
[Service]
Type=forking
ExecStart=/usr/bin/python3 /opt/myapp/app.py
[Install]
WantedBy=multi-user.target
If I try to start it manually using sudo service myservice start
- it works just fine. It halts my terminal\ssh window but I can close it and it works like expected.
But when I reboot my PC it does not start. When checking it's status with systemctl status myservice
I get the following:
systemd[1]: Starting My Service...
systemd[1]: myserivce.service: Start operation timed out. Terminating.
systemd[1]: Failed to start My Service.
systemd[1]: myserivce.service: Unit entered failed state.
systemd[1]: myserivce.service: Failed with result 'timeout'.
What am I doing wrong here?
Upvotes: 20
Views: 40058
Reputation: 338
In my particular case, I also had a flask application and was getting the same error, but it was working originally using the forking type, but recently stopped working after redeploying a new container. In my case, the problem is that the PIDFile property was pointing to a previous container that no longer existed.
In the section: [Service]
There was a property like this causing my issue:
PIDFile=/run/user/1000/containers/overlay-containers/b20a186570e8d177d86a649bab2a81c739ccea9bf5a157b5ea443700871587cf/userdata/conmon.pid
I was able to find the correct container name by running the command:
podman ps
Then I grabbed the first part of the container ID displayed and then matched it to the appropriate subdirectory listed within this location:
/run/user/1000/containers/overlay-containers
After updating it to the correct container ID, I left fork type alone, ran the command:
systemctl --user daemon-reload
systemctl --user start [containername]
Now it functions as expected and no longer times out thinking it's still starting.
Upvotes: 1
Reputation: 7
You have to check /var/log/message.
After you start service, If your script works normally at first and then failed, try this.
ExecStart=/usr/bin/bash -c '/usr/bin/python3 /opt/myapp/app.py'
Upvotes: 0
Reputation: 6264
Set a larger start timeout:
[Service]
TimeoutStartSec=300
In case your service would actually need more time to complete the startup.
Upvotes: 14
Reputation: 1262
Your type seems wrong, forking is for programs that detach immediately by themselves. Flask does not, it stays attached to your console.
Your service type should probably be simple
Upvotes: 34