You are not logged in.
Pages: 1
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
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
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
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
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
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
<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
Sorry for my late reply but I have been very busy
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
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
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
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
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
hmm I get this as the output:
awk: /^\+ /^\-
awk: ^ backslash not last character on line
Offline
A my bad it should be
awk '/^\+ |^\- /'
Offline
Pages: 1