在HPUX上配置一个自定义的服务,用于init.d启动和停止一项服务,以下过程是配置一个简单的ping服务的启动和停止的过程。
-
Make a copy of the /sbin/init.d/template template file to use as a template for your pinger startup script.
# cp /sbin/init.d/template /sbin/init.d/pinger - Use your editor of choice to customize the new startup script.
# vi /sbin/init.d/pinger
- Scroll down to the case statement towards the middle of the script. Look for the following:
'start_msg')
# Emit a _short_ message relating to running this script # with the "start" argument; this message appears as part # of the checklist.
echo "Starting the
;;
Change the echo statement to the following:
'start_msg')
# Emit a _short_ message relating to running this script
# with the "start" argument; this message appears as part
# of the checklist.
echo "Starting the pinger subsystem"
;;
- Scroll down to the stop_msg portion of the case statement that looks like this:
'stop_msg')
# Emit a _short_ message relating to running this script
# with the "stop" argument; this message appears as part
# of the checklist.
echo "Stopping the
;;
Change the echo statement to the following:
'stop_msg')
# Emit a _short_ message relating to running this script
# with the "stop" argument; this message appears as part
# of the checklist.
echo "Stopping the pinger subsystem"
;;
- Scroll down to the start argument in the case statement that looks like this:
# Check to see if this script is allowed to run...
if [ "$CONTROL_VARIABLE" != 1 ]; then
rval=2
else
# Execute the commands to start your subsystem
:
fi
;;
Change the CONTROL_VARIABLE, and add the command necessary to start the ping command in the background as shown below. Also add a call to the set_return function to notify /sbin/rc if the daemon successfully starts:
# Check to see if this script is allowed to run...
if [ "$PINGER" != 1 ]; then
rval=2
else
# Execute the commands to start your subsystem
/usr/sbin/ping localhost >/dev/null &
set_return
:
fi
;;
- Next, scroll down to the stop argument in the case statement that looks like this:
# Check to see if this script is allowed to run...
if [ "$CONTROL_VARIABLE" != 1 ]; then
rval=2
else
:
# Execute the commands to stop your subsystem
fi
;;
Change the CONTROL_VARIABLE, and add the command necessary to kill the ping command as shown below. Many applications include a command that may be used to shutdown the application. Otherwise, use the kill and ps commands as shown here. In this case, we’re using the ps –C and –o options to obtain the PID of the process currently running the ping command. The –C and –o options only work if the UNIX95 variable has been defined to enable special XPG4 options on the ps command. See the ps(1) man page for more information. Add a call to the set_return function to notify /sbin/rc if the daemon successfully starts:
# Check to see if this script is allowed to run...
if [ "$PINGER" != 1 ]; then
rval=2
else
:
# Execute the commands to stop your subsystem
kill $(UNIX95=true ps -o pid= -C “ping”)
set_return
fi
;;
- Save your changes to /sbin/init.d/pinger and quit.
- Create a configuration file and a control variable for your new startup script:
# vi /etc/rc.config.d/pinger
PINGER=1
- Create a start link to start the new service at run level 3 using the “don’t care” 900 sequence number, and a kill link to kill the new service with sequence number 100 at run level 2:
# ln –s /sbin/init.d/pinger /sbin/rc3.d/S900pinger
# ln –s /sbin/init.d/pinger /sbin/rc2.d/K100pinger
- Test your new startup script by executing both the start and kill links.
# /sbin/rc3.d/S900pinger start
# ps –ef | grep ping
# /sbin/rc2.d/K100pinger stop
# ps –ef | grep ping
- Assuming the previous test succeeded, change run levels a few times to further test your scripts.
# init 2
# init 3
# init 2
-
Disable the the pinger service in /etc/rc.config.d/pinger.
# vi /etc/rc.config.d/pinger
PINGER=0 -
Return to run level 3 before proceeding to the next lab.
# init 3