You are not logged in.

#1 2008-03-28 19:22:33

creatorx
Member
Registered: 2005-09-14
Posts: 26

application monitoring

Is there any way you can monitor an application startup and termination with a bash script.
For example, I want to know when Firefox is started, and when its terminated.

I found that you can do something like this;

while true
do
ps aux | head -n 1
ps aux | egrep 'firefox' | grep -v grep
sleep 15
done

But this only show the process information, I need something that can trigger an event on the application
startup and termination.

Thanks for your help

Offline

#2 2008-03-28 19:47:27

DonVla
Member
From: Bonn, Germany
Registered: 2007-06-07
Posts: 997

Re: application monitoring

hi,

simple bash script

#!/bin/sh
<command what you want to do before ff>
firefox
<command what you want to do after ff>

when firefox stops, <command what you wnat to do> is executed.

vlad

Offline

#3 2008-03-28 20:01:53

dyscoria
Member
Registered: 2008-01-10
Posts: 1,007

Re: application monitoring

An example of a command you could use on the command line would be:

echo firefox has started;firefox;echo firefox has stopped

Last edited by dyscoria (2008-03-28 20:02:19)


flack 2.0.6: menu-driven BASH script to easily tag FLAC files (AUR)
knock-once 1.2: BASH script to easily create/send one-time sequences for knockd (forum/AUR)

Offline

#4 2008-03-28 20:20:08

dyscoria
Member
Registered: 2008-01-10
Posts: 1,007

Re: application monitoring

Here's my quick attempt at something different, in case you need a different method to detect if an app is running. This script will carry on running regardless of whether the application is running or not and will notify what it's status is. Haven't quite figured out how to do commands only once though:

#!/bin/bash

detectApp ()
{
until [[ $(ps aux | awk '$11 == "xcalc"') == '' ]]
do
    echo xcalc is running
    sleep 1
done
    
echo xcalc is not running
sleep 1
detectApp
}

detectApp

Last edited by dyscoria (2008-03-28 20:25:48)


flack 2.0.6: menu-driven BASH script to easily tag FLAC files (AUR)
knock-once 1.2: BASH script to easily create/send one-time sequences for knockd (forum/AUR)

Offline

#5 2008-03-28 20:44:10

dyscoria
Member
Registered: 2008-01-10
Posts: 1,007

Re: application monitoring

OK second attempt here. Only echoes once whether or not the application is running, and continues to run in the background:

#!/bin/bash

detectApp ()
{
    until [[ $(ps aux | awk '$11 == "xcalc"') != '' ]]
    do
        sleep 1
    done

    echo xcalc has been executed

    until [[ $(ps aux | awk '$11 == "xcalc"') == '' ]]
    do
        sleep 1
    done

    echo xcalc has been stopped

    detectApp
}

detectApp

flack 2.0.6: menu-driven BASH script to easily tag FLAC files (AUR)
knock-once 1.2: BASH script to easily create/send one-time sequences for knockd (forum/AUR)

Offline

#6 2008-03-30 11:38:02

T-Dawg
Forum Fellow
From: Charlotte, NC
Registered: 2005-01-29
Posts: 2,736

Re: application monitoring

pgrep returns 1 if the process is found and 0 if not:

pgrep 2>&1 >/dev/null firefox && do_firefox_is_started_task()
pgrep 2>&1 >/dev/null firefox || do_firefox_is_shutdown_task()

Last edited by T-Dawg (2008-03-30 11:41:33)

Offline

#7 2008-03-30 13:02:33

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: application monitoring

T-Dawg wrote:

<who cares what he wrote, he's here>

Hey T-Dawg, good to see ya, how's it going?


*sigh*  It has been at least six months since I hijacked a good thread. I must be getting old.

Dusty

Offline

#8 2008-04-09 18:58:05

creatorx
Member
Registered: 2005-09-14
Posts: 26

Re: application monitoring

Sorry for my late reply but I have been very busy sad

Tanks for all the advice, I have learned a lot of new stuff, but let me elaborate more on my issue:

I want the script to run as long the computer is on, such as in an infinite loop, and I wanted to trace the applications/process which are created and terminated on the localhost.

Example output of the script can be something like;

App firefox has started
App htop has started
App firefox has terminated
App x has started
App x has terminated
App htop has terminated

I researched if there is any possible way to do this kind of thing through the syslogd, so far I have not found anything similar to this.
Or maybe I can someway monitor the signals which are send to the process ???

Again thank you for your replies

Offline

#9 2008-04-10 12:25:27

DonVla
Member
From: Bonn, Germany
Registered: 2007-06-07
Posts: 997

Re: application monitoring

check the /proc directory for new pids owned by given user every x seconds. get process name of pid and print it to stdout.
when application ends the pid directory vanishes form /proc. put everything in a bash script or better a python script and there you have your app monitoring script.

vlad

Offline

#10 2008-04-11 03:47:42

peets
Member
From: Montreal
Registered: 2007-01-11
Posts: 936
Website

Re: application monitoring

Another approach is to monitor the access to the executable files using inotify. When I set off to write this post, I thought "ha, that's easy, I'll monitor /proc for process creation and deletion," but /proc is a pseudo-filesystem and therefore inotify doesn't work with it. It seems though that it's possible to monitor /bin, /usr/bin, /usr/sbin, etc; from induction (I've tried running a couple apps, and they all behaved this way,) starting a program causes the "ACCESS" event on its executable file (it must be read in order to be executed), and stopping the same program causes a "CLOSE_NOWRITE" event (this is weird... I would have thought the executable file would have been closed right after having been read). So this may be useful for what you need.

The good thing about inotify is that you don't have to actively check for something every second. You can read when you please, and a buffer of all events will be returned. Nothing is missed: with polling, some processes can start and stop in bewteem two polls, and you will never know about them; inotify will catch everything.

Again though, I'm not sure checking the access of executable files is a reliable way to find out when the programs are started and stopped. That will have to be investigated further.

P.S.: inotify is part of the linux API since 2.6.13. Try "man inotify". There's "inotify-tools" in the AUR. There are bindings in perl and python and maybe more.

Last edited by peets (2008-04-11 03:49:43)

Offline

#11 2008-04-11 19:44:36

creatorx
Member
Registered: 2005-09-14
Posts: 26

Re: application monitoring

Thanks for all your replies, am going to go the inotify way, I will se what I can do either in bash script or in C using inotify

Offline

#12 2008-04-16 18:12:21

creatorx
Member
Registered: 2005-09-14
Posts: 26

Re: application monitoring

big_smile Well I made up a simple solution (finally), for anyone interested here it is; any comments are appreciated also (remember am still a newbie)

#!/bin/bash
while true
do
    ps -Ac | awk '{print $4}' | sort > old.ps.log
    sleep 20
    ps -Ac | awk '{print $4}' | sort > new.ps.log
    diff -c old.ps.log new.ps.log | awk '/^\+ /^\- '
done

This just gives me a '+' when an application is initiated, and a '-' when is terminated; I can now add logic to the script to do whatever when either '+' or '-' are found.  All I need to do now is to schedule it via cron to eliminate the busy waiting.

Offline

#13 2008-04-18 05:04:56

peets
Member
From: Montreal
Registered: 2007-01-11
Posts: 936
Website

Re: application monitoring

hmm I get this as the output:

awk: /^\+ /^\- 
awk:        ^ backslash not last character on line

Offline

#14 2008-04-18 14:06:22

creatorx
Member
Registered: 2005-09-14
Posts: 26

Re: application monitoring

A my bad it should be

awk '/^\+ |^\- /'

Offline

Board footer

Powered by FluxBB