LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 04-02-2009, 06:58 PM   #1
MarkGM
LQ Newbie
 
Registered: Apr 2009
Posts: 2

Rep: Reputation: 0
[bash] if statements in a loop


I'm trying to scan the process list for multiple things, and if one of those things is found then I want it to log the entry and kill it. This is what I have so far, but its unacceptable because it wont identify which one of the processes was found, only that ONE was found.

Code:
function myfunc() {

process1=`ps aux | grep process1 | grep -v grep | awk '{print $2}' | sort`
process2=`ps aux | grep process2 | grep -v grep | awk '{print $2}' | sort`

if [ $process1 ] && [ process2 ]; then
kill -9 $process1
kill -9 $process2
echo "Process found and killed" >> /var/log/test.log
else
myfunc
fi

sleep 10
myfunc
}

myfunc
As you can see, if ONE of the processes are found then it attempts to kill both. Basically it serves as an active scanner for the processes I define. I want it to search for the processes I tell it to and then kill the process and log WHAT process it found ("Process 1 was found and killed" or "Process 2 was found and killed")

It also needs to be a loop so the script can continue to scan even after its found its target and killed it.

Thanks for reading, hopefully you guys can help
 
Old 04-02-2009, 07:38 PM   #2
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 97
Firstly, the if-statement as you have given it will only return true if both variables have been assigned. To execute the then block if either are assigned, the tests should be:

Code:
if [ $process1 ] || [ $process2 ]; then
A continuous loop can be done in many ways, for example:

Code:
while [ 1 = 1 ]; do
#code goes here
done
To execute one command if-and-only-if another succeeds, you can use command1 && command2:
Code:
rob:~$ gedit &
[1] 9366
rob:~$ process1=""
rob:~$ process2=9366
rob:~$ kill -9 $process1 2>/dev/null && echo Process $process1 killed
rob:~$ kill -9 $process2 2>/dev/null && echo Process $process2 killed
Process 9366 killed
The append operator ( >> ) could then be used to redirect output to a log file.

Hope this helps,
Rob
 
Old 04-02-2009, 07:46 PM   #3
MarkGM
LQ Newbie
 
Registered: Apr 2009
Posts: 2

Original Poster
Rep: Reputation: 0
Code:
function myfunc() {

process1=`ps aux | grep process1 | grep -v grep | awk '{print $2}' | sort`
process2=`ps aux | grep process2 | grep -v grep | awk '{print $2}' | sort`
process3=`ps aux | grep process3 | grep -v grep | awk '{print $2}' | sort`

if [ $process1 ] || [ $process2 ] || [ $process3 ]; then
kill -9 $process1 && echo "process 1 killed" >> /var/log/test.log
kill -9 $process2 && echo "process 2 killed" >> /var/log/test.log
kill -9 $process3 && echo "process 3 killed" >> /var/log/test.log
else
myfunc
fi

sleep 10
myfunc
}

myfunc
Let's say process1 was found, the script will only attempt to kill process1 right? I don't want it to try to kill process2 or process3 if only process1 was found. That way we can log what process was found.

Thanks
 
Old 04-02-2009, 08:12 PM   #4
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 97
Quote:
Originally Posted by MarkGM View Post
Let's say process1 was found, the script will only attempt to kill process1 right? I don't want it to try to kill process2 or process3 if only process1 was found. That way we can log what process was found.

Thanks
This way, it will try to kill each process, but fail if the variable is not set (i.e., there is no process id to kill). This would normally return an error (which is why I added 2>/dev/null to throw away error messages). However, if kill fails it will return exit value 1 (an error state, logically false) so the statement after the && operator will not be executed. Something will only be logged if kill succeeds (exits with value 0).

To only attempt to kill the process if it exists, you could use:
Code:
[ $process1 ] && kill -9 $process1 && echo Process $process1 killed >> /var/log/test.log
Actually, that is better than my first suggestion. No need for the if statement, You would need also to unset the variables at the bottom of the loop block (something I forgot in my first answer).

Code:
unset process1
 
  


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
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
bash loop within a loop for mysql ops br8kwall Programming 10 04-30-2008 03:50 AM
BASH IF statements Part II kinetik Programming 4 05-07-2006 01:18 AM
Do/while loop and if statements not functioning properly w/ array... ohfaney Programming 5 05-02-2006 02:22 AM
Newbie troubles with Bash if/then statements jimieee Programming 4 12-04-2003 06:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:17 AM.

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