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?

  • poweroff is the best way to shut it down – RahulAN Jun 18 '15 at 6:33
  • poweroff is probably a symbolic link to halt...! 8-) – SlySven Jan 14 '16 at 3:56
up vote 39 down vote accepted

You can safely shutdown the pi using

shutdown -h now

The -h just halts all processes

  • 7
    Might be worth learning at the same time that -r will do a restart (shutdown + reboot). – XTL Jan 29 '13 at 7:57
  • 6
    Don't forget you must be root to shutdown or use sudo. – keiki Jan 31 '13 at 9:34
  • 1
    Alternatively sudo poweroff – berto Jul 13 '15 at 18:00
  • 3
    The -h does halt the entire system - without that option shutdown will take init to run-level 1 - i.e, single user mode, waiting for a super-user login (root password needed). After logging in as root and then logging out init will then redo the stuff necessary to bring the RPi up in multi-user mode - as it would do during the original boot-up (after completion of all the stuff, i.e. fsck etc, running /etc/rc.local that it does then). In this context "shutdown" is "take the system off-line for normal users"... – SlySven Jan 14 '16 at 4:06
  • Yes, but shutdown actually takes some time to execute, and disconnects any ssh clients while it's at it. So how do you know when it's safe to, for example, remove the SD card? – Tom Auger Nov 22 '17 at 18:51

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 ;)

  • 3
    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
  • 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 '15 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 '15 at 3:41
  • 1
    In this usage it is considered better to use sudo telinit #where # is a number between 0 and 6 - telinit is a symbolic link to init which recognises that it not being called by its primary name "init" (and that it does not have a PID of 1!) so it creates a pipe to the real "init" process and tells it to change the current runlevel to the new value stated as the numeric argument. telinit is a contraction of "tell init the new runlevel". – SlySven Jan 14 '16 at 4:11

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

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...

  • 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 '15 at 4:50
  • You should not get the habit to use halt or poweroff, as they are only aliases to shutdown -h now with GNU tools, but on other systems you will power off your system immediately, killing all your programs and possibly corrupting your filesystem. This said, you can use it on a raspberry pi with most linux distributions, but if you use the pi for learning, you may want to do it "the right way". – allo Jul 3 '16 at 15:48

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.

I know it's 3 years after the original question. But I just got my Raspberry Pi and I'm having trouble shutting it down if I forgot to connect it to a monitor screen and it doesn't have any network connection.

I've written a small Python script to automatically shut it down within 60 seconds by plugging in a thumbdrive containing file named "pi_auto_shutdown".

Just call this script from rc.local.

I hope this helps.

shutdown_loop_delay = 60
shutdown_flag_file = 'pi_auto_shutdown'

def poll_shutdown_flag():
    """check whether a shutdown flag file in a usb drive exists"""

    ## run mount command
    ## sample mount output: "/dev/sda1 on /media/path/"
    output, error = subprocess.Popen('mount', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
    if len(error) > 0:
        log('mount error: {}'.format(error))
        return False

    ## parse mount output
    for output_line in output.split('\n'):
        output_words = output_line.split(' ')

        if len(output_words) < 3:

        if output_words[0].startswith('/dev/sd'):
            flag_file_path = os.path.join(output_words[2], shutdown_flag_file)
            if os.path.isfile(flag_file_path):
                return True

    return False

def shutdown():
    """shutdown the system immediately"""
    subprocess.Popen('sudo shutdown -h now', shell=True).communicate()

def loop_shutdown():
    while True:
        if poll_shutdown_flag():

  • Interesting approach. – Eric Wilson Jul 3 '16 at 18:02

I ssh into my RPi box using the command

$ ssh rpi sudo poweroff

rpi is the alias for the IP Address of my RPi box and is defined in the ~/.ssh/config file.

Your Answer


By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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