Skip to content

Instantly share code, notes, and snippets.

@jcook793
Created August 14, 2013 03:40
Show Gist options
  • Save jcook793/6227813 to your computer and use it in GitHub Desktop.
Save jcook793/6227813 to your computer and use it in GitHub Desktop.
Checks the process list every 5 seconds for a new process. If it finds one, sends an email.
#!/bin/bash
MASTER_PROCESS_FILE=/tmp/master_process_list.out
OUT_FILE=/tmp/process_list.out
SLEEP_TIME=5s
RECIPIENTS=me@nobody.com
ps -wweo pid,euser,ruser,suser,fuser,comm,args | grep -v "ps -wweo pid,euser,ruser,suser,fuser,comm,args" | grep -v `basename $0` > ${MASTER_PROCESS_FILE}
while true; do
ps -wweo pid,euser,ruser,suser,fuser,comm,args | grep -v "ps -wweo pid,euser,ruser,suser,fuser,comm,args" | grep -v `basename $0` > ${OUT_FILE}
new_procs=`comm -13 ${MASTER_PROCESS_FILE} ${OUT_FILE}`
DATE=`date`
if [ -n "${new_procs}" ]; then
ps_header=`ps -wweo pid,euser,ruser,suser,fuser,comm,args | head -1`
echo "${DATE} - New processes!"
echo "${ps_header}"
echo "${new_procs}"
host=`hostname`
echo "<html><body><pre>${ps_header}
${new_procs}</pre></body></html>" | mail -s "$(echo -e "New process on ${host}\nContent-Type: text/html")" "${RECIPIENTS}"
sleep 20s # give mail a chance to clean up
else
echo "${DATE} - No new processes"
fi
mv ${OUT_FILE} ${MASTER_PROCESS_FILE}
sleep ${SLEEP_TIME}
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment