Sign up ×
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
poweroff is the best way to shut it down – RahulAN Jun 18 at 6:33

6 Answers 6

up vote 32 down vote accepted

You can safely shutdown the pi using

shutdown -h now

The -h just halts all processes

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

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)

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. The file must end in .rules and all files in this directory are processed lexicographically. I believe rules declared earlier take precedence, so using 00-my_pi.rules will keep it at the front of the line (numbers sort before letters). 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
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

You may issue the following command to shutdown:

sudo init 0

And to reboot:

sudo init 6
share|improve this answer
This is highly dependent upon run levels being a concept the operating system still uses. A shift to systemd makes this less usable nowadays. – Stephen Michael Kellat Jul 13 at 3:16
Excluding the edit from today, you may have noticed this is quite an old answer. The question is also greater than two years old. – Registered User Jul 13 at 3:41

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

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 sudo):

To halt: halt (for Wheezy and prior this command also powers the system off; for Jessie it doesn't actually poweroff although it is safe to pull the plug once finished) halt -p; shutdown now -h or simply poweroff are required for Jessie...

To reboot: reboot

I prefer these commands as they are straight forward, easy to remember and self-evident...

share|improve this answer
Don't mean to whinge but I think that it's a little harsh that my answer (from 8 months ago) was downvoted because Debian (i.e. upstream of Raspbian) changed the way that their commands work (assuming that is why I was down voted). Also my answer (prior to editing) did still answer the OP (i.e. if you halt the system it is safe to pull the plug...) FWIW I have updated the answer so it is clear that this no longer works as a user might hope... – Jeremy Davis Jun 18 at 4:50

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.

share|improve this answer

Your Answer


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.