LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-30-2011, 11:29 AM   #1
kais1
Member
 
Registered: Dec 2008
Posts: 61

Rep: Reputation: 17
Killing process automatically


Dear all,

I have a log file which gives some info like

11/07/30 06:13:55 [libopmnoc4j] OC4J ping attempt timed out
11/07/30 06:13:55 [libopmnoc4j] Process Ping Failed: default_group~OC4J_CMG~default_group~1 (1080848993:28956)


I need a script to constantly monitor this log file and look for the above files. if the file is getting any new
messages like above, it has to take the process id from the line

[libopmnoc4j] Process Ping Failed: default_group~OC4J_CMG~default_group~1 (1080848993:28956)


which is 28956 and do kill -9 28956 automatically/immediately.

I need to your expert guidance to achieve the same .

Thanks
Kai
 
Old 07-30-2011, 12:29 PM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Try something like:

Code:
while [[ 1 == 1 ]]; do
    rev /path/to/log/file | grep "$(date +"%y/%m/%d %H:%M:%S").*Process Ping Failed:"
    if [ $? ]; then
        PID=$(rev /path/to/log/file | grep "Process Ping Failed:" | grep -o ":[0-9]*)" | sed 's/://; s/)//')
        if kill -9 $PID > /dev/null 2>&1; then
            echo "Killed $PID"
        fi
    fi
    sleep 1
done
Hope this helps,

Last edited by Snark1994; 07-30-2011 at 02:04 PM.
 
1 members found this post helpful.
Old 07-30-2011, 02:01 PM   #3
kais1
Member
 
Registered: Dec 2008
Posts: 61

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by Snark1994 View Post
Try something like:

Code:
while [[ 1 == 1 ]]; do
    rev /path/to/log/file | grep "$(date +"%y/%m/%d %H:%M:%S).*Process Ping Failed:"
    if [ $? ]; then
        PID=$(rev /path/to/log/file | grep "Process Ping Failed:" | grep -o ":[0-9]*)" | sed 's/://; s/)//')
        if kill -9 $PID > /dev/null 2>&1; then
            echo "Killed $PID"
        fi
    fi
    sleep 1
done
Hope this helps,
<dapps02]orasoa:medt/appl/1013/opmn/logs> sh opmn.sh
opmn.sh: syntax error at line 3: `)' unexpected
<dapps02]orasoa:medt/appl/1013/opmn/logs> cat opmn.sh
#!/bin/sh
while [[ 1 == 1 ]]; do
rev medt/appl/1013/opmn/logs/opmn.log | grep "$(date +"%y/%m/%d %H:%M:%S).*Process Ping Failed:"
if [ $? ]; then
PID=$(rev medt/appl/1013/opmn/logs/opmn.log | grep "Process Ping Failed:" | grep -o ":[0-9]*)" | sed 's/://; s/)//')
if kill -9 $PID > /dev/null 2>&1; then
echo "Killed $PID"
fi
fi
sleep 1
done


Any idea ?

Kai
 
Old 07-30-2011, 02:05 PM   #4
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Sorry, I missed a quote - I've amended the original code. It should be:

Code:
grep "$(date +"%y/%m/%d %H:%M:%S").*Process Ping Failed:"
not

Code:
grep "$(date +"%y/%m/%d %H:%M:%S).*Process Ping Failed:"
Apologies,
 
Old 07-30-2011, 02:11 PM   #5
kais1
Member
 
Registered: Dec 2008
Posts: 61

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by Snark1994 View Post
Sorry, I missed a quote - I've amended the original code. It should be:

Code:
grep "$(date +"%y/%m/%d %H:%M:%S").*Process Ping Failed:"
not

Code:
grep "$(date +"%y/%m/%d %H:%M:%S).*Process Ping Failed:"
Apologies,
Thanks Snark,

Am getting the below error now

opmn.sh: syntax error at line 5: `PID=$' unexpected

Kai
 
