summaryrefslogtreecommitdiff
path: root/bin/autoshut.sh
diff options
context:
space:
mode:
authormoehm2014-11-23 13:56:04 +0100
committermoehm2014-11-23 13:56:04 +0100
commit21e75ab969099baf43ccb275fa8b7307f1389c62 (patch)
tree3e8207265f9c6995bfd7784fffed6a4f20a5883c /bin/autoshut.sh
parentae724c4943ed53c6b201b917db859b76e5fdb936 (diff)
parente7936e6381ae55a13a32087e4a66dab2088913a3 (diff)
downloaddotfiles-21e75ab969099baf43ccb275fa8b7307f1389c62.tar.gz
Merge branch 'master' of git.iamfabulous.de:dotfiles
Diffstat (limited to 'bin/autoshut.sh')
-rwxr-xr-xbin/autoshut.sh55
1 files changed, 55 insertions, 0 deletions
diff --git a/bin/autoshut.sh b/bin/autoshut.sh
new file mode 100755
index 0000000..8be2e56
--- /dev/null
+++ b/bin/autoshut.sh
@@ -0,0 +1,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