Shell script to monitor the log file & kill the process if log is not updating.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
@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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.