linux opensuse custom init script

1. create the 'program'
2. create the init.d script
3. create the links
4. test

1.
The 'program' is a script that writes its pid to a file
in /var/run. The content of the file is used when stopping
the 'program'.

# cat /usr/bin/mysvc
#!/bin/sh
echo $$ > /var/run/mysvc.pid
while true
do
logger "$0 is running"
sleep 60
done

# chmod +x /usr/bin/mysvc

2. The init.d script contains three sections:
- header
- initialisation
- procedures

# cat /etc/init.d/mysvc
#!/bin/sh
# Copyright (c) 2013 Tester
# All rights reserved.
#
# Author: Tester, 2013
#
# /etc/init.d/mysvc
# and its symbolic link
# /usr/bin/mysvc
### BEGIN INIT INFO
# Provides: mysvc
# Required-Start: $network
# Required-Stop:
# Default-Start: 5
# Default-Stop: 0 1 2 3 6
# Short-Description: mysvc daemon, run a dummy script
# Description: dummy
### END INIT INFO

# Check for missing binaries
mysvc_BIN=/usr/bin/mysvc
test -x $mysvc_BIN || { echo "$mysvc_BIN not installed";
if [ "$1" = "stop" ]; then exit 0;
else exit 5; fi; }

mysvc_PIDFILE=/var/run/mysvc.pid

# Load the rc.status script for this service.
. /etc/rc.status
# Reset status of this service
rc_reset

case "$1" in
start)
DISPLAY=:0;export DISPLAY
echo -n "Starting mysvc "
## Start daemon with startproc(8). If this fails
## the return value is set appropriately by startproc.
# startproc $mysvc_BIN
startproc -f $mysvc_BIN

# Remember status and be verbose
rc_status -v
;;
stop)
echo -n "Shutting down mysvc "
logger "$mysvc_BIN stopping"
## Stop daemon with killproc(8) and if this fails
## killproc sets the return value according to LSB.

# killproc -TERM $mysvc_BIN
killproc -p $mysvc_PIDFILE -TERM $mysvc_BIN

# Remember status and be verbose
rc_status -v
;;
restart)
## Stop the service and regardless of whether it was
## running or not, start it again.
$0 stop
$0 start

# Remember status and be quiet
rc_status
;;
reload)
# If it supports signaling:
echo -n "Reload service mysvc "
killproc -HUP $mysvc_BIN
#touch /var/run/mysvc.pid
rc_status -v

## Otherwise if it does not support reload:
#rc_failed 3
#rc_status -v
;;
status)
echo -n "Checking for service mysvc "
## Check status with checkproc(8), if process is running
## checkproc will return with exit status 0.

# Return value is slightly different for the status command:
# 0 - service up and running
# 1 - service dead, but /var/run/ pid file exists
# 2 - service dead, but /var/lock/ lock file exists
# 3 - service not running (unused)
# 4 - service status unknown 🙁
# 5--199 reserved (5--99 LSB, 100--149 distro, 150--199 appl.)

# NOTE: checkproc returns LSB compliant status values.
checkproc $mysvc_BIN
# NOTE: rc_status knows that we called this init script with
# "status" option and adapts its messages accordingly.
rc_status -v
;;
*)
## If no parameters are given, print which are avaiable.
echo "Usage: $0 {start|stop|status|restart|reload}"
exit 1
;;
esac

rc_exit

# chmod +x /etc/init.d/mysvc

3.
The 'program' should only be started at runlevel 5
See the entry: Default-Start in the initialisation section.

# insserv mysvc
# chkconfig --list mysvc
mysvc 0:off 1:off 2:off 3:off 4:off 5:on 6:off

# service mysvc status
mysvc.service - LSB: mysvc daemon, run a dummy script
Loaded: loaded (/etc/init.d/mysvc)
Active: inactive (dead) since Wed, 2013-05-22

# service mysvc start
# cat /var/run/mysvc.pid
6642

# service mysvc status
mysvc.service - LSB: mysvc daemon, run a dummy script
Loaded: loaded (/etc/init.d/mysvc)
Active: active (running)

# ps -ef | grep mysvc | grep -v grep
root 6642 1 0 14:25 ? 00:00:00 /bin/sh /usr/bin/mysvc

This entry was posted in linux. Bookmark the permalink.

Comments are closed.