In this code I find a list all of the running java
processes and give the below function a name to look for, it will do its best. But since I find my approach a little too ugly, could some POSIX shell script writer have a look and possibly give me some simplification recommendations?
#!/bin/sh
# here is an ugly constant vuze java process name
readonly vuze_java_process_name='org.gudy.azureus2.ui.swt.Main'
is_java_program_running()
# expected arguments:
# $1 = java program name
{
# there will always be at least one java process:
# jdk.jcmd/sun.tools.jps.Jps, which is actually
# the program giving us the list of java processes
java_process_list=$( jps -l | awk '{print $2}' )
# this behaves strangely if there is zero processes (needs verification)
# but since there is always at least one, no problem here
java_process_list_count=$(( $( printf '%s\n' "${java_process_list}" | wc -l ) ))
# set the result value as if we did not find it
result=false
# POSIX-ly simulate FOR loop
i=1; while [ "${i}" -le "${java_process_list_count}" ]
do
# here we take one line from the list on $i position
java_process_entry=$( echo "${java_process_list}" | sed --posix --quiet "${i}{p;q}" )
# compare the given process entry with given java program name
if [ "${java_process_entry}" = "${1}" ]
then
# set the result value
result=true
# end this loop
break
fi
# increase iterator
i=$(( i + 1 ))
done
# depending on if we found vuze process running,
# return positive or negative result value
if [ "${result}" = true ]
then
return 0
else
return 1
fi
}
###
### EXAMPLE on Vuze
###
# keep Vuze alive forever, check in 5 seconds interval
while true
do
sleep 5s
if ! is_java_program_running "${vuze_java_process_name}"
then
my_date_time=$(date +%Y-%m-%d_%H:%M:%S)
printf '%s %s\n' "${my_date_time}" "(re-)starting Vuze"
( /home/vlastimil/Downloads/vuze/vuze > /dev/null 2>&1 & )
fi
done