#!/bin/bash

# Grep to find out whether there's X process running and user logged in

# Is Desktop running on this machine? Anything greater than 0 means yes
DESKTOPRUNNING=`/opt/slurm-16.05.4/bin/squeue | grep desktop | grep -c $HOSTNAME`

# Is X running? Anything greater than 0 means yes
XRUNNING=`ps aux | grep "/usr/bin/X" | grep -c root`

# This checks if no desktop is running but X is still there
if [ "$DESKTOPRUNNING" == 0 ] && [ "XRUNNING" > 0 ]
then
        # Trying to get XPID
	if [ -z "$XPID" ]
	then
		XPID=`ps -ef | grep "/usr/bin/X" | grep root | awk '{print $2}'`
		kill $XPID > /dev/null 2>&1 
        fi
fi
