Skip to content

Commit

Permalink
Add project
Browse files Browse the repository at this point in the history
  • Loading branch information
redno2 committed Aug 28, 2023
1 parent 64b4c7d commit 63b8f3b
Show file tree
Hide file tree
Showing 13 changed files with 309 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pc-power-scheduler/DEBIAN/control
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Package: pc-power-scheduler
Version: 0.0.11
Section: utils
Priority: standard
Architecture: amd64
Depends: ethtool, gettext-base, gawk
Homepage: https://github.com/swissquote/pc-power-scheduler
Maintainer: Raphael Pretot <[email protected]>
Description: A daemon to schedule your shutdown and wake up your personal desktop
44 changes: 44 additions & 0 deletions pc-power-scheduler/DEBIAN/postinst
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env bash

SOURCE_DIR=/usr/share/pc-power-scheduler

mkdir -p /usr/local/sbin/

# Set default configuration
if [[ ! -f /etc/default/pc-power-scheduler ]]; then
cp $SOURCE_DIR/pc-power-scheduler /etc/default/
fi

# Set systemd services
cp $SOURCE_DIR/shutdown.service /etc/systemd/system/
cp $SOURCE_DIR/wakeup.service /etc/systemd/system/
cp $SOURCE_DIR/pc-power-scheduler.path /etc/systemd/system/
cp $SOURCE_DIR/pc-power-scheduler.service /etc/systemd/system/


# Set bin
cp $SOURCE_DIR/shutdown-popup /usr/local/sbin/
chmod +x /usr/local/sbin/shutdown-popup

cp $SOURCE_DIR/set-wakeup-time /usr/local/sbin/set-wakeup-time
chmod +x /usr/local/sbin/set-wakeup-time

cp $SOURCE_DIR/pc-power-scheduler-service-updater /usr/local/sbin/pc-power-scheduler-service-updater
chmod +x /usr/local/sbin/pc-power-scheduler-service-updater
bash $SOURCE_DIR/pc-power-scheduler-service-updater

# Manage systemd services
systemctl daemon-reload

systemctl disable shutdown.service
systemctl enable shutdown.timer
systemctl start shutdown.timer

systemctl enable wakeup.service
systemctl start wakeup.service

systemctl enable pc-power-scheduler.service
systemctl start pc-power-scheduler.service
systemctl enable pc-power-scheduler.path
systemctl start pc-power-scheduler.path
#systemctl enable wol.service
53 changes: 53 additions & 0 deletions pc-power-scheduler/DEBIAN/postrm
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env bash

function clean_service() {
service=$1
systemctl stop $service
systemctl disable $service
rm /etc/systemd/system/$service
}

if [[ -e /etc/systemd/system/shutdown.service ]]; then
clean_service shutdown.service
fi

if [[ -e /etc/systemd/system/shutdown.timer ]]; then
clean_service shutdown.timer
fi

if [[ -e /etc/systemd/system/wol.service ]]; then
clean_service wol.service
fi

if [[ -e /usr/local/sbin/shutdown-popup ]]; then
rm /usr/local/sbin/shutdown-popup
fi

if [[ -e /usr/local/sbin/set-wakeup-time ]]; then
rm /usr/local/sbin/set-wakeup-time
fi

if [[ -e /usr/local/sbin/pc-power-scheduler-service-updater ]]; then
rm /usr/local/sbin/pc-power-scheduler-service-updater
fi

if [[ -e /etc/systemd/system/wakeup.service ]]; then
clean_service wakeup.service
fi

if [[ -e /etc/systemd/system/pc-power-scheduler.path ]]; then
clean_service pc-power-scheduler.path
fi

if [[ -e /etc/systemd/system/pc-power-scheduler.service ]]; then
clean_service pc-power-scheduler.service
fi

if [ "$1" = purge ]; then
if [[ -e /etc/default/pc-power-scheduler ]]; then
rm /etc/default/pc-power-scheduler
fi
fi

systemctl daemon-reload
systemctl reset-failed
22 changes: 22 additions & 0 deletions pc-power-scheduler/usr/share/pc-power-scheduler/pc-power-scheduler
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## This is the configuration file for pc-power-scheduler daemon

# Enable or disable power management (disable shutdown and wakeup actions)
# [ True, False ]
ENABLE='True'

# The method you want to use it for the halt
# [ poweroff, suspend, hibernate ]
METHOD='poweroff'

# Which day you want to enable the automatic shutdown
# locale's abbreviated weekday name, separate by comma (e.g., 'Sun,Tue')
DAYS='Mon,Tue,Wed,Thu,Fri'

# Specify the time you want to shutdown up your computer
# Time format HH:MM
SHUTDOWNTIME='19:30'

# Specify the time you want to wake up your computer (the day after the shutdown)
# Time format HH:MM
WAKEUPTIME='07:00'

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash
# update the daemon from the default config file /etc/default/pc-power-scheduler

SHUTDOWN_WOL_PATH='/usr/share/pc-power-scheduler'
SERVICE_PATH='/etc/systemd/system/'

set -a
. /etc/default/pc-power-scheduler

if [[ "$ENABLE" =~ [tT]rue ]]; then

envsubst < $SHUTDOWN_WOL_PATH/shutdown.timer.tmpl > $SERVICE_PATH/shutdown.timer

/usr/local/sbin/set-wakeup-time

systemctl daemon-reload
systemctl restart wakeup.service
systemctl enable shutdown.timer
systemctl stop shutdown.timer
systemctl start shutdown.timer
systemctl stop shutdown.service

