-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathstop-if-inactive.sh
executable file
·79 lines (76 loc) · 2.5 KB
/
stop-if-inactive.sh
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
#!/bin/bash
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
set -euo pipefail
# This script is invoked in /etc/cron.d/c9-automatic-shutdown
# which uses the full path to the file: /home/<username>/.c9/stop-if-inactive.sh
# Parse the username from the file path, which can vary based on the Cloud9's OS.
USER=$(echo $0 | cut -d'/' -f3)
CONFIG=$(cat /home/$USER/.c9/autoshutdown-configuration)
SHUTDOWN_TIMEOUT=${CONFIG#*=}
if ! [[ $SHUTDOWN_TIMEOUT =~ ^[0-9]*$ ]]; then
echo "shutdown timeout is invalid"
exit 1
fi
is_shutting_down() {
is_shutting_down_ubuntu &> /dev/null || is_shutting_down_al1 &> /dev/null || is_shutting_down_al2 &> /dev/null || is_shutting_down_al2023 &> /dev/null
}
is_shutting_down_ubuntu() {
local TIMEOUT
TIMEOUT=$(busctl get-property org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager ScheduledShutdown)
if [ "$?" -ne "0" ]; then
return 1
fi
local SHUTDOWN_TIMESTAMP
SHUTDOWN_TIMESTAMP="$(echo $TIMEOUT | awk "{print \$3}")"
if [ $SHUTDOWN_TIMESTAMP == "0" ] || [ $SHUTDOWN_TIMESTAMP == "18446744073709551615" ]; then
return 1
else
return 0
fi
}
is_shutting_down_al1() {
pgrep shutdown
}
is_shutting_down_al2() {
local FILE
FILE=/run/systemd/shutdown/scheduled
if [[ -f "$FILE" ]]; then
return 0
else
return 1
fi
}
is_shutting_down_al2023() {
local TIMEOUT
TIMEOUT=$(busctl get-property org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager ScheduledShutdown)
if [ "$?" -ne "0" ]; then
return 1
fi
local SHUTDOWN_TIMESTAMP
SHUTDOWN_TIMESTAMP="$(echo $TIMEOUT | awk "{print \$3}")"
if [ $SHUTDOWN_TIMESTAMP == "0" ] || [ $SHUTDOWN_TIMESTAMP == "18446744073709551615" ]; then
return 1
else
return 0
fi
}
is_vfs_connected() {
pgrep -f vfs-worker >/dev/null
}
is_vscode_connected() {
pgrep -u $USER -f .vscode-server/bin/ -a | grep -v -F 'shellIntegration-bash.sh' >/dev/null
}
if is_shutting_down; then
if [[ ! $SHUTDOWN_TIMEOUT =~ ^[0-9]+$ ]] || is_vfs_connected || is_vscode_connected; then
sudo shutdown -c
echo > "/home/$USER/.c9/autoshutdown-timestamp"
else
TIMESTAMP=$(date +%s)
echo "$TIMESTAMP" > "/home/$USER/.c9/autoshutdown-timestamp"
fi
else
if [[ $SHUTDOWN_TIMEOUT =~ ^[0-9]+$ ]] && ! is_vfs_connected && ! is_vscode_connected; then
sudo shutdown -h $SHUTDOWN_TIMEOUT
fi
fi