I often have the need to run a piece of software as a daemon on Linux.
As I always tend to forget the exact syntax, I decided to write it down here.
The first step is to create a file in /etc/systemd/system/
with the extension .service
(e.g. my-daemon.service
).
1[Unit]
2Description=Runs myapp as a daemon
3
4[Service]
5Restart=always
6WorkingDirectory=/var/www/working-dir
7ExecStart=/var/www/working-dir/my-app
8StandardOutput=append:/var/www/working-dir/stdout.log
9StandardError=append:/var/www/working-dir/stderr.log
10Environment="DATA_DIR=data"
11Environment="PORT=3000"
12
13[Install]
14WantedBy=default.target
Once you have the file, you need to install it using the following commands:
1$ sudo systemctl daemon-reload
2$ sudo systemctl enable my-daemon.service
3$ sudo systemctl start my-daemon.service
You can run the following commands to control the daemon.
1$ sudo systemctl start my-daemon.service
2$ sudo systemctl stop my-daemon.service
3$ sudo systemctl restart my-daemon.service
4$ sudo systemctl status my-daemon.service
If this post was enjoyable or useful for you, please share it! If you have comments, questions, or feedback, you can email my personal email. To get new posts, subscribe use the RSS feed.