Tag Archives: Code for start stop script

How to run your script with system boot in HPUX

A how-to guide for a running script or starting your custom coded daemon/service at system boot. Use the RC script directory to load custom daemons at boot.

There are many daemons and system services which starts with system boot. You might be wondering how to add your own script or customized daemon or service in boot sequence so that when the system is booted its there already running.

In this article, we will be seeing how to run your script with system boot in HPUX. First, we will create a small script that will be taking the start, stop arguments to call your original script/daemon/service. Then we will keep this script inside an appropriate run level RC directory so that it will be executed when the system enters that run level.

Let’s assume you have /usr/sbin/my_agentd to start at boot. You can create an additional script /sbin/init.d/my_agent which can take the start, stop options like below :

# cat /sbin/init.d/my_agent

choice=$1
case $choice in
"start")
        cd /usr/sbin
        ./my_agentd <other option if any>
        ;;
"stop")
        ps -ef|grep -i my_agentd|grep -v grep|awk '{print $2}'|xargs kill -9
        ;;
esac

The above script will take the start and stop as arguments. It will execute your agent binary when supplied with start argument and kills your running agent if supplied with a stop option. It is advisable to keep this script in /sbin/init.d directory since there lives all start, stop scripts of daemons or services.

Make sure you give proper executable permissions to this newly crafted script file.

# chmod 555 /sbin/init.d/my_agent

Now the last step is to have this script executed with run-level 3 (multi-user mode). To accomplish this, create a link for this file in /sbin/rc3.d directory.

/sbin/rc3.d directory contains all run level 3 related startup scripts. Keeping yours in it makes sure that it will start with run level 3.

# cd /sbin/rc3.d
# ln -s /sbin/init.d/my_agent S99my_agent

You are all set!

Now, whenever your system enters run level 3. It will try to execute S99my_agent file with the start argument. Which in turn calls /sbin/init.d/my_agent since its a link. When /sbin/init.d/my_agent (our coded script) gets start an argument, it calls /usr/sbin/my_agentd which is your customized daemon/service/script.