If I am running a Pi headless, is there a command I can use to safely shut down, or should I simply remove the power cord?
You can safely shutdown the pi using
The -h just halts all processes
Do not simply unplug the cord, as this could occasionally (perhaps, often) lead to filesystem corruption.
As Impluss says, use
There is a good, perhaps mildly outdated but well written, introduction to udev rules |here|. The basic idea is you get some information about the device via
The third field labelled ID is the vendor and model id separated by a colon. Presuming you do not have multiple identical devices plugged in, this combination should be unique.
You can get more detailed relevant informationan via
Notice the ID_MODEL and ID_VENDOR fields. These are what you want to use in your udev rule. There are some outdated or incorrect sources online that suggest using ATTR fields, but these are ENV fields with regard to a "remove" event.
Create a file in
Make sure this is loaded with
My preferred method is to use
Just to throw it in, if you are into adding a bit of hardware, you can write a small daemon to poll the GPIO pins and upon assertion of a certain pin, reboot (or shutdown) the Pi.
Also, all commands mentioned here can be ran over SSH.
While the question has been adequately answered already; my preference is different to what has been already answered.
As others have said avoid just pulling the power. My preferred commands (either as root or prepend with
I prefer these commands as they are straight forward, easy to remember and self-evident...