If you have a router with storage media attached it's rather important that it be shut down gracefully when powering it off, else filesystem corruption is a possibility.
In my case i shut down everything at night by powering off my PC, then my UPC which supplies power to all other attached devices. I wanted to auto-shutdown the router on system shutdown instead of killing the power by shutting off the UPC and there is tons of advice on how to accomplish this using a systemd unit file, every bit of which either failed to work for me or didn't sound reasonable, and then i found a post on the Manjaro forum that solved the problem.
Goal:
- Gracefully shut down router on system shutdown, but not on reboot.
Requirements:
- Linux
- (the dreaded) systemd
- working, password-less root logon to the router using ssh keys
- tested with OpenWRT
assumed key file name: router_id_ed25519
assumed user name: dingaling
assumed router IP/port: 192.168.1.1:22
In /etc/systemd/system
create the systemd unit file:
$ sudo nano shutdown-router.service
In shutdown-router.service
adjust the following and paste it in the file:
[Unit] Description=Shutdown router on system shutdown DefaultDependencies=no Before=poweroff.target halt.target shutdown.target # to test, comment out below and run: $ sudo systemctl start shutdown-router.service Requires=poweroff.target [Service] Type=oneshot ExecStart=/usr/bin/ssh -i /home/dingaling/.ssh/router_id_ed25519 192.168.1.1 poweroff RemainAfterExit=yes [Install] WantedBy=shutdown.target
After saving the file, finish up with the following:
$ sudo systemctl enable shutdown-router.service $ sudo systemctl daemon-reload