Back in the old days of unix there was an easy way to start a daemon or script every time a computer booted. Simply put it in one of the /etc/rc.? text files and it would start all the services in the order specified. Later, it was made more flexible by having different startup folders based on which runlevel you were on. Even later still scripts these rc[1-6].d startup folders became deprecated yet are still used to some extent by legacy programs and now things are all managed with new commands.
To put it bluntly, it’s messy, non intuitive and definitely not as easy as it should be. There is hope however and getting a script or daemon to run “the right way” at startup isn’t too terribly daunting and I’ll walk you through the process now.
In our instance we need a program called splunkmysqlmonitor.py to run on boot. It takes one of 3 arguments, start, stop, restart, and is located in /opt/splunk/etc/apps/mysqlmonitor/bin/daemon/. It’s almost ready to run at startup but first we should look at the command we’re using to call splunkmysqlmonitor.py and that’s the chkconfig command.
The chkconfig command takes a script that’s located in /etc/init.d and creates all the necessary symlinks for it in the rc[1-6].d folders that tell the system what order to start all the services and which runlevels start which services. Runlevels are mostly deprecated in linux these days but just as an FYI, the runlevels you need to pay attention to are 2,3,4 and 5 and they are amost always identical. The only thing you really need to worry about is the order in the boot process that the scripts get started and and less so the order that the script gets shutdown when rebooted. For example, a program that relies on nfs to be on when running started necessarily needs to be run after the nfs command mounts the drives successfully. Numbers lower in the list start first and the list goes from 1 – 99. Since splunk is at priority 90 and this monitor needs to start after splunk I’ll give it a priority of 95. As for shutdown, this service should turn off quickly since it relies on other services to run and may spit out errors if these dependent services are turned off before it. I’ll give this shutdown a priority of 5 which means it’ll be one of the first processes to shutdown.
So now that we know when in the boot process the script should run and at (priority 95) which run levels it should run from (2,3,4,5) we just need to put this info into the system somehow. We do this by adding specially formatted comment lines into our script located in /etc/init.d. Here’s what our example looks like with the new comments added
#!/usr/bin/env python # run level startup shutdown # chkconfig: 2345 95 5 # description: monitors local mysql processes for splunk # processname: splunkmysqlmonitor #
import sys, time, os, socket...
Now we have to put the script into the /etc/init.d folder and that is best done with a symlink.
ln -s /opt/splunk/etc/apps/mysqlmonitor/bin/daemon/splunkmysqlmonitor.py /etc/init.d
And finally the chkconfig command itself
chkconfig --add /etc/init.d/splunkmysqlmonitor.py
This should add the script to startup and next time you reboot it’ll launch automagically.