summaryrefslogtreecommitdiff
path: root/bin/autoshut.sh
blob: 8be2e56ab62e1718351dae3f9971a0a53df60fc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/bin/bash

function idle {
	prog="$1"
	while pgrep "$prog" >/dev/null 2>&1; do
		sleep 3
	done
	shutdown -hP now
}

usage="Usage: $0 [-d] [-p PROCESS]"
function printhelp {
	echo "A small shellscript which shutdowns your computer when a specific process ends"
	echo $usage
	echo "-d		daemonize into the background"
	echo "-p PROCESS	commit the process name for which we are waiting to finish"
	exit
}

prog=""
daemon=false

while [ $# -gt 0 ]; do
	if [ "$1" == "-p" ]; then
		shift
		prog="$1"
		shift
	elif [ "$1" == "-d" ]; then
		daemon=true
		shift
	elif [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
		printhelp
	else
		echo "Unsupported argument '$1'. Try '-h' for more help." 1>&2
		exit 1
	fi
done

if [ -z "$prog" ] || [ "$prog" == "" ]; then
	echo "I'm confused. Which programm shall I monitor? Tell me, master!" 1>&2
	echo $usage
	exit 1
fi

if [ $UID != 0 ]; then
	read -p "We aren't root! Are you sure you have the proper permissions? If yes, press any key to continue or CTRL+C to abort. " null
fi

if [ $daemon == true ]; then
	echo "Daemonizing..."
	idle "$prog" >/dev/null 2>&1 &
	disown
else
	idle "$prog" >/dev/null 2>&1
fi