Skip to content

Instantly share code, notes, and snippets.

@msinghal34
Last active May 15, 2021 07:30
Show Gist options
  • Save msinghal34/39e1dea201a113814a1a49c9d3d6d3d6 to your computer and use it in GitHub Desktop.
Save msinghal34/39e1dea201a113814a1a49c9d3d6d3d6 to your computer and use it in GitHub Desktop.
Automatic Ubuntu Shutdown with a notification.

Ever thought that someone should just shutdown your PC at night so that you can sleep on time. Here's the solution:

Add .sleep.sh in your home directory. Make it executable by doing:

cd ~ && chmod +x .sleep.sh

Append crontab entry given in sudo_crontab.txt to your root cron using,

sudo crontab -e

Append crontab entry given in crontab.txt to your user cron using,

crontab -e

Voila! Your PC should automatically given you a shutdown notification at 11:30 PM. And will shutdown after 20 seconds.

  • You can change the time as you wish by editing crontab entries.
  • You can also change the icons and sound of notification by editing .sleep.sh
#! /bin/bash
export XDG_RUNTIME_DIR="/run/user/1000"
/usr/bin/notify-send -i /usr/share/icons/Adwaita/48x48/emotes/face-smile.png -u "critical" "Close apps " "PC will shutdown in 20 seconds"
/usr/bin/paplay --volume=32768 /usr/share/sounds/freedesktop/stereo/complete.oga
# This will show a notification daily at 11:30 PM.
30 23 * * * eval "export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME gnome-session)/environ)"; ~/.sleep.sh
# This will shutdown your PC daily at 11:30:20 PM.
30 23 * * * sleep 20 && /sbin/shutdown -h now
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment