LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-11-2012, 07:53 AM   #1
milu_k
LQ Newbie
 
Registered: Jul 2012
Posts: 2

Rep: Reputation: Disabled
Shell script to monitor the log file & kill the process if log is not updating.


Hi Everyone,

Can anyone help me to write a script to monitor the log file after every 10 mins & if the log file is not updating then kill the particular process by name.

Your early response will be highly appreciated.
 
Old 07-11-2012, 11:42 AM   #2
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch
Posts: 419

Rep: Reputation: 18
Quote:
Originally Posted by milu_k View Post
Hi Everyone,

Can anyone help me to write a script to monitor the log file after every 10 mins & if the log file is not updating then kill the particular process by name.

Your early response will be highly appreciated.
Not tested, but something like
Code:
logmtr(){
  oldln=
  while [ 0 ]; do
    newln=$(tail -1 log.txt)
    if [ "$newln" != "$oldln" ]; then oldln=$newln; sleep 10m; continue; fi
    oldfs=$IFS
    IFS=$'\x0A'
    psaux=($(ps aux|grep "$1"))
    IFS=$'\x20'
    pid=(${psaux[1]})
    kill ${pid[0]}
    IFS=$oldfs
    break
}
This should kill the first or only occurrence of app-to-kill, and then only if it cooperates with a kill request. If not you would need to use kill -9 ${pid[0]}

Last edited by porphyry5; 07-11-2012 at 11:46 AM. Reason: Add qualification
 
Old 07-12-2012, 09:44 AM   #3
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 11.4
Posts: 1,319

Rep: Reputation: 252Reputation: 252Reputation: 252
@porphyry5: instead of just comparing the contents of the file, I would suggest to use stat and remember the last modification time of the file.

Another way of implementing it, could be an application where you set an alarm(600); which you trap and to reset the timer by an inotify() when the file is modified.
 
Old 07-13-2012, 09:38 AM   #4
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch
Posts: 419

Rep: Reputation: 18
Quote:
Originally Posted by Reuti View Post
@porphyry5: instead of just comparing the contents of the file, I would suggest to use stat and remember the last modification time of the file.
That would be good too, but I'm lazy, and
Code:
newln=$(tail -1 log.txt)
is less typing than
Code:
newln=$(stat log.txt | grep Modify)
 
Old 07-18-2012, 03:29 AM   #5
milu_k
LQ Newbie
 
Registered: Jul 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks everyone for their valuable inputs.

With your inputs & search on google I made smalll scripts & its working for me.....

-----------------------------------------------
if test `find "/mylogfilepath" -mmin +10`
then
echo old enough
kill -9 `pgrep ProcessName`

fi
-----------------------------------------------

Pls reply me with your valuable suggestions.
 
Old 07-19-2012, 08:23 AM   #6
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch
Posts: 419

Rep: Reputation: 18
Quote:
Originally Posted by milu_k View Post
Thanks everyone for their valuable inputs.

With your inputs & search on google I made smalll scripts & its working for me.....

-----------------------------------------------
if test `find "/mylogfilepath" -mmin +10`
then
echo old enough
kill -9 `pgrep ProcessName`

fi
-----------------------------------------------

Pls reply me with your valuable suggestions.
Its best not to use "kill -9" as your first choice to shut down a process. Use "kill" instead. "kill" is a request to the app that it shut itself down, which allows it to clean up any immediate unfinished business and make an orderly exit. "kill -9" is an immediate forced shutdown of the app by the system, which may leave a mess of partially completed business behind it that may cause very obscure errors later on.

If you use kill first, wait a little and if the app is still active then use kill -9, it might save some grief.

Also, grep returns a list of everything that matches its argument, so if there is more than one instance running of the app you want to shut down, it will return a list of them all. I don't know how kill reacts to receiving a list, but if its like other linux commands, it will kill every instance on the list, not just one of them.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Process a log file on an hourly basis but the log only rotates once a day hattori.hanzo Programming 4 10-28-2010 08:55 AM
Daemon shell or perl script to monitor a log file khriz Programming 4 01-07-2010 07:35 AM
shell script to monitor log file calipryss Linux - Newbie 14 08-05-2008 10:46 PM
how to backup & truncate the log file while the process is running mvenkat_in Programming 13 10-30-2007 07:03 AM
Log all of a shell script's I/O to the console and a file trevelluk Programming 8 04-18-2005 01:40 PM


All times are GMT -5. The time now is 05:31 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration