Originally Posted by MarkGM
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.
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:
[ $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).