Go to /etc/systemd/system/
or create the folder if it does not exist.
Lets call our new service monitoring
:
Create the file /etc/systemd/system/monitoring.service
:
[Unit]
Description=Monitoring
After=network.target
StartLimitInterval=200
StartLimitBurst=5
[Service]
Type=forking # note the type here
User=monitoringuser
WorkingDirectory=/usr/monitoring
ExecStart=/home/monitoring/start.sh
ExecStop=/home/monitoring/stop.sh
Restart=always
RestartSec=30
[Install]
WantedBy=multi-user.target
Use type forking
for scripts which are starting the service and then exit.
For scripts which are running forever use type simple
:
[Unit]
Description=Monitoring
After=network.target
StartLimitInterval=200
StartLimitBurst=5
[Service]
Type=simple
User=monitoringuser
WorkingDirectory=/usr/monitoring
ExecStart=java -jar monitoring.jar
ExecStop=/bin/kill -9 $MAINPID
Restart=always
RestartSec=30
[Install]
WantedBy=multi-user.target
Whenever you change a *.service
file you have to reload systemd
systemctl daemon-reload
Now you can start and stop the service
systemctl start monitoring
systemctl stop monitoring
To enable or disable autostart and auto-restart
systemctl enable monitoring
systemctl disable monitoring