Skip to content

Instantly share code, notes, and snippets.

@benkulbertis benkulbertis/
Last active Dec 26, 2015

What would you like to do?
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…
# Usage: ./ 1234
# Or: ./ $(pidof -s someprocess)
if [ -z "$pid" ]
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
if (( EUID != 0 ))
echo "ERROR: You must be root/sudoed to shut down the system." 1>&2
exit 1
ps -p $pid > /dev/null 2>&1
if [ $? -ne 0 ]
echo "ERROR: The PID '$pid' was not found or is an invalid PID." 1>&2
exit 1
echo "The system will shutdown when process $pid ends." | wall
while ps -p $pid > /dev/null
sleep 1
sleep 5
shutdown -P now
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.