Is it possible in unix to write a script that could determine when a particular application is running or not ?
There are several ways. It would be useful in the future if you mentioned your OS.
here are two ways:
#On systems with pgrep - like Solaris Linux
is_running()
{
pgrep $1
}
# almost any system
is_running()
{
ps -ef | grep "$1" | grep -v "grep"
}
usage
is_up=$(is_running myppname)
if [[ -z "$is_up" ]] ; then
echo "myappname is not running"
else
echo "myappname is running"
fi
Thanks Jim
Its an old version of HP unix, I'll give your second iteration a try.
In addition to Jim's suggestion, you could kill the output inside the function and then I think it looks nicer when you use it:
is_running()
{
pgrep "$1" >dev/null 2>&1
}
or
is_running()
{
ps -ef | grep "$1" | grep -qv grep
}
Then you can do this:
if is_running myppname
then
echo "myappname is not running"
else
echo "myappname is running"
fi