Note all systemd process run as root.
Rules:
- If systemd no see process it will shut it down. RemainAfterExit=true
- If installation is not creating service symbolic links, you forgot WantedBy = multi-user.target, under [Install] unit
Everything as root
[Unit]
Description = service test daemon
After = network.target
[Service]
#since we starting a separate process we fork
Type=forking
WorkingDirectory = /root
#since systemd is not monitoring the new process
RemainAfterExit=true
ExecStart = /root/serviceTest.sh start
ExecStop = /root/serviceTest.sh stop
ExecReload = /root/serviceTest.sh reload
[Install]
WantedBy = multi-user.target
Everything don't have to be in /root, as long as root has access to the file
#! /bin/bash
function do_start ( )
{
echo "Hi, start time $(date)" | tee -a /root/serviceTest.log
sleep 5
}
function do_stop ( )
{
echo "Bye, stopped time $(date)" | tee -a /root/serviceTest.log
}
function do_status ( )
{
echo "Status, asked time $(date)" | tee -a /root/serviceTest.log
}
# Some Things That run always
touch /var/lock/serviceTest
# Management instructions of the service
echo "Command: $0 $1" | tee -a /root/serviceTest.log
case "$1" in
start )
do_start
;;
stop )
do_stop
;;
reload )
do_stop
sleep 1
do_start
;;
status )
do_status
;;
* )
echo "Usage: $0 {start | stop | reload | status}"
exit 1
;;
esac
exit 0
chmod +x serviceTest.sh
systemctl enable serviceTest.service
Created symlink from /etc/systemd/system/multi-user.target.wants/serviceTest.service to /etc/systemd/system/serviceTest.service.
systemctl start serviceTest.service
systemctl stop serviceTest.service
systemctl deamon-reload
journalctl -xe