LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   [bash] if statements in a loop (https://www.linuxquestions.org/questions/programming-9/%5Bbash%5D-if-statements-in-a-loop-716452/)

MarkGM 04-02-2009 06:58 PM

[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 :)

Robhogg 04-02-2009 07:38 PM

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

MarkGM 04-02-2009 07:46 PM

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

Robhogg 04-02-2009 08:12 PM

Quote:

Originally Posted by MarkGM (Post 3496671)
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


All times are GMT -5. The time now is 10:30 PM.