Service files: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
 
(20 intermediate revisions by 2 users not shown)
Line 1: Line 1:
= Making a systemd service file =
=== Making a systemd service file ===


See: https://blog.miguelgrinberg.com/post/running-a-flask-application-as-a-service-with-systemd
When you deploy your application on a server, you need to make sure the application runs uninterrupted. If the application crashes, you'd want it to automatically restart, and if the server experiences a power outage, you'd want the application to start immediately once power is restored.
 
'''SEE:''' https://blog.miguelgrinberg.com/post/running-a-flask-application-as-a-service-with-systemd  


Useful: https://containersolutions.github.io/runbooks/posts/linux/debug-systemd-service-units/
Useful: https://containersolutions.github.io/runbooks/posts/linux/debug-systemd-service-units/


    sudo nano /etc/systemd/system/myflaskapp.service
$ sudo nano /etc/systemd/system/YOUR_NAME_OF_THE_APP'''.service'''


<source>
<pre>
[Unit]
[Unit]
Description=<a description of your application>
Description=<a description of your application>
Line 20: Line 22:
[Install]
[Install]
WantedBy=multi-user.target
WantedBy=multi-user.target
</source>
</pre>


Here is an example:
<syntaxhighlight lang="bash">
[Unit]
Description=Bla Bla Bla made in Flask
After=network.target
[Service]
User=YOUR_USERNAME
WorkingDirectory=/YOUR/PATH/
ExecStart=/YOUR/PATH/.venv/bin/flask --app NAME_OF_YOUR_PY_FILE run --port 51?? #DO NOT PUT .py AT THE END + #PUT YOUR_PORT
Restart=always
[Install]
WantedBy=multi-user.target
</syntaxhighlight>


When the service file is new or changed, you need (one time) to:
1st thing to do when your <code>.service</code> file is new or changed


    sudo systemctl daemon-reload
$ sudo systemctl daemon-reload


Then you can:
Then in order:


    sudo systemctl start myflaskapp
$ sudo systemctl '''start''' YOUR_NAME_OF_THE_APP
    sudo systemctl status myflaskapp
$ sudo systemctl '''status''' YOUR_NAME_OF_THE_APP
    sudo systemctl restart myflaskapp
    sudo systemctl stop myflaskapp


'''Then finally''' when you see that start works (checking status, checking that it actually is running , etc)
When you see that it's working (checking its status, that it actually is running, etc...)


    sudo systemctl enable myflaskapp
$ sudo systemctl '''enable''' YOUR_NAME_OF_THE_APP


Will make the "service" auto start when the pi restarts.
Will make the "service" auto start when the pi restarts.
'''Additional commands:'''
$ sudo systemctl restart YOUR_NAME_OF_THE_APP
$ sudo systemctl stop YOUR_NAME_OF_THE_APP


To view the log file (errors):
To view the log file (errors):


    sudo journalctl -u myflaskapp -f
$ sudo journalctl -u YOUR_NAME_OF_THE_APP -f
 
You can find documentation here:
 
https://www.freedesktop.org/software/systemd/man/latest/systemd.exec.html


[[Category:Cookbook]]
[[Category:Cookbook]]

Latest revision as of 13:11, 27 May 2026

Making a systemd service file

When you deploy your application on a server, you need to make sure the application runs uninterrupted. If the application crashes, you'd want it to automatically restart, and if the server experiences a power outage, you'd want the application to start immediately once power is restored.

SEE: https://blog.miguelgrinberg.com/post/running-a-flask-application-as-a-service-with-systemd

Useful: https://containersolutions.github.io/runbooks/posts/linux/debug-systemd-service-units/

$ sudo nano /etc/systemd/system/YOUR_NAME_OF_THE_APP.service
[Unit]
Description=<a description of your application>
After=network.target

[Service]
User=<username>
WorkingDirectory=<path to your app>
ExecStart=<app start command>
Restart=always

[Install]
WantedBy=multi-user.target

Here is an example:

 [Unit]
 Description=Bla Bla Bla made in Flask
 After=network.target
 
 [Service]
 User=YOUR_USERNAME
 WorkingDirectory=/YOUR/PATH/
 ExecStart=/YOUR/PATH/.venv/bin/flask --app NAME_OF_YOUR_PY_FILE run --port 51?? #DO NOT PUT .py AT THE END + #PUT YOUR_PORT
 Restart=always
 
 [Install]
 WantedBy=multi-user.target

1st thing to do when your .service file is new or changed

$ sudo systemctl daemon-reload

Then in order:

$ sudo systemctl start YOUR_NAME_OF_THE_APP
$ sudo systemctl status YOUR_NAME_OF_THE_APP

When you see that it's working (checking its status, that it actually is running, etc...)

$ sudo systemctl enable YOUR_NAME_OF_THE_APP

Will make the "service" auto start when the pi restarts.

Additional commands:

$ sudo systemctl restart YOUR_NAME_OF_THE_APP
$ sudo systemctl stop YOUR_NAME_OF_THE_APP

To view the log file (errors):

$ sudo journalctl -u YOUR_NAME_OF_THE_APP -f

You can find documentation here:

https://www.freedesktop.org/software/systemd/man/latest/systemd.exec.html