Skip to content

Instantly share code, notes, and snippets.

@luckylittle
Last active May 25, 2022 00:05
Show Gist options
  • Save luckylittle/8dabdd8492f589cb74d920c6a436253a to your computer and use it in GitHub Desktop.
Save luckylittle/8dabdd8492f589cb74d920c6a436253a to your computer and use it in GitHub Desktop.
Make your computer go to sleep today at 16:59 (once)
#!/bin/zsh
# Old way of doing it:
# at -M 16:59 <<END
# /usr/bin/sudo /bin/systemctl hybrid-sleep
# END
# New way of doing it:
# systemd-analyze calendar --iterations=5 '16:59' <-- this would run every day
# systemd-analyze calendar "$(date +%Y-%m-%d) 16:59" <-- only today
systemd-run --user --on-calendar="$(date +%Y-%m-%d) 16:59" --timer-property=AccuracySec=1s --timer-property=RandomizedDelaySec=10s /bin/systemctl hybrid-sleep
@luckylittle
Copy link
Author

luckylittle commented May 22, 2022

Note: It creates these two files:
1.

$ cat /run/user/1000/systemd/transient/run-rfa6448f7c0b14890b0798364623530a3.timer
# This is a transient unit file, created programmatically via the systemd API. Do not edit.
[Unit]
Description=/bin/systemctl hybrid-sleep

[Timer]
OnCalendar=2022-05-25 16:59
AccuracySec=1s
RandomizedDelaySec=10s
RemainAfterElapse=no
$ cat /run/user/1000/systemd/transient/run-rfa6448f7c0b14890b0798364623530a3.service
# This is a transient unit file, created programmatically via the systemd API. Do not edit.
[Unit]
Description=/bin/systemctl hybrid-sleep

[Service]
ExecStart=
ExecStart=@/bin/systemctl "/bin/systemctl" "hybrid-sleep"

And this unit can be listed:

systemctl --user list-units run-rfa6448f7c0b14890b0798364623530a3.timer --all
UNIT                                        LOAD   ACTIVE SUB     DESCRIPTION                
run-rfa6448f7c0b14890b0798364623530a3.timer loaded active waiting /bin/systemctl hybrid-sleep

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.

1 loaded units listed.
To show all installed unit files use 'systemctl list-unit-files'.

As well as observe here:

systemctl list-timers --user --all

To remove the timer, do not delete the transient files, instead run:

systemctl --user stop run-rfa6448f7c0b14890b0798364623530a3.{timer,service}

If you already deleted the transient files, remove them from systemd:

systemctl daemon-reload --user
systemctl reset-failed --user

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment