1

I was looking at How to shut down RPi when running headless and really happy to see the awesomeness!

So, is it possible to use udev rule to shutdown my Pi when I switch off the router that connects my Pi with the eth cable?

3

You could write a script that checks the content of the file /sys/class/net/eth0/operstate in a loop. When the state shows down execute the halt command on your RPi.

Something like:

while true
    grep 'down' /sys/class/net/eth0/operstate && halt
    sleep 5
done

Or, even better with a cron job run, say, every 2 minutes:

*/2 * * * * root /bin/grep 'down' /sys/class/net/eth0/operstate && halt -p
  • Great one! The cron will be better, I think. – Kangkan Feb 4 '14 at 10:48
  • The cron job was not running with the entry that you provided. It seems, you missed the user name in the crontab entry. I added root as the user and added -p to power down when it halts. – Kangkan Aug 8 '14 at 12:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.