else
systemctl stop shutdown.timer
echo 0 > /sys/class/rtc/rtc0/wakealarm
fi
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[Path]
PathModified=/etc/default/pc-power-scheduler

[Install]
WantedBy=multi-user.target
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[Unit]
Description=Wake up and shutdown the computer at defined time
#Requires=shutdown.timer wakeup.service
#PartOf=shutdown.service wakeup.service
After=shutdown.timer wakeup.service

[Service]
Type=oneshot
ExecStart=/usr/local/sbin/pc-power-scheduler-service-updater
StandardOutput=journal

[Install]
WantedBy=multi-user.target
29 changes: 29 additions & 0 deletions pc-power-scheduler/usr/share/pc-power-scheduler/set-wakeup-time
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env bash

. /etc/default/pc-power-scheduler

DAYS=(${DAYS//,/ })
i=0

RANDOM_LOWER_BOUND=0
RANDOM_UPPER_BOUND=30
RANDOM_OFFSET=$(shuf --head-count=1 --input-range=$RANDOM_LOWER_BOUND-$RANDOM_UPPER_BOUND)

TODAY=$(LC_TIME=en_US.UTF8 date +%a)
for element in ${DAYS[@]}; do
NEXTDAY=${DAYS[++i]}

if [ "${element}" == "$TODAY" ]; then

if [ -z "$NEXTDAY" ]; then
NEXTDAY=${DAYS[0]}
fi

DESIRED_WAKEUPTIME=$(date --date "$NEXTDAY $WAKEUPTIME:00" +%s)
REAL_WAKEUP="$(($DESIRED_WAKEUPTIME-$RANDOM_OFFSET))"
echo 0 > /sys/class/rtc/rtc0/wakealarm
echo "$REAL_WAKEUP" > /sys/class/rtc/rtc0/wakealarm
echo "Next wake up time, programed for $(date --date "@$DESIRED_WAKEUPTIME") will occur at $(date --date "@$REAL_WAKEUP") [@$RANDOM_OFFSET offset]"
break
fi
done
64 changes: 64 additions & 0 deletions pc-power-scheduler/usr/share/pc-power-scheduler/shutdown-popup
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/env bash

. /etc/default/pc-power-scheduler

# display a warning
# $1 username to show the question to
# $2 display to show the question on
# $3 the time remaining before shutdown, in minute
function sdwarn() {

. /etc/default/pc-power-scheduler

local username=$1
local display=$2
local remaining_time=$3

echo "Notifying $username on $display"

wakeuptime=$(date --date "@$(cat /sys/class/rtc/rtc0/wakealarm)")
remaining_time_minute=$(echo ${remaining_time}/60 |bc)
when=$(date -d "+ $remaining_time_minute minutes" +%R )
notification "$username" "$display" "PC power scheduler" "Computer will going to $METHOD at $when, and will wake up on $wakeuptime!"
}

# $1 username to show the question to
# $2 display to show the question on
# $3 title
# $4 text to show
function notification() {

DISPLAY="$2" sudo -E -u "$1" notify-send \
-u critical \
-i /usr/share/icons/Humanity/actions/32/system-shutdown.svg \
"$3" \
"$4"
}

FUNC=$(declare -f sdwarn notification)

REMAINING_TIME=600

for u in $(who | awk '{print $1}' | sort | uniq); do
sessions=$(loginctl list-sessions --no-legend | grep $u | awk '{print $1}')

for session in $sessions; do

display=$(loginctl show-session $sessions -p Display | cut -d= -f2)

if [ $display == ":" ]; then
display=$(who -u | awk -e '$8 ~ /\((:[0-9]|tty.)\)/ {print $8}' | tr -d '()' |sort -u)
else
display=':0'
fi

DBUS_ADDRESS="unix:path=/run/user/$(id -u $u)/bus"
export DBUS_SESSION_BUS_ADDRESS=${DBUS_ADDRESS}
sudo -u $u DISPLAY=${display} \
DBUS_SESSION_BUS_ADDRESS=${DBUS_ADDRESS} \
bash -c "$FUNC; sdwarn \"$u\" \"$display\" $REMAINING_TIME"
done
done


(sleep $REMAINING_TIME; systemctl $METHOD) &
13 changes: 13 additions & 0 deletions pc-power-scheduler/usr/share/pc-power-scheduler/shutdown.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[Unit]
Description=Shuts the computer down
DefaultDependencies=no
After=basic.target

[Service]
Type=simple
ExecStart=/usr/local/sbin/shutdown-popup
TimeoutStartSec=0
RemainAfterExit=yes

[Install]
WantedBy=default.target
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[Unit]
Description=Shuts the computer down at $SHUTDOWNTIME

[Timer]
Unit=shutdown.service
OnCalendar=$DAYS $SHUTDOWNTIME
AccuracySec=1s
Persistent=true

[Install]
WantedBy=timers.target
11 changes: 11 additions & 0 deletions pc-power-scheduler/usr/share/pc-power-scheduler/wakeup.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[Unit]
Description=Wake up the computer at defined time
DefaultDependencies=no

[Service]
Type=oneshot
ExecStart=/usr/local/sbin/set-wakeup-time
StandardOutput=journal

[Install]
WantedBy=default.target
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[Unit]
Description=Enable Wake On Lan

[Service]
Type=oneshot
ExecStart=ethtool --change ${NICNAME} wol g

[Install]
WantedBy=basic.target

0 comments on commit 63b8f3b

Please sign in to comment.