All too often a program or script will die, hang, or error out during execution or fail to even start up. This type of problem can be hard to nail down due to the unpredictable behavior and the timing required to catch the event as it happens. We also sometimes want to execute some commands before a process starts, as the process starts (or as the monitoring starts), or as a post event when the process dies. Timing is everything! Instead of reentering the same command over and over to monitor a process, we can write scripts to wait for a process to start or end and record the timestamps, or we can perform some other function as a pre-startup or post-event. To monitor the process we are going to use grep to grab one or more matched patterns from the process list output. Because we are going to use grep, there is a need for the process to be unique in some way — for example, by process name, user name, process ID (PID), parent process ID (PPID), or even a date/time.
In this chapter we cover four scripts that do the following: