summaryrefslogtreecommitdiff
path: root/shutdownasap.in
blob: a32f92da48785c2868d8871becb97214445d101b (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/bin/bash

# shutdownasap version #VERSION#

set -e

. #ETCDIR#/shutdownasap.conf

[ -r "${pidFile}" ] && ps aux | grep -q "^\S\+\s\+$(cat "${pidFile}") " && exit

echo $$ > "${pidFile}"

quiet=false
reboot=false
while [ $# -gt 0 ]; do
  case "$1" in
    '-q')
      quiet=true
    ;;
    '-r')
      reboot=true
    ;;
    *)
      >&2 echo 'usage: shutdownasap [-q] [-r]'
      exit 1
    ;;
  esac
  shift
done

beforeWatchHook

i=1
while [ ${i} -gt 0 ] || ! additionalWatchHookSlow
do
  if [ ${i} -eq 0 ]
  then
    i=1
  fi
  sleep 1
  prozesse=$(
    ps aux \
    | grep -v grep \
    | grep -vF 'rsync --daemon --no-detach'
  )

  nochwarten=false

  if [ -d "${waitForDir}" ] && ls -1A "${waitForDir}" | grep -q "."
  then
    nochwarten=true
    if ! ${quiet}; then
      echo ".warteauf: "
      ls -1A "${waitForDir}"
    fi
  fi

  if users | grep -q "."
  then
    nochwarten=true
    if ! ${quiet}; then
      echo -n "users: "
      users
    fi
  fi

  for synActFile in /sys/block/md*/md/sync_action
  do
    if [ -r "${synActFile}" ] && ! grep -q "^idle\$" "${synActFile}"
    then
      nochWarten=true
      if ! ${quiet}; then
        echo -n "raid ${synActFile}: "
        cat "${synActFile}"
      fi
    fi
  done

  for s in "${!shutDownNoGoProcesses[@]}"
  do
    if echo "${prozesse}" | grep -q "\(\s\|/\)${shutDownNoGoProcesses[${s}]}\(\$\|\s\)"
    then
      nochwarten=true
      if ! ${quiet}; then
        echo "prozess: ${s}"
      fi
    fi
  done

  for s in "${!shutDownNoGoFiles[@]}"
  do
    if [ -e "${shutDownNoGoFiles[${s}]}" ] && echo "${prozesse}" | grep -q "^\S\+\s\+$(cat "${shutDownNoGoFiles[${s}]}")\s"
    then
      nochwarten=true
      if ! ${quiet}; then
        echo "datei: ${s}"
      fi
    fi
  done

  if ! additionalWatchHookFast
  then
    nochwarten=true
    if ! ${quiet}; then
      echo "additionalWatchHookFast"
    fi
  fi

  if ${nochwarten}
  then
    if ! ${quiet}; then
      echo "warten ..."
    fi
    i=10
    continue
  else
    if ! ${quiet}; then
      echo "noch ${i} Sekunden"
    fi
  fi
  i=$[${i}-1]
done

beforeShutDownHook

if command -v sudo >/dev/null \
&& [ "$(whoami)" != 'root' ]; then
  pre='sudo'
else
  pre=''
fi

if ${reboot}; then
  ${pre} /sbin/reboot
else
  ${pre} /sbin/poweroff
fi