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 <[email protected]>
# 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} > ${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" >> $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" >> $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?