Old 07-30-2011, 02:20 PM   #6
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Can you post the code you're running? I can't run the code I posted for you (I don't have either the log file or processes to kill), but this works for me without complaining:
Code:
#!/bin/sh
while [[ 1 == 1 ]]; do
    rev tt | grep "$(date +"%y/%m/%d %H:%M:%S").*Process Ping Failed:"
    PID=$(rev tt | grep "Process Ping Failed:" | grep -o ":[0-9]*)" | sed 's/://; s/)//')
    sleep 1
done
If you can run the above code but not what you've got in your script then I'll have to do some thinking about how I can debug it more carefully
 
Old 07-30-2011, 02:24 PM   #7
kais1
Member
 
Registered: Dec 2008
Posts: 61

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by Snark1994 View Post
Can you post the code you're running? I can't run the code I posted for you (I don't have either the log file or processes to kill), but this works for me without complaining:
Code:
#!/bin/sh
while [[ 1 == 1 ]]; do
    rev tt | grep "$(date +"%y/%m/%d %H:%M:%S").*Process Ping Failed:"
    PID=$(rev tt | grep "Process Ping Failed:" | grep -o ":[0-9]*)" | sed 's/://; s/)//')
    sleep 1
done
If you can run the above code but not what you've got in your script then I'll have to do some thinking about how I can debug it more carefully
Thanks Snark

[/CODE]

#!/bin/sh
while [[ 1 == 1 ]]; do
rev /fraud/appl/orasoa/1013/opmn/logs/opmn.log | grep "$(date +"%y/%m/%d %H:%M:%S").*Process Ping Failed:"
if [ $? ]; then
PID=$(rev /fraud/appl/orasoa/1013/opmn/logs/opmn.log | grep "Process Ping Failed:" | grep -o ":[0-9]*)" | sed 's/://; s/)//')
if kill -9 $PID > /dev/null 2>&1; then
echo "Killed $PID"
fi
fi
sleep 1
done

[/CODE]

Tried running the below

[/CODE]

#!/bin/sh
while [[ 1 == 1 ]]; do
rev tt | grep "$(date +"%y/%m/%d %H:%M:%S").*Process Ping Failed:"
PID=$(rev tt | grep "Process Ping Failed:" | grep -o ":[0-9]*)" | sed 's/://; s/)//')
sleep 1
done
[/CODE]

Am getting the same error

Thanks
Kai

Last edited by kais1; 07-30-2011 at 02:26 PM.
 
Old 07-30-2011, 02:43 PM   #8
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Hm, I just noticed that my /bin/sh is symlinked to bash. Perhaps try running it using "#!/bin/bash" or "#!/usr/bin/bash" (depending where bash is on your system). That's all I can think of, I'm afraid.

As a note: To open a code tag, it's "[ CODE ]" not "[ / CODE ]" (without spaces)
 
Old 07-30-2011, 02:55 PM   #9
kais1
Member
 
Registered: Dec 2008
Posts: 61

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by Snark1994 View Post
Hm, I just noticed that my /bin/sh is symlinked to bash. Perhaps try running it using "#!/bin/bash" or "#!/usr/bin/bash" (depending where bash is on your system). That's all I can think of, I'm afraid.

As a note: To open a code tag, it's "[ CODE ]" not "[ / CODE ]" (without spaces)
Thanks Snark,

Tried using both "#!/bin/bash" and "#!/usr/bin/bash"

[CODE]
"opmn.sh" 11 lines, 417 characters
#!/usr/bin/bash
while [[ 1 == 1 ]]; do
rev /fraud/appl/orasoa/1013/opmn/logs/opmn.log | grep "$(date +"%y/%m/%d %H:%M:%S").*Process Ping Failed:"
if [ $? ]; then
PID=$(rev /fraud/appl/orasoa/1013/opmn/logs/opmn.log | grep "Process Ping Failed:" | grep -o ":[0-9]*)" | sed 's/://; s/)//')
if kill -9 $PID > /dev/null 2>&1; then
echo "Killed $PID"
fi
fi
sleep 1
done
[CODE]

Am still getting the same error

<frdapps02]orasoa:/fraud/appl/orasoa/1013/opmn/logs> sh opmn.sh
opmn.sh: syntax error at line 4: `PID=$' unexpected


Kai

Last edited by kais1; 07-30-2011 at 03:02 PM.
 
Old 07-30-2011, 03:02 PM   #10
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Ah, but you're still calling it with "sh" - try running

Code:
bash opmn.sh
instead.

And nearly there with the code tags - I can't write it in the forum, because it goes funny about it in the forum. If you run:

Code:
printf "\x5BCODE\x5Dyour code in here\x5B/CODE\x5D\n"
then it will print out what it should look like
 
Old 07-30-2011, 03:34 PM   #11
kais1
Member
 
Registered: Dec 2008
Posts: 61

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by Snark1994 View Post
Ah, but you're still calling it with "sh" - try running

Code:
bash opmn.sh
instead.

And nearly there with the code tags - I can't write it in the forum, because it goes funny about it in the forum. If you run:

Code:
printf "\x5BCODE\x5Dyour code in here\x5B/CODE\x5D\n"
then it will print out what it should look like
Sorry to bother you again.. But am getting a different error

<frdapps02]orasoa:/fraud/appl/orasoa/1013/opmn/logs> bash opmn.sh
opmn.sh: line 3: rev: command not found
opmn.sh: line 5: rev: command not found
grep: illegal option -- o
Usage: grep -hblcnsviw pattern file . . .
opmn.sh: line 3: rev: command not found
opmn.sh: line 5: rev: command not found
grep: illegal option -- o
Usage: grep -hblcnsviw pattern file . . .
opmn.sh: line 3: rev: command not found
opmn.sh: line 5: rev: command not found
grep: illegal option -- o
Usage: grep -hblcnsviw pattern file . . .
opmn.sh: line 3: rev: command not found
opmn.sh: line 5: rev: command not found
grep: illegal option -- o
Usage: grep -hblcnsviw pattern file . . .
opmn.sh: line 3: rev: command not found
opmn.sh: line 5: rev: command not found
grep: illegal option -- o
Usage: grep -hblcnsviw pattern file . . .


Kai
 
Old 07-30-2011, 03:57 PM   #12
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
No need to apologise - I'm the one who's not being very good at giving advice, eh this now comes down to the version of utilities that you have... I use the "util-linux" package (from http://ftp://ftp.kernel.org/pub/linux/utils/util-linux/) which contains 'rev'; certainly there's a "util-linux" package available for Ubuntu and Arch (my distribution of choice), I don't know what distribution you run. For grep:
Code:
$ grep --version
grep (GNU grep) 2.9
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Mike Haertel and others, see <http://git.sv.gnu.org/cgit/grep.git/tree/AUTHORS>.
How does that compare with your version? You could maybe try 'egrep' rather than 'grep', see if your version of that supports the '-o' flag (which prints only the parts of the line which match the pattern, rather than the whole line).
 
1 members found this post helpful.
Old 07-31-2011, 07:20 PM   #13
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
From post #9
Code:
PID=$(rev /fraud/appl/orasoa/1013/opmn/logs/opmn.log | grep "Process Ping Failed:" | grep -o ":[0-9]*)" | sed 's/://; s/)//')


# summarized
PID=$( rev ...) | sed .... )..)

# unbalanced brackets
I'd be a little worried that your loop is not going to miss or dupe any instances; depends how fast it loops.
I'd base my soln on 'tail -f' or use the Perl module http://search.cpan.org/~mgrabnar/Fil...0.99.3/Tail.pm
 
2 members found this post helpful.
Old 08-01-2011, 04:07 AM   #14
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
I'm not quite sure if I misunderstand your objections... The brackets are quoted, so it's more like:
Code:
#summarized
PID=$( rev ... | grep -o "...)" | sed 's/)//')
Is this still likely to cause problems?

And it's going to keep on going from the end of the file backwards until it finds the latest match - are you just worried that it might spawn two processes which need deleting before it wraps around again? It definitely will hit the same process more than once, but I didn't think this would be an issue as it's already killed the process. However, it did strike me as remarkably inelegant - could you just demonstrate how you could feed 'tail -f' into a loop?
 
1 members found this post helpful.
Old 08-01-2011, 06:19 PM   #15
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Ok, you may be right about the brackets; its a messy line to read

Re tail -f, something like this (untested)
Code:
tail -f myfile.log|grep/awk <pattern grab pid>|kill
you get the general idea.
The -f switch means follow, so you don't need a loop.

Your orig idea would keep on trying to kill the the same process, leading to extra load on the cpu and spurious error msgs about pid nor found etc, if its quick.
If its slow, it may miss some...

The Perl module gives you a nice clean, controlled soln and you can log more info eg how many killed, how often etc.
BTW, isn't there anyway to cut down/eliminate those failures in the first place ie fix the prob rather than the symptom?
 
2 members found this post helpful.
  


Reply



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
killng parent process without killing child process (Linux C programming) lettuce84@naver.com Linux - Newbie 3 07-24-2015 08:37 AM
Killing More than one process ruud Programming 11 04-27-2010 06:30 AM
Killing a process by its name rabeea Linux - Networking 3 03-17-2005 05:30 AM
help with killing all java process idandl Linux - Newbie 2 02-02-2004 05:32 AM
Really Killing a Process! lazlow69 Linux - Newbie 14 05-10-2003 10:31 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:29 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration