EDIT 2: You can use a dhcpcd exit hook script. The script is called on each dhcpcd event and checks the reason for calling dhcpcd and the related interface.
Create the script /etc/dhcpcd.exit-hook (usually it doesn't exists, otherwise edit it):
if [ "$reason" = NOCARRIER ] && [ "$interface" = eth0 ]
then
echo "$(date) - eth0 DOWN, will shutdown now" >> /log/eth0_shutdown.log
poweroff
fi
Note that this script file doesn't need to have a first line like #!/bin/bash
and it also doesn't need to be set as executable.
Create the folder /log if it doesn't exists
sudo mkdir /log
Edit 1: If a cron job is too slow, then a while loop may be used instead. Adjust the sleep time as required.
#!/bin/bash
while [ $(cat /sys/class/net/eth0/operstate) == "up" ]
do
sleep 10
done
echo "LAN is down, will shutdown now"
poweroff
First answer
You can monitor the interface status with a script which is regularly called by a cron job.
if [ $(cat /sys/class/net/eth0/operstate) == "down" ]
then
echo "LAN is down, will shutdown now"
poweroff
fi
dhcpcd
networking. – Milliways Mar 25 '18 at 22:53