Skip to content

Instantly share code, notes, and snippets.

@benkulbertis
Last active December 26, 2015 17:29
Show Gist options
  • Save benkulbertis/7187800 to your computer and use it in GitHub Desktop.
Save benkulbertis/7187800 to your computer and use it in GitHub Desktop.
This is a little bash script that will shut down the system when a process id exits. Just pass the relevant process id as an argument. It requires root/sudo to shutdown the system. The program Cuttlefish used to be able to do this but is now unmaintained. The kde program kshutdown can do this but requires a lot of dependencies on non-kde systems…
#!/bin/bash
# Usage: ./pshutdown.sh 1234
# Or: ./pshutdown.sh $(pidof -s someprocess)
pid=$1
if [ -z "$pid" ]
then
echo "This script shuts down the system when a certain process exits. Please pass a process id as an argument to shut the system down when it exits." 1>&2
exit 1
fi
if (( EUID != 0 ))
then
echo "ERROR: You must be root/sudoed to shut down the system." 1>&2
exit 1
fi
ps -p $pid > /dev/null 2>&1
if [ $? -ne 0 ]
then
echo "ERROR: The PID '$pid' was not found or is an invalid PID." 1>&2
exit 1
fi
echo "The system will shutdown when process $pid ends." | wall
while ps -p $pid > /dev/null
do
sleep 1
done
sleep 5
shutdown -P now
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment