-
Notifications
You must be signed in to change notification settings - Fork 0
/
shutdown-and-wakeup-with-weekend.sh
101 lines (83 loc) · 2.56 KB
/
shutdown-and-wakeup-with-weekend.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env bash
# Script to control shutdown once every day.
# Different settings can be applied for weekdays and weekends.
# You can not make the computer "sleep all weekend".
# The computer has to wake up every day - at least for one hour.
#
# Script takes six arguments.
# Arguments need to be given in the right order.
#
# Arguments:
# weekday-shutdown-hours
# weekday-shutdown-minutes
# weekday-number-of-hours-to-sleep
# weekend-shutdown-hours
# weekend-shutdown-minutes
# weekend-number-of-hours-to-sleep
# Any questions? Contact Agnete Moos, [email protected]
TCRON=/tmp/oldcron
USERCRON=/tmp/usercron
MESSAGE="Denne computer lukker ned om fem minutter"
crontab -l > $TCRON
sudo -u user crontab -l > $USERCRON
if [ "$1" == "--off" ]
then
if [ -f $TCRON ]
then
sed -i -e "/\/rtcwake/d" $TCRON
crontab $TCRON
fi
if [ -f $USERCRON ]
then
sed -i -e "/lukker/d" $USERCRON
sudo -u user crontab $USERCRON
fi
else
if [ $# == 6 ]
then
HOURS_WEEKDAY=$1
MINUTES_WEEKDAY=$2
SECONDS_TO_WAKEUP_WEEKDAY=$(expr 3600 \* $3)
HOURS_WEEKEND=$4
MINUTES_WEEKEND=$5
SECONDS_TO_WAKEUP_WEEKEND=$(expr 3600 \* $6)
# We still remove shutdown lines, if any
if [ -f $TCRON ]
then
sed -i -e "/\/rtcwake/d" $TCRON
fi
if [ -f $USERCRON ]
then
sed -i -e "/lukker/d" $USERCRON
fi
# Assume the parameters are already validated as integers.
echo "$MINUTES_WEEKDAY $HOURS_WEEKDAY * * 1-5 /usr/sbin/rtcwake -m off -s $SECONDS_TO_WAKEUP_WEEKDAY" >> $TCRON
echo "$MINUTES_WEEKEND $HOURS_WEEKEND * * 6,0 /usr/sbin/rtcwake -m off -s $SECONDS_TO_WAKEUP_WEEKEND" >> $TCRON
crontab $TCRON
#WEEKDAYS
MINM5P60=$(expr $(expr $MINUTES_WEEKDAY - 5) + 60)
# Rounding minutes
MINS=$(expr $MINM5P60 % 60)
HRCORR=$(expr 1 - $(expr $MINM5P60 / 60))
HRS=$(expr $HOURS_WEEKDAY - $HRCORR)
HRS=$(expr $(expr $HRS + 24) % 24)
# Now output to user's crontab as well
echo "$MINS $HRS * * 1-5 DISPLAY=:0.0 /usr/bin/notify-send \"$MESSAGE\"" >> $USERCRON
#WEEKENDS
MINM5P60=$(expr $(expr $MINUTES_WEEKEND - 5) + 60)
# Rounding minutes
MINS=$(expr $MINM5P60 % 60)
HRCORR=$(expr 1 - $(expr $MINM5P60 / 60))
HRS=$(expr $HOURS_WEEKEND - $HRCORR)
HRS=$(expr $(expr $HRS + 24) % 24)
# Now output to user's crontab as well
echo "$MINS $HRS * * 6,0 DISPLAY=:0.0 /usr/bin/notify-send \"$MESSAGE\"" >> $USERCRON
sudo -u user crontab $USERCRON
else
echo "Please read the instructions on how to use the script."
fi
fi
if [ -f $TCRON ]
then
rm $TCRON
fi