Shell script to start and stop Lucee and Nginx

Update

I have written a new blog post with an updated script that starts and stops Lucee (Tomcat) as a non-root user on OSX. Click the link to see the details, otherwise keep reading.

 

----------------- original post follows ------------------

 

I have been working on my shell programming and thought I would write a script to start and stop my Lucee (Tomcat) and Nginx servers at the same time as a convenience. You can substitute process names and program commands if you want to use the script with a different combination of programs. This script should work on any system supporting ps, grep, and awk, e.g. linux and OS X.

Note this line:

if [ "$(ps -ef | grep tomcat | grep java | awk ' { print $2 } ')" ]

It checks for a running Tomcat process by using ps and filtering for tomcat, then filtering for java so as to not return the process information for the previous grep command,  which is piped to the grep java command along with the actual tomcat process.

#!/bin/sh

case $1 in
start)
        if [ "$(ps -ef | grep tomcat | grep java | awk ' { print $2 } ')" ]
        then
            echo Tomcat is running
        else
            echo Starting Tomcat ...
            sudo /opt/tomcat/bin/startup.sh
        fi
        if [ "$(ps -ef | grep 'nginx\:' | awk ' {print $2}')" ]
        then
            echo Nginx is running
        else
            echo Starting Nginx ...
            sudo nginx
        fi
        ;;
stop)
        if [ "$(ps -ef | grep tomcat | grep java | awk ' { print $2 } ')" ]
        then
            echo Stopping Tomcat ...
            sudo /opt/tomcat/bin/shutdown.sh
        else
            echo Tomcat is not running
        fi
        if [ "$(ps -ef | grep 'nginx\:' | awk ' {print $2}')" ]
        then
            echo Stopping Nginx ...
            sudo nginx -s stop
        else
            echo Nginx is not running
        fi
    ;;
*)
    echo "Usage: ./lucee start|stop"
    ;;
esac