CentOS startup script

I am trying to create a user-friendly means of restarting all relevant services on a CentOS server. The script appears below. So far, all of it works EXCEPT - it seems to shut down tomcat and then does not start it back up. When the script is run, it prompts the first time for a password. After that, all other commands are run as su (sudo), and do not prompt for a password. The command throw no errors. If I run the sudo commands for tomcat manually, they work fine, but in this script, tomcat (lucee) never starts back up. Anyone care to take a whack at why?

sudo echo "WEB DEV SERVICE RESTARTS"
clear


echo
echo ------------------------- STOPPING -------------------------

echo "Stopping Apache..."
sudo apachectl stop
sleep 2
echo "done."
echo

echo "Stopping Tomcat/Lucee..."
sudo /opt/lucee/tomcat/bin/shutdown.sh
sleep 2
echo "done."
echo

echo "Stopping MySQL..."
sudo systemctl stop mariadb
sleep 3
echo "done."
echo


echo
echo ------------------------- STARTING -------------------------

echo "Starting Apache..."
sudo apachectl start
sleep 2
echo "done."
echo

echo "Starting Tomcat/Lucee..."
sudo /opt/lucee/tomcat/bin/startup.sh
sleep 2
echo "done."
echo

echo "Starting MySQL..."
sudo systemctl start mariadb
sleep 2
echo "done."
echo
sleep 3

My guess here would be that Tomcat hasn’t fully stopped yet by the time you’re trying to start it again. You should check that the PID of Apache, Tomcat, MySQL, etc. have actually been removed before moving on to the next step.

Something like;

while ps -p $pid > /dev/null; do sleep 1; done;

where $pid is the PID of the process being stopped. You can get the PID for each process using code similar to the following:

ps -ef | awk '$8=="name_of_process" {print $2}'

where name_of_process is the process you’re wanting to get the PID for.

More details:

EDIT: Also, if your intent is to just restart the services then systemctl restart <service name> would do the stop/start for you.

HTH

– Denny

1 Like

What version of CentOS? Why aren’t you using SystemD?

1 Like

Thanks all. Using CentOS 7. Denny, I believe you were correct; likely that the process was still shutting down when the startup script was called. Replaced both with:

service lucee_ctl restart 

And all seems to be well. Thanks for the point in the right direction!

2 Likes