Create Custom Service (Daemon) | Systemd
Create file:
sudo nano /etc/systemd/system/somename.service
[Unit]
Description=MyApp
[Service]
ExecStart=/usr/bin/somecommand start
ExecStop=/usr/bin/somecommand stop
Restart=always
User=nobody
# Note Debian/Ubuntu uses 'nogroup', RHEL/Fedora uses 'nobody'
Group=nogroup
WorkingDirectory=/home/ubuntu/SomeDir
[Install]
WantedBy=multi-user.target
- Java App Example
[Unit]
Description=Email service
Requires=network.target remote-fs.target
After=network.target remote-fs.target
[Service]
Type=simple
PIDFile=/path_to_PID/java-app.pid
User=ubuntu
Group=ubuntu
WorkingDirectory=/opt/apps/
ExecStart=/usr/bin/java -jar /opt/apps/email-1.0.0.jar
StandardOutput=append:/var/log/app.log
StandardError=append:/var/log/app-error.log
Restart=on-failure
RestartSec=10
[Install]
WantedBy=multi-user.target
Enable To Run On Boot:
sudo systemctl enable somename.service
If you changed systemd config you need to reload it.
sudo systemctl daemon-reload
Start, Stop, Status:
sudo systemctl start somename.service
sudo systemctl stop somename.service
sudo systemctl restart somename.service
sudo systemctl status custom.service