Reputation: 12101
Let's say I have to build a Scala application that will alert me if one of specified system processes stops running.
Is there a passive way to accomplish this as opposed to running an endless loop every few seconds of something like "pidof process_name" !
(executes shell command in scala)?
If Scala/Java is not capable of this, is there something I could have in the middle that would make this work? My JNI
and C
skills are rusty at best, but I would give it a shot if that really is the only way.
The operating system in question is Centos 6.4
UPDATE: I've found something called "upstart" which apparently is used by quite a few linux distros now (including Centos 6.4) to manage processes. According to this answer it may be capable of what I need, although not a lot of information is given.
Upvotes: 1
Views: 706
Reputation: 16412
Couple standard options:
You can schedule this script to run via crontab every 5 mins for example like this:
*/5 * * * * /install_dir/app_monitor.sh
The script itself app_monitor.sh
:
#!/bin/bash
# Application name
appName="jar"
# find all app instances
procCount=$(ps -ef | grep "$appName" | egrep -v grep | wc -l)
if [ "$procCount" -eq 0 ]; then
echo "No applications running."
mutt -e 'my_hdr From:[email protected]' -s "Your app just died yo, time: $(date)." [email protected] -c [email protected] < "More useful info or log file here"
# or use sendmail, or any other notification option
exit 1
else
echo "Something is running."
fi
This finds an app by name, or regex - adjust accordingly.
Another option would be to use fcntl file locking which is what Java standard library uses on Linux. You lock the pid file and keep it locked while application is running. A lock can be checked from any program whether its C, Java or shell script (lsof). If process dies a lock will be released by OS so you always know whether the process is dead or alive. If that process is a C program that forks off child processes then those will not inherit the lock by default.
I once done that implementation on Linux for apps that worked together and it was implemented in Python, Perl, Java, C, and shell scripts. It was used to avoid startup of multiple instances of the same app and for monitoring.
Use readymade tools like nagios: http://www.nagios.com/solutions/linux-process-monitoring/
Upvotes: 1
Reputation: 438
You can create a wrapper script that starts your process and then simply waits for its termination. You should be able to write the wrapper script in Bash or Scala (or other shell). After termination the wrapper can register the event with your Scala monitoring application.
Upvotes: 0