Skip to content

Commit 4c34c7c

Browse files
author
Andreas Poulsen
committed
Merge branch 'feature/54758_make_shutdown_and_wakeup_script_always_logout' into 'master'
Make shutdown_and_wakeup.sh always log out user See merge request os2borgerpc/os2borgerpc-scripts!187
2 parents e22727d + ab27fc3 commit 4c34c7c

File tree

2 files changed

+73
-100
lines changed

2 files changed

+73
-100
lines changed
Lines changed: 55 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,81 @@
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

4932
WAKE_PLAN_FILE=/etc/os2borgerpc/plan.json
33+
SCHEDULED_OFF_SCRIPT="/usr/local/lib/os2borgerpc/scheduled_off.sh"
5034

5135
if [ -f $WAKE_PLAN_FILE ]; then
5236
echo "Dette script kan ikke anvendes på en PC, der er tilknyttet en tænd/sluk tidsplan."
5337
exit 1
5438
fi
5539

56-
TCRON=/tmp/oldcron
57-
USERCRON=/tmp/usercron
40+
ROOTCRON_TMP=/tmp/oldcron
41+
USERCRON_TMP=/tmp/usercron
5842
MESSAGE="Denne computer lukker ned om fem minutter"
5943

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
6247

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
6351

6452
if [ "$1" == "--off" ]; then
53+
rm --force $SCHEDULED_OFF_SCRIPT
54+
else
6555

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
7060
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
7563
76-
else
64+
pkill -KILL -u user
65+
pkill -KILL -u superuser
66+
/usr/sbin/rtcwake --mode \$MODE --seconds \$DURATION
67+
EOF
7768

78-
if [ $# -gt 2 ]; then
69+
chmod 700 $SCHEDULED_OFF_SCRIPT
7970
HOURS=$1
8071
MINUTES=$2
8172
SECONDS_TO_WAKEUP=$(( 3600 * $3))
8273

8374
# If not set set it to the previous script default: off
8475
[ -z "$4" ] && MODE="off" || MODE=$4
8576

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
9377
# 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
9679

9780
MINM5P60=$(( $(( MINUTES - 5)) + 60))
9881
# Rounding minutes
@@ -101,12 +84,14 @@ else
10184
HRS=$(( HOURS - HRCORR))
10285
HRS=$(( $(( HRS + 24)) % 24))
10386
# 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
10688
else
10789
echo "Usage: shutdown_and_wakeup.sh [--off] [hours minutes] [hours]"
10890
fi
109-
11091
fi
11192

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

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 AND shutdown_and_wakeup.sh
40+
sed --in-place --expression "/shutdown/d" --expression "/rtcwake/d" --expression "/scheduled_off/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)