Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Add a 'status' switch to startup items UNIX
After installing MySQL, I decided to build a startup item in /System -> Library -> StartupItems. On Linux, I usually have start, stop, restart, and status. OS X lets you do the first three, but does not have a status option by default. I decided to implement that feature.

Here's the one I did for MySQL. You can obviously adapt it for any Unix application. This document assumes you already have your startup item configured in both /etc/hostconfig and /System/Library/StartupItems.

Read the rest of the hint for the how-to...

The first thing you need to do is open /etc/rc.common in your text editor of choice. As always, make sure you backup any files you edit. Find the RunService () routine and add the following line to the case statement, right after the restart line:
status ) StatusService  ;;
Your case statement should now look like this:
RunService ()
{
    case $1 in
      start  ) StartService   ;;
      stop   ) StopService    ;;
      restart) RestartService ;;
      status ) StatusService  ;;
      *      ) echo "$0: unknown argument: $1";;
    esac
}
This entry tells your startup item what to do with the "status" switch. Save /etc/rc.common. Now you need to modify the script that starts and stops your Unix application. Open the startup script and add something similar to this right before the RunService "$1" line at the very end:
StatusService ()
{
        ps auxww | grep mysqld | grep -v grep > /dev/null 2>&1
        if [ $? -eq 0 ]; then
                ConsoleMessage "MySQL is running."
        else
                ConsoleMessage "MySQL is not running."
        fi
}
The first line just greps for the app and pipes the output to /dev/null. The second line queries the exit status and sends the appropriate console message. After adding this bit of code you can save the script. Here it is in action:
baracus:~ root# /System/Library/StartupItems/MySQL/MySQL stop
Stopping the MySQL database server.

baracus:~ root# /System/Library/StartupItems/MySQL/MySQL status
MySQL is not running.

baracus:~ root# /System/Library/StartupItems/MySQL/MySQL start
Starting the MySQL database server.

baracus:~ root# /System/Library/StartupItems/MySQL/MySQL status
MySQL is running.
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[9,989 views]  

Add a 'status' switch to startup items | 6 comments | Create New Account
Click here to return to the 'Add a 'status' switch to startup items' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Add a 'status' switch to startup items
Authored by: hirschle on Mar 17, '04 11:51:06AM

.. and you sure know that it would be perfect if you were using the "/Library/StartupItems" directory ..



[ Reply to This | # ]
Add a 'status' switch to startup items
Authored by: LC on Mar 17, '04 02:26:04PM

Is rc.common liable to be replaced during a subsequent OS update?



[ Reply to This | # ]
rc.common being replaced in system upgrades
Authored by: MordEth on Mar 18, '04 12:53:56AM

yes, i think so...i back mine up, because OS X has a history of overwriting /etc/rc.common...10.3.3 did.



[ Reply to This | # ]
Add a 'status' switch to startup items
Authored by: hekal on Mar 30, '04 06:54:54PM

Hmmm I rebooted and rc.common has the "status" flag missing from the case statement. I re-added it and rebooted to see if the reboot modifies it. I hope that isn't the case! :/



[ Reply to This | # ]
Add a 'status' switch to startup items
Authored by: hekal on Mar 30, '04 07:06:25PM

Yep. This is a Software Update quirk. I have a script I wrote ~ 2 years ago that does the following:

Creates a /backuproot/YYYY/Month/MM-DD-YYYY directory if it does not exist
Copies any pertinent files to this directory (some files in /etc, crontabs, ~/Library/Mail, etc)
Tar and gzips the MM-DD-YYYY directory
Deletes the MM-DD-YYYY directory after it's been gzip'd

rc.common is in there. I can just restore it from there if need be.



[ Reply to This | # ]
Add a 'status' switch to startup items
Authored by: bluehz on Mar 18, '04 12:31:15PM
Great tip thx - I used your hint to rewrite my custom firewall startup. Was a bit tricky since ipfw does not actually exhibit a process id that I can see. So I ended up managing a fake pid file. Its not the best script in the world but it works for me and I learned something in the process. I know the parsing of the command line arguments is ugly, but I spent an hour this morning reading up on the subject and never could get anything else to work. If soene guru out can enlighten me on a better way - I sure would like that.

I usually symlink this into /usr/local/bin/ipfwctl (learned that from my Slackware stuff) so its easy to startup.


#!/bin/sh
# add -xv for debugging

# ipfw init file.
#
#
#
# add this line in /etc/hostconfig
#	CUST_FIREWALL=-YES-

. /etc/rc.common

# variables
IPFW=/sbin/ipfw
verbose="false"
function usage () {
   cat <<EOF
Usage: `basename $0` [start|stop|restart|status] [-v]
   start   starts the firewall
   stop    stops the firewall
   restart restarts the firewall
   status  reports status of firewall with optional
           verbose [-v] listing of firewall rules
   -v      verbose (only in status)
EOF
exit 1
}

# parse command line arguments
if [ $# -gt 1 ]; then
	if [ $2 = "-v" ]; then
		verbose="true"
	else
		usage
	fi
fi

StartService ()
{
	if [ "${CUST_FIREWALL:=-YES-}" = "-YES-" ]; then
		ConsoleMessage "Configuring IPFW"
		/usr/sbin/sysctl -w net.inet.ip.fw.verbose=1
		CheckForNetwork

		# check for network
		if [ "${NETWORKUP}" = "-NO-" ]; then 
			echo "Network not available"; exit; 
		fi

		# remove stale fake pid
		if [ -f /var/run/ipfw.pid ]; then
			rm /var/run/ipfw.pid
		fi

		# clear all rules
		${IPFW} -f flush

		# start firewall
		ConsoleMessage "Starting Custom Firewall..."
		${IPFW} -q /etc/ipfw.conf

		# check status and write fake pid		
		if [ $? -eq 0 ]; then
			ConsoleMessage "Custom Firewall successfully started..."
			touch /var/run/ipfw.pid
		else
			ConsoleMessage "Could not start Custom Firewall..."
			exit 1
		fi
	fi
}

StopService ()
{
	# stop service and remove fake pid
	if [ -f /var/run/ipfw.pid ]; then
	       	ConsoleMessage "Stopping Custom Firewall..."
       		${IPFW} -f flush
		rm /var/run/ipfw.pid
	else
		ConsoleMessage "Custom Firewall is not running!"
	fi
}


RestartService () 
{ 
	StopService; 
	StartService; 
}

StatusService ()
{
	# check for fake pid
	if [ -f /var/run/ipfw.pid ]; then

	# check for verbose flag
	if [ $verbose = "true" ]; then
	echo "here"
    			ConsoleMessage "Custom Firewall is running!"
    			${IPFW} list
			else
				ConsoleMessage "Custom Firewall is running!"
 fi
	else
		ConsoleMessage "Custom Firewall is not running!"
    fi
}

RunService "$1"



[ Reply to This | # ]