Linux Shutdown Command

How do we shutdown a Linux server from the command line? With the shutdown command. This will work for real physical servers, but also virtual machines running in the cloud.

To shutdown a Linux machine immediately we can run:

sudo shutdown now

When to Avoid Immediate Shutdown, and Schedule Instead

If a single person uses this server, we can certainly power it off this way. But if it's used by many people, then powering off immediately might be wrong.

On a bigger Linux machine, there might be multiple people logged in at the same time. So an immediate shutdown might abruptly disconnect everyone. People would wonder "What happened?" And some of their work in progress could be lost.

There's also another reason why we might not want to shutdown immediately. The server might provide certain services. For example, maybe this is a database server used by some website(s). Shutting it down at 5PM โ€“ when the website is heavily used โ€“ might not be the best idea. We might want to power it off at 4AM in the morning instead. When almost no one is using it. But maybe we're sleeping at that time, so we can't power it off manually.

However, the shutdown command has us covered. We can fix both of these issues with two superpowers of the shutdown command:

  • The ability to schedule a shutdown at a future time. For example, we can tell the shutdown command to automatically power off the server at 4AM, while we're sleeping.
  • And we can also send a message to everyone connected to this server, telling them why it will be powered off at 4AM. This way, anyone connected to it will be informed about what will happen. And they'll have time to wrap off their work before the server shuts down.

So let's see the various ways we can shutdown Linux machines from the command line.

The Syntax of the Linux Shutdown Command

We can see the manual of the shutdown command by typing this:

man shutdown

We notice that the general syntax of the shutdown command is:

shutdown [OPTIONS] [TIME] [WALL]

The important options we can pass to the shutdown command are:

  • -P to power off the machine. This is the default behavior, though, so we usually don't need to use this option.
  • -r to restart the machine (instead of powering it off). Example of a command we could use to restart our machine: sudo shutdown -r now.
  • -c to cancel a scheduled shutdown (or reboot). For example, if we instructed our machine to automatically power off at 4AM, we can later cancel this by running sudo shutdown -c.
  • --show to see if any future shutdown (or reboot) is currently scheduled. So we can run shutdown --show to see if any other administrator has scheduled a future shutdown / reboot for this machine.

Schedule a Shutdown at a Specific Time

To schedule a shutdown at 04:15 in the morning, we can run a command like this:

sudo shutdown 04:15

The time specification is in the 24-hour format. So to schedule it at 16:30 we would run this instead:

sudo shutdown 16:30

To cancel a scheduled shutdown, we can run this command:

sudo shutdown -c

Set Wall Message with the Shutdown Command (Notify Users)

Now for the last argument we can pass to the shutdown command, the "wall" argument.

This is simply a message we can send to all people connected to this machine. Let's see this in action, as it will be easier to understand it this way.

We'll assume a person called John is logged in to our server. And he's doing some work in his own terminal window, in a remote SSH session:

Now, we log in as administrators in a separate terminal window / SSH session. And we schedule a shutdown at 04:15, as we did in a previous example. However, this time, we'll also add a wall message:

sudo shutdown 04:15 'Hey guys, there will be a scheduled hardware upgrade tonight.'

The wall message is all the text that we wrote after 04:15. Note that we needed to wrap our message between ' ' single quotes.

As we run our command in our own SSH session, we'll see this:

And the other user, called John, will see this in his SSH session:

Now we could say:

Hmm, there's no message for John.

That's because the wall message is not sent instantly. But, rather, at fixed intervals.

180 minutes (3 hours) before the scheduled shutdown, users will start to be notified with this wall message. Then they will get a notification again, when 150 minutes remain until the scheduled shutdown. As time goes by, they will be notified again, and again, more often. So they should have time to wrap up their work.

This is what John (and other users) will see in their terminal windows, once the scheduled shutdown is 3 hours away:

๐Ÿ’ก
Note: The time is shown in UTC format, so everyone will have to convert to their own timezone.

And this message will be repeated, periodically. So users will be notified. And not only will they know when the shutdown will happen, but they will also know why. Pretty cool feature, isn't it?