Skip to content

Instantly share code, notes, and snippets.

@tristan-k
Last active June 25, 2022 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tristan-k/23ba43a7f6a862daf9af7449a12a0aa5 to your computer and use it in GitHub Desktop.
Save tristan-k/23ba43a7f6a862daf9af7449a12a0aa5 to your computer and use it in GitHub Desktop.
auto_shutdown_proxmox
```
$ cat /root/autoshutdown.sh
#!/bin/bash
qm list | grep running
if [ $? -eq 1 ]
then
shutdown -h now
fi
```
```
$ cat /etc/systemd/system/autoshutdown.timer
[Unit]
Description=autoshutdown timer
Requires=autoshutdown.service
[Timer]
OnBootSec=900
OnUnitActiveSec=300
Unit=autoshutdown.service
[Install]
WantedBy=timers.target
```
```
$ cat /etc/systemd/system/autoshutdown.service
[Unit]
Description=autoshutdown service
Wants=autoshutdown.timer
[Service]
Type=oneshot
ExecStart=/usr/bin/sh /root/autoshutdown.sh
```
```
$ systemctl daemon-reload
$ systemctl enable autoshutdown.timer && systemctl start autoshutdown.timer
```
Reboot time is 16:20
```
$ systemctl status autoshutdown.timer
● autoshutdown.timer - autoshutdown timer
Loaded: loaded (/etc/systemd/system/autoshutdown.timer; enabled; vendor preset: enabled)
Active: active (waiting) since Sat 2022-06-25 16:20:48 CEST; 3min 1s ago
Trigger: Sat 2022-06-25 16:25:48 CEST; 1min 58s left
Triggers: ● autoshutdown.service
Jun 25 16:20:48 gamera-pve systemd[1]: Started autoshutdown timer.
```
```
$ systemctl status autoshutdown.service
● autoshutdown.service - autoshutdown service
Loaded: loaded (/etc/systemd/system/autoshutdown.service; static)
Active: inactive (dead) since Sat 2022-06-25 16:20:49 CEST; 3min 49s ago
TriggeredBy: ● autoshutdown.timer
Process: 1559 ExecStart=/usr/bin/sh /root/autoshutdown.sh (code=exited, status=0/SUCCESS)
Main PID: 1559 (code=exited, status=0/SUCCESS)
CPU: 466ms
Jun 25 16:20:48 gamera-pve systemd[1]: Starting autoshutdown service...
Jun 25 16:20:49 gamera-pve sh[1562]: ipcc_send_rec[1] failed: Connection refused
Jun 25 16:20:49 gamera-pve sh[1562]: ipcc_send_rec[2] failed: Connection refused
Jun 25 16:20:49 gamera-pve sh[1562]: ipcc_send_rec[3] failed: Connection refused
Jun 25 16:20:49 gamera-pve sh[1562]: Unable to load access control list: Connection refused
Jun 25 16:20:49 gamera-pve sh[1559]: shutdown -h now
Jun 25 16:20:49 gamera-pve systemd[1]: autoshutdown.service: Succeeded.
Jun 25 16:20:49 gamera-pve systemd[1]: Finished autoshutdown service.
```
### Script
- https://forum.proxmox.com/threads/script-to-shutdown-node-when-no-vm-active-for-1-hour.38803/post-218473
- https://forum.proxmox.com/threads/automatisches-starten-und-herunterfahren-bei-unt%C3%A4tigkeit.64940/
### Issue
- https://gist.github.com/drmalex07/350238961f451d6946dd?permalink_comment_id=2216416#gistcomment-2216416
### Source
- https://linuxhint.com/cron_systemd_timer/
- https://wiki.ubuntuusers.de/systemd/Timer_Units/
- https://wiki.archlinux.org/title/Systemd/Timers
- https://wiki.archlinux.de/title/Systemd/Timers
- https://kofler.info/systemd-timer-als-cron-alternative/
- https://www.linux-magazine.com/index.php/layout/set/print/Issues/2018/214/Systemd-Timers/(tagID)/628
- https://unix.stackexchange.com/questions/556391/how-to-continuously-run-a-script-with-systemd
- https://unix.stackexchange.com/questions/427346/im-writing-a-systemd-timer-what-value-should-i-use-for-wantedby
- https://forum.proxmox.com/threads/run-systemd-service-after-proxmox-finished-starting.82284/
- https://unix.stackexchange.com/questions/126786/systemd-timer-every-15-minutes#comment913273_294200
- https://techoverflow.net/2021/06/11/how-to-run-systemd-timer-every-five-minutes/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment