Take the 2-minute tour ×
Raspberry Pi Stack Exchange is a question and answer site for users and developers of hardware and software for Raspberry Pi. It's 100% free, no registration required.

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?

share|improve this question
add comment

4 Answers 4

up vote 27 down vote accepted

You can safely shutdown the pi using

shutdown -h now

The -h just halts all processes

share|improve this answer
5  
Might be worth learning at the same time that -r will do a restart (shutdown + reboot). –  XTL Jan 29 '13 at 7:57
5  
Don't forget you must be root to shutdown or use sudo. –  otakun85 Jan 31 '13 at 9:34
add comment

Do not simply unplug the cord, as this could occasionally (perhaps, often) lead to filesystem corruption.

As Impluss says, use shutdown. I recently ran across a tip about configuring udev to trigger shutdown or reboot when a specific usb device is unplugged. This is useful if the system has become unresponsive or has lost a network connection and you can't or won't bother with plugging hid (human interface device) stuff like a keyboard into it.

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 lsusb, for example:

Bus 002 Device 003: ID 0bda:8176 Realtek Semiconductor Corp. RTL8188CUS 802.11n WLAN

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 udevadm monitor --udev --property, which will report to standard out until you kill it, eg. when I unplug the teenie weenie wifi dongle from above it spits forth:

UDEV  [2834.504860] remove   /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.6 (usb)
ACTION=remove
[...]
ID_BUS=usb
ID_MODEL=802.11n_WLAN_Adapter
ID_MODEL_ENC=802.11n\x20WLAN\x20Adapter
ID_MODEL_ID=8176
[...]
ID_VENDOR=Realtek
ID_VENDOR_ENC=Realtek
ID_VENDOR_ID=0bda

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 /etc/udev/rules.d. This is the same regardless of distribution. It does not matter what the file is called. In it add a line like:

ACTION=="remove", ENV{ID_VENDOR_ID}=="0bda", ENV{ID_MODEL_ID}=="8176", RUN+="/sbin/shutdown -h now"

Beware == and not =. If you use the later, the criteria is meaningless. In which case you could end up with a udev rule that matches any event!

Make sure this is loaded with udevadm control --reload-rules. Now when you yank the wifi dongle out, the pi should cleanly shutdown...give it a minute to do that and you can then unplug the power (try this with a screen attached the first time). You could also use this to reboot -- see man shutdown, and, actually, the man page for all the commands mentioned here ;)

share|improve this answer
1  
I dont think this is what the OP was asking. But +1 for the info. –  Vincent P Jan 29 '13 at 4:41
    
Neat! You should of course also be able to have the removal (or plugging in) of an arbitrary USB device trigger this –  Tobias Kienzler Jan 29 '13 at 7:00
    
This is a nice start. Getting the shutdown triggered by a GPIO button (any way to get something like that to ACPI or HID event?) or something would be even more handy. –  XTL Jan 29 '13 at 7:56
    
@XTL: There are apci daemons around, so it is possible (the kernel reports via proc, etc). The same is at least potentially true for gpio on the pi. HID events are more contextual (a example context being a GUI desktop environment) and the fact that you can type "halt" (== shutdown -h now on linux) maybe mitigates the need for such ;) –  goldilocks Jan 30 '13 at 8:03
    
This is perfect, the dongle is the only thing that's plugged in to our Pi, and we often lose connection so then we need to move it around, without corrupting it from power loss. –  Noio Jun 23 '13 at 11:31
add comment

you may issue the command to shutdown

sudo init 0

to reboot:

sudo init 6
share|improve this answer
add comment

My preferred method is to use sudo poweroff, which is an alias for a shutdown command that also kills power usage.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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