I gave it a try. I created a script that can be installed as a cron job and I will paste it at the end of the post. You can try to use it at your own risk.
Mini manual: The script will kill all the stray processes for a particular user on a machine, the user has logged out. Additionally the script would not kill the user processes in case it detects presence of a optional specified file in the command line (helpful if the user has automated processes). I'd recommend it to execute it periodically as a root user, in its crontab for example. Executing the script without command line arguments will result in a display of its usage.
Installation (ask a technical person if unsure): Copy the script source below and paste it in some script file, say as /usr/local/bin/kill_stray_processes, then give it executable rights. From the terminal it would look like something similar:
# Create /usr/local/bin in case it doesn't exist
sudo mkdir -p /usr/local/bin
# Use your favourite editor to paste the source of the script, here for example vi
sudo vi /usr/local/bin/kill_stray_processes
# After pasting the source, saving and closing the editor, set executable rights of the script
sudo chmod a+x /usr/local/bin/kill_stray_processes
The cron job to check and kill stray processes for user myuser every hour would look like this:
0 * * * * /usr/local/bin/kill_stray_processes myuser
Suppose an automated process creates temporarily a file /tmp/mylock and you don't want to kill processes at the time it runs. The cron job would look like this:
0 * * * * /usr/local/bin/kill_stray_processes myuser /tmp/mylock
The source of the script starts below (no empty line in the beginning):
#!/bin/bash
# SYNOPSIS:
# This is a command line utility that would kill processes belonging
# to a particular user which does not have login session. Additionally
# the script can take as a command line argument files which when present
# can cause it not to proceed with killing the stray processes.
#
# USAGE:
# kill_stray_user_processes <user> [<lock_file>...]
#
COMMAND=$0
COMMAND_PATH=`dirname $COMMAND`
pushd . > /dev/null
cd $COMMAND_PATH
COMMAND_PATH=`pwd`
COMMAND_NAME=`basename $0`
popd > /dev/null
#
USER="$1"
if [ -z "$USER" ]; then
echo "USAGE: $COMMAND_NAME <user> [<lock_file>...]"
exit 0
fi
shift 1
# In case of specified lock files check for their presence and exit if any
while [ -n "$1" ]; do
if [ -f "$1" ]; then
exit 0
fi
shift 1
done
# Make sure the user has logged out
if w | grep -q -i "^${USER}[[:space:]]"; then
exit 0
fi
# Kill the stray launchd's and all their subprocesses
for pid in `ps -jaxwww | grep -i "^${USER}[[:space:]]" | grep "/sbin/launchd" | awk '{print $2}'`; do
bash -c "kill -- -${pid}"
done
# Try to kill gracefully everything else
for pid in `ps -jaxwww | grep -i "^${USER}[[:space:]]" | awk '{print $2}'`; do
bash -c "kill -- -${pid}" 2> /dev/null
done
# Kill forcefully everything that has left
for pid in `ps -jaxwww | grep -i "^${USER}[[:space:]]" | awk '{print $2}'`; do
kill -9 $pid 2> /dev/null
done