Process monitoring script I wrote a script which logs memory usage of a process (pid in file), and reacts in case a limit is reached. I run the script nightly. In my case, the process concerned propably has a memory leak and locks up the whole system after a couple of days. Hence, the script should reboot the system if excess memory is used. Logged memory usage values are formated to have 1 decimal place.

The code works, but I'd like to know if and how I could optimize it, and if there are no mistakes.

#!/bin/sh

# process_monitor.sh
#
# monitor specific process
# customized for memory usage monitor (can easily be changed)
# customized to perform system reboot if limit is exceeded (can easily be changed)
# by geohei &lt;xxxx@xxxx.xx&gt;
# created : 14.10.2016
# revised : n/a
#
# use cron to trigger (e.g. nightly at 04:15 '15 4 * * * ~/process_monitor.sh'
# /bin/bc is used (possibly not installed by default)
#
# $pidfile must include path
# $logpath is used to save 'process_monitor.log' file (omit trailing '/')
# $pidlimit can be floating point (e.g. 14.1)

pidfile='/tmp/process.pid'
logpath='~'
pidlimit=20.0

pidvalue=$(ps -p $(eval "cat $echo $pidfile") -o %mem --no-headers)

if [ "$(echo "${pidvalue} &gt; ${pidlimit}" | bc)" -eq 1 ]; then
    # log $pidvalue, $pidlimit and reboot system
    echo "$(date '+%Y-%m-%d %H:%M:%S')   pidvalue: $pidvalue, pidlimit: $(echo $pidlimit | awk '{printf "%.1f\n", int($1)}') - reboot" &gt;&gt; $logpath/process_monitor.log
    shutdown -r -t 10
    exit 1
else
    # log $pidvalue, $pidlimit
    echo "$(date '+%Y-%m-%d %H:%M:%S')   pidvalue: $pidvalue, pidlimit: $pidlimit" &gt;&gt; $logpath/process_monitor.log
fi

exit 0


That's how the log looks like:

...
2016-10-14 04:15:04   pidvalue: 19.2, pidlimit: 20.0
2016-10-15 04:15:03   pidvalue: 20.4, pidlimit: 20.0 - reboot
...


Any remarks, comments or suggestions?
Don't write custom scripts for common sysadmin tasks. Using the proper tools will get the job done better, and the solution will be more maintainable — especially by any colleagues you might have.

Using monit, for example, this would just be a matter of writing a simple configuration file.

set logfile /root/process_monitor.log
check process myproc with pidfile /tmp/pidfile.pid
    restart program = "shutdown -r -t 10"  # draconian!
    every "15 4 * * *"
    if memory &gt; 20 MB then restart myproc