Skip to content

Commit 0b22e8d

Browse files
mfxaMarcus Funch
authored andcommitted
Rewrite and simplify shutdown_and_wakeup.sh and shutdown_at_time.sh
1 parent 142bea9 commit 0b22e8d

File tree

2 files changed

+60
-104
lines changed

2 files changed

+60
-104
lines changed
Lines changed: 42 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,31 @@
11
#!/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.
4213
#
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
4629

4730
set -x
4831

@@ -54,31 +37,23 @@ if [ -f $WAKE_PLAN_FILE ]; then
5437
exit 1
5538
fi
5639

57-
TCRON=/tmp/oldcron
58-
USERCRON=/tmp/usercron
40+
ROOTCRON_TMP=/tmp/oldcron
41+
USERCRON_TMP=/tmp/usercron
5942
MESSAGE="Denne computer lukker ned om fem minutter"
6043

61-
crontab -l > $TCRON
62-
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
6347

48+
# Delete current crontab entries related to this script
49+
sed --in-place --expression "/rtcwake/d" --expression "/scheduled_off/d" $ROOTCRON_TMP
50+
sed --in-place "/lukker/d" $USERCRON_TMP
6451

6552
if [ "$1" == "--off" ]; then
66-
67-
if [ -f $TCRON ]; then
68-
sed -i -e "/\/rtcwake/d" $TCRON
69-
sed -i "/scheduled_off/d" $TCRON
70-
crontab $TCRON
71-
fi
72-
73-
if [ -f $USERCRON ]; then
74-
sed -i -e "/lukker/d" $USERCRON
75-
crontab -u user $USERCRON
76-
fi
77-
7853
rm --force $SCHEDULED_OFF_SCRIPT
79-
8054
else
8155

56+
# If not called with --off: Determine the new crontab contents
8257
if [ $# -gt 2 ]; then
8358
cat <<EOF > $SCHEDULED_OFF_SCRIPT
8459
#!/usr/bin/env bash
@@ -99,17 +74,8 @@ EOF
9974
# If not set set it to the previous script default: off
10075
[ -z "$4" ] && MODE="off" || MODE=$4
10176

102-
# We still remove shutdown lines, if any
103-
if [ -f $TCRON ]; then
104-
sed -i -e "/\/rtcwake/d" $TCRON
105-
sed -i "/scheduled_off/d" $TCRON
106-
fi
107-
if [ -f $USERCRON ]; then
108-
sed -i -e "/lukker/d" $USERCRON
109-
fi
11077
# Assume the parameters are already validated as integers.
111-
echo "$MINUTES $HOURS * * * $SCHEDULED_OFF_SCRIPT $MODE $SECONDS_TO_WAKEUP" >> $TCRON
112-
crontab $TCRON
78+
echo "$MINUTES $HOURS * * * $SCHEDULED_OFF_SCRIPT $MODE $SECONDS_TO_WAKEUP" >> $ROOTCRON_TMP
11379

11480
MINM5P60=$(( $(( MINUTES - 5)) + 60))
11581
# Rounding minutes
@@ -118,12 +84,14 @@ EOF
11884
HRS=$(( HOURS - HRCORR))
11985
HRS=$(( $(( HRS + 24)) % 24))
12086
# Now output to user's crontab as well
121-
echo "$MINS $HRS * * * XDG_RUNTIME_DIR=/run/user/\$(id -u) /usr/bin/notify-send \"$MESSAGE\"" >> $USERCRON
122-
crontab -u user $USERCRON
87+
echo "$MINS $HRS * * * XDG_RUNTIME_DIR=/run/user/\$(id -u) /usr/bin/notify-send \"$MESSAGE\"" >> $USERCRON_TMP
12388
else
12489
echo "Usage: shutdown_and_wakeup.sh [--off] [hours minutes] [hours]"
12590
fi
126-
12791
fi
12892

129-
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

os2borgerpc/os2borgerpc/shutdown_at_time.sh

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,54 +28,42 @@ if [ -f $WAKE_PLAN_FILE ]; then
2828
exit 1
2929
fi
3030

31-
TCRON=/tmp/oldcron
32-
USERCRON=/tmp/usercron
31+
ROOTCRON_TMP=/tmp/oldcron
32+
USERCRON_TMP=/tmp/usercron
3333
MESSAGE="Denne computer lukker ned om fem minutter"
3434

35-
crontab -l > $TCRON
36-
crontab -u user -l > $USERCRON
35+
# Read and save current cron settings first
36+
crontab -l > $ROOTCRON_TMP
37+
crontab -u user -l > $USERCRON_TMP
3738

39+
# Delete current crontab entries related to this script
40+
sed --in-place "/shutdown/d" $ROOTCRON_TMP
41+
sed --in-place "/lukker/d" $USERCRON_TMP
3842

39-
if [ "$1" == "--off" ]; then
40-
41-
if [ -f $TCRON ]; then
42-
sed -i -e "/\/sbin\/shutdown/d" $TCRON
43-
crontab $TCRON
44-
fi
45-
46-
if [ -f $USERCRON ]; then
47-
sed -i -e "/lukker/d" $USERCRON
48-
crontab -u user $USERCRON
49-
fi
50-
51-
else
43+
# If not called with --off: Determine the new crontab contents
44+
if [ "$1" != "--off" ]; then
5245

5346
if [ $# == 2 ]; then
5447
HOURS=$1
5548
MINUTES=$2
56-
# We still remove shutdown lines, if any
57-
if [ -f $TCRON ]; then
58-
sed -i -e "/\/sbin\/shutdown/d" $TCRON
59-
fi
60-
if [ -f $USERCRON ]; then
61-
sed -i -e "/lukker/d" $USERCRON
62-
fi
6349
# Assume the parameters are already validated as integers.
64-
echo "$MINUTES $HOURS * * * /sbin/shutdown -P now" >> $TCRON
65-
crontab $TCRON
50+
echo "$MINUTES $HOURS * * * /sbin/shutdown -P now" >> $ROOTCRON_TMP
6651

6752
MINM5P60=$(( $(( MINUTES - 5)) + 60))
6853
# Rounding minutes
69-
MINS=$((MINM5P60 % 60))
54+
MINS=$(( MINM5P60 % 60))
7055
HRCORR=$(( 1 - $(( MINM5P60 / 60))))
7156
HRS=$(( HOURS - HRCORR))
7257
HRS=$(( $(( HRS + 24)) % 24))
7358
# Now output to user's crontab as well
74-
echo "$MINS $HRS * * * XDG_RUNTIME_DIR=/run/user/\$(id -u) /usr/bin/notify-send \"$MESSAGE\"" >> $USERCRON
75-
crontab -u user $USERCRON
59+
echo "$MINS $HRS * * * XDG_RUNTIME_DIR=/run/user/\$(id -u) /usr/bin/notify-send \"$MESSAGE\"" >> $USERCRON_TMP
7660
else
7761
echo "Usage: shutdown_at_time.sh [--off] [hours minutes]"
7862
fi
7963
fi
8064

81-
rm --force $TCRON
65+
# Update crontabs accordingly - either with an empty crontab or updated ones
66+
crontab $ROOTCRON_TMP
67+
crontab -u user $USERCRON_TMP
68+
69+
rm --force $ROOTCRON_TMP $USERCRON_TMP

0 commit comments

Comments
 (0)