1
1
#! /usr/bin/env bash
2
- # ================================================================
3
- # HEADER
4
- # ================================================================
5
- # % SYNOPSIS
6
- # + shutdown_and_wakeup.sh --args <hours> <minutes> <hours>
7
- # %
8
- # % DESCRIPTION
9
- # % This is a script to make a OS2BorgerPC machine shutdown at a certain time.
10
- # % Synopsis:
11
- # %
12
- # % shutdown_and_wakeup.sh <hours> <minutes> <hours>
13
- # %
14
- # % to enable shutdown mechanism.
15
- # %
16
- # % shutdown_and_wakeup.sh --off
17
- # %
18
- # % to disable.
19
- # %
20
- # % We'll suppose the user only wants to have regular shutdown once a day
21
- # % as specified by the <hours> and <minutes> parameters. Thus, any line in
22
- # % crontab already specifying a shutdown will be deleted before a new one is
23
- # % inserted.
24
- # % We'll also suppose the user wants the machine to wakeup after X numbers
25
- # % of hours after shutdown everyday.
26
- # %
27
- # ================================================================
28
- # - IMPLEMENTATION
29
- # - version shutdown_and_wakeup.sh (magenta.dk) 0.0.1
30
- # - author Danni Als
31
- # - copyright Copyright 2018, Magenta Aps"
32
- # - license GNU General Public License
33
-
34
- # -
35
- # ================================================================
36
- # HISTORY
37
- # 2018/12/12 : danni : Script creation - based on shutdown_at_time.sh
38
- # 2018/12/12 : danni : Changed paramter count from 2 to 3.
39
- # Corrected sed delete regex.
40
- # 2021/05/06 : mfm : Switch from sudo -u user crontab to crontab -u
41
- # user to not trigger our sudo warnings
2
+
3
+ # SYNOPSIS
4
+ # shutdown_and_wakeup.sh --args <hours> <minutes> <hours>
5
+ #
6
+ # DESCRIPTION
7
+ # This is a script to make a OS2BorgerPC machine shutdown at a certain time.
8
+ # Synopsis:
9
+ #
10
+ # shutdown_and_wakeup.sh <hours> <minutes> <hours>
11
+ #
12
+ # to enable shutdown mechanism.
42
13
#
43
- # ================================================================
44
- # END_OF_HEADER
45
- # ================================================================
14
+ # shutdown_and_wakeup.sh --off
15
+ #
16
+ # to disable.
17
+ #
18
+ # We'll suppose the user only wants to have regular shutdown once a day
19
+ # as specified by the <hours> and <minutes> parameters. Thus, any line in
20
+ # crontab already specifying a shutdown will be deleted before a new one is
21
+ # inserted.
22
+ # We'll also suppose the user wants the machine to wakeup after X numbers
23
+ # of hours after shutdown everyday.
24
+ #
25
+ # IMPLEMENTATION
26
+ # author Danni Als
27
+ # copyright Copyright 2018, Magenta Aps"
28
+ # license GNU General Public License
46
29
47
30
set -x
48
31
49
32
WAKE_PLAN_FILE=/etc/os2borgerpc/plan.json
33
+ SCHEDULED_OFF_SCRIPT=" /usr/local/lib/os2borgerpc/scheduled_off.sh"
50
34
51
35
if [ -f $WAKE_PLAN_FILE ]; then
52
36
echo " Dette script kan ikke anvendes på en PC, der er tilknyttet en tænd/sluk tidsplan."
53
37
exit 1
54
38
fi
55
39
56
- TCRON =/tmp/oldcron
57
- USERCRON =/tmp/usercron
40
+ ROOTCRON_TMP =/tmp/oldcron
41
+ USERCRON_TMP =/tmp/usercron
58
42
MESSAGE=" Denne computer lukker ned om fem minutter"
59
43
60
- crontab -l > $TCRON
61
- crontab -u user -l > $USERCRON
44
+ # Read and save current cron settings first
45
+ crontab -l > $ROOTCRON_TMP
46
+ crontab -u user -l > $USERCRON_TMP
62
47
48
+ # Delete current crontab entries related to this script AND shutdown_at_time
49
+ sed --in-place --expression " /rtcwake/d" --expression " /scheduled_off/d" --expression " /shutdown/d" $ROOTCRON_TMP
50
+ sed --in-place " /lukker/d" $USERCRON_TMP
63
51
64
52
if [ " $1 " == " --off" ]; then
53
+ rm --force $SCHEDULED_OFF_SCRIPT
54
+ else
65
55
66
- if [ -f $TCRON ] ; then
67
- sed -i -e " /\/rtcwake/d " $TCRON
68
- crontab $TCRON
69
- fi
56
+ # If not called with --off: Determine the new crontab contents
57
+ if [ $# -gt 2 ] ; then
58
+ cat << EOF > $SCHEDULED_OFF_SCRIPT
59
+ #!/usr/bin/env bash
70
60
71
- if [ -f $USERCRON ]; then
72
- sed -i -e " /lukker/d" $USERCRON
73
- crontab -u user $USERCRON
74
- fi
61
+ MODE=\$ 1
62
+ DURATION=\$ 2
75
63
76
- else
64
+ pkill -KILL -u user
65
+ pkill -KILL -u superuser
66
+ /usr/sbin/rtcwake --mode \$ MODE --seconds \$ DURATION
67
+ EOF
77
68
78
- if [ $# -gt 2 ] ; then
69
+ chmod 700 $SCHEDULED_OFF_SCRIPT
79
70
HOURS=$1
80
71
MINUTES=$2
81
72
SECONDS_TO_WAKEUP=$(( 3600 * $3 ))
82
73
83
74
# If not set set it to the previous script default: off
84
75
[ -z " $4 " ] && MODE=" off" || MODE=$4
85
76
86
- # We still remove shutdown lines, if any
87
- if [ -f $TCRON ]; then
88
- sed -i -e " /\/rtcwake/d" $TCRON
89
- fi
90
- if [ -f $USERCRON ]; then
91
- sed -i -e " /lukker/d" $USERCRON
92
- fi
93
77
# Assume the parameters are already validated as integers.
94
- echo " $MINUTES $HOURS * * * /usr/sbin/rtcwake --mode $MODE --seconds $SECONDS_TO_WAKEUP " >> $TCRON
95
- crontab $TCRON
78
+ echo " $MINUTES $HOURS * * * $SCHEDULED_OFF_SCRIPT $MODE $SECONDS_TO_WAKEUP " >> $ROOTCRON_TMP
96
79
97
80
MINM5P60=$(( $(( MINUTES - 5 )) + 60))
98
81
# Rounding minutes
@@ -101,12 +84,14 @@ else
101
84
HRS=$(( HOURS - HRCORR))
102
85
HRS=$(( $(( HRS + 24 )) % 24))
103
86
# Now output to user's crontab as well
104
- echo " $MINS $HRS * * * XDG_RUNTIME_DIR=/run/user/\$ (id -u) /usr/bin/notify-send \" $MESSAGE \" " >> $USERCRON
105
- crontab -u user $USERCRON
87
+ echo " $MINS $HRS * * * XDG_RUNTIME_DIR=/run/user/\$ (id -u) /usr/bin/notify-send \" $MESSAGE \" " >> $USERCRON_TMP
106
88
else
107
89
echo " Usage: shutdown_and_wakeup.sh [--off] [hours minutes] [hours]"
108
90
fi
109
-
110
91
fi
111
92
112
- rm --force $TCRON
93
+ # Update crontabs accordingly - either with an empty crontab or updated ones
94
+ crontab $ROOTCRON_TMP
95
+ crontab -u user $USERCRON_TMP
96
+
97
+ rm --force $ROOTCRON_TMP $USERCRON_TMP
0 commit comments