You use while to run a command as long as a condition is met. In Listing 27-22 you see how while can monitor the activity of an important process.

Listing 27-22. Monitoring Process Activity with while

# usage: monitor <processname>

done logger $1 is no longer present

The body of this script consists of the command ps aux | grep $1.This command monitors for the availability of the process of which the name was entered as an argument when starting this script. As long as the process was detected, the condition is met, and the commands in the loop are executed. In this case, the script waits just one second and then repeats its action. When the process is no longer detected, the logger command writes a message to syslog.

Was this article helpful?

0 0

Post a comment