How To Set Up A Service On Redhat V7

How To Set Up A Service On Redhat V7

Introduction

The following example shows how to set-up a service on Redhat V7 to start a monitoring Daemon on reboot

Step-By-Step

1. Set-up the Service Unit

  • Log on as root
  • cd /usr/lib/systemd/system
  • vi mymonitor.service
  • Add the contents of the service file as detailed in the example below

NOTES
Some sites may store the service files in /etc/systemd/system

2. Create the Script /u01/app/MyMonitor/control_monitor.sh

  • Log on as oracle
  • cd /u01/app/MyMonitor
  • vi control_monitor.sh
  • Add the contents of the script file as detailed in the example below

3. Test the stop and start of the service

  • Log on as root
  • systemctl start mymonitor
  • systemctl status mymonitor
  • systemctl stop mymonitor

4. Enable the service

  • Log on as root
  • systemctl enable mymonitor

Example of the mymonitor service file.

[Unit]
Description = MyMonitor Stop Start Service
After = network.target
[Service]
User = oracle
ExecStart = /u01/app/MyMonitor/control_monitor.sh start
ExecStop = /u01/app/MyMonitor/control_monitor.sh stop
RemainAfterExit=yes
[Install]
WantedBy = multi-user.target

Example of the stop start script file.

start() {
   nohup java mymonitor.jar &
}

stop() {
  MYPID=$(ps -ef |grep mymonitor.jar |grep -v grep |awk '{print $2}')
  kill -9 ${MYPID}
}

case $1 in
  start|stop) "$1" ;;
esac
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License