LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Shell script to monitor the log file & kill the process if log is not updating. (http://www.linuxquestions.org/questions/programming-9/shell-script-to-monitor-the-log-file-and-kill-the-process-if-log-is-not-updating-4175416110/)

milu_k 07-11-2012 07:53 AM

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.

porphyry5 07-11-2012 11:42 AM

Quote:

Originally Posted by milu_k (Post 4724922)
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]}

Reuti 07-12-2012 09:44 AM

@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.

porphyry5 07-13-2012 09:38 AM

Quote:

Originally Posted by Reuti (Post 4726085)
@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)

milu_k 07-18-2012 03:29 AM

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.

porphyry5 07-19-2012 08:23 AM

Quote:

Originally Posted by milu_k (Post 4731413)
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.


All times are GMT -5. The time now is 06:52 AM.