Why? Because you want to restart your app with simple /etc/systemd/appname restart or be 100% sure, what after your server reboots, your app will be up and running.
Create your service file
touch /lib/systemd/system/appname.service
# or /etc/systemd/system
Add configuration
Nothing special, service name, necessary groups, executable path and work dir, also logs redirect
[Unit]
Description=appname
[Service]
Group=www-data
Type=simple
Restart=always
RestartSec=5s
WorkingDirectory=/home/user/apps/appname
ExecStart=/home/user/apps/appname
Restart=always
PermissionsStartOnly=true
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=sleepservice
[Install]
WantedBy=multi-user.target
Run and test
Cool, save your file, and let’s try to enable service with systemctl command:
sudo systemctl enable appname
Check status of your service
● appname.service - appname
Loaded: loaded (/lib/systemd/system/appname.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2019-08-08 20:00:05 UTC; 10min ago
Main PID: 21635 (acj)
Tasks: 3 (limit: 4915)
Memory: 2.1M
CPU: 10ms
CGroup: /system.slice/appname.service
└─21635 /home/user/appname
Dec 06 20:00:05 serenity systemd[1]: Started appname.
All your app logs now redirecting to syslog, just run usual journalctl to check them:
journalctl -u appname.service
That’s all guys.