Skip to content

Instantly share code, notes, and snippets.

@sloanlance
Last active October 28, 2016 18:11
Show Gist options
  • Save sloanlance/4d473e2d71d1bc07e2e3217341f7fe04 to your computer and use it in GitHub Desktop.
Save sloanlance/4d473e2d71d1bc07e2e3217341f7fe04 to your computer and use it in GitHub Desktop.
BASH: Script to determine whether an application is running.
#!/bin/bash --
# File containing the process ID of the application, written by the application
PID_FILE='app.pid'
# The process name or some string unique to the application's process
PROCESS_NAME="java -Dspring.config.location=${HOME}/application.properties"
[[ $# -gt 0 && $1 = '-v' ]] && VERBOSE=1
if [[ -f ${PID_FILE} ]]; then
[[ ${VERBOSE} ]] && echo "${PID_FILE} found"
PID=$(cat "${PID_FILE}")
else
[[ ${VERBOSE} ]] && echo "${PID_FILE} not found"
fi
if [[ ! ${PID} ]]; then
[[ ${VERBOSE} ]] && echo "No process ID. Searching processes by name."
PID=$(pgrep -f "${PROCESS_NAME}")
fi
if [[ ${PID} ]]; then
echo "Running: process ID = ${PID}"
exit $(true)
else
echo 'Not running: process ID not found'
exit $(false)
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment