Skip to content

Instantly share code, notes, and snippets.

@balamaci
Created January 2, 2015 17:24
Show Gist options
  • Save balamaci/42e649ac8e787f9b64dd to your computer and use it in GitHub Desktop.
Save balamaci/42e649ac8e787f9b64dd to your computer and use it in GitHub Desktop.
start script outputing pid
#!/bin/bash
MYDIR=$(dirname $0)
STATFILE=$MYDIR/command.pid
COMMAND_MARK="myprocess.jar"
MEMORY_OPTIONS="-Xms32m -Xmx256m"
MISC_OPTIONS="-Djava.awt.headless=true"
LOG_FILE=console.log
function launch_COMMAND() {
pushd .
cd $MYDIR
source local.env
echo Starting Process ...;
java -server $MEMORY_OPTIONS $MISC_OPTIONS -jar $COMMAND_MARK > $LOG_FILE 2>&1 &
PID=$!
echo $PID > $STATFILE
popd
}
function shutdown_COMMAND() {
# this function assumes that command will gracefully exit on SIGTERM
echo Shutting down process...;
kill $(cat $STATFILE);
rm $STATFILE;
}
# main()
echo "Background application control script"
case "$1" in
start)
if [ -f $STATFILE ]; then
# pidfile exists.checking if it is valid or stale.
ps $(cat $STATFILE) | grep $COMMAND_MARK
if [[ $? == 0 ]]; then
# Process is a COMMAND , so the pidfile is not stale, AND the process is running.
echo Process $(cat $STATFILE) is still running, you can not start it again
exit 1
else
# A process with that pid exists, but is not our process, so the pidfile must be stale
rm $STATFILE
launch_COMMAND;
fi
else
# pidfile does not exist, so COMMAND was not started
launch_COMMAND;
fi
;;
stop)
if [ -f $STATFILE ]; then
# pidfile exists.checking if it is valid or stale.
ps $(cat $STATFILE) | grep $COMMAND_MARK
if [[ $? == 0 ]]; then
# Process is a COMMAND , so the pidfile is not stale, AND the process is running.
echo Process $(cat $STATFILE) is running,shutting down
shutdown_COMMAND;
exit 0
else
# A process with that pid exists, but is not our process, so the pidfile must be stale
rm $STATFILE
echo Not Started 2.
exit 2
fi
else
# pidfile does not exist, so COMMAND was not started
echo Not started 3.;
exit 3
fi
;;
*)
echo "Usage : $0 {start|stop}"
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment