106

I always used the command:

shutdown -r now

However, sometimes that causes MySQL issues.

What's the most graceful way to restart CentOS?

I've seen:

reboot

and

halt

How can I gently reboot the machine?

4
  • 4
    reboot is essentially an alias to shutdown -r ...so...yeah.
    – Nathan C
    Aug 15, 2014 at 1:50
  • Did my post answer your question, or do you need to know anything else?
    – Joffrey
    Aug 31, 2014 at 13:38
  • Cannot comment here yet, but I wanted to place a warning here about rebooting. If the instance is on Amazon EC2, doing reboot -h now will brick the instance, so never do this. Instead, if you have to shutdown or reboot, do it through Amazon's online interface, not the shell. If you do brick your instance, you can stop it, detach the volume, create a separate instance, and attach the volume to it, without detaching the first volume that came with the new instance. Then you'll need to ssh into the new instance, mount the old volume, and get your files off it.
    – kloddant
    Aug 26, 2016 at 14:00
  • actually, you want to run sync: sync; before running shutdown -r so all file I/O will be synchronized. I suspect this will also clear up the problem with mysql Feb 16, 2017 at 0:44

5 Answers 5

116

Systems using systemd (CentOS >=7) will have the reboot, shutdown and halt commands symlinked to systemctl to handle the reboot. The systemctl program will detect the use of the symlink and run the systemctl command with the correstponing arguments. For the difference between the commands see the manpage for systemctl (man systemctl) for it is quite nicely documented.

For CentOS 6, there is no better way to restart your server by using anything else than any those commands stated in the original question:

  • shutdown is the most common way to stop your system. Adding the argument -r and a specific time (or 'now') will reboot your system instead of halting it after the shutdown sequence.
  • reboot is a wrapper round shutdown which does some harddisk maintenance (syncing and/or putting in standby mode and not really relevant).
  • New versions of reboot (>2.74) will initiate shutdown if not in runlevel 0 or 6.
  • Most init scripts call halt to make a log in utmp.

Modern distributions will have all tasks covered regardless of the command you are using. Basically they all initiate the shutdown run-time of your SysV (CentOS <7) or systemd (CentOS >=7) scripts (I will call them init scripts for ease of reading).

Shutting down using init scripts step by step stop all your services registered under usually runlevel 'S'. Individual init scripts can have a timeout, like the MySQL init script in CentOS. When the stop argument is given and the daemon will not be shutdown in a fair amount of time, the script will stop and exit giving a failure. The shutdown process will continue as if nothing was wrong, only taking a bit longer and probably print a warning. At the end, when all init scripts are executed, the inevitable will happen: all processes still running will get a SIGTERM signal and, after a few seconds (2 or 5), a SIGKILL. This will clean up the rest before an ACPI call is done to really reboot or shutdown your system.

One exception is using the reboot command with the -f option, this will skip executing init scripts and will reboot the system directly.

You will be better off fixing the root-cause of your worries: MySQL not shutting down properly.

Often this is due to the massive load of work that needs to be done before the daemon can be exited safely. I once had a MySQL instance with +300.000 tables that took over an hour to exit. Similar issues can be found with systems using huge buffers and sparse I/O availability.

4
  • Sorry to sound ignorant, but you sound like you know what you're talking about so I'd like to ask :) What does "call halt to make a log in utmp" mean? Following the link gives "utmp maintains a full accounting of the current status of the system, system boot time (used by uptime), recording user logins at which terminals, logouts, system events etc." but this doesn't really tell me what it is or what you mean by your comment.
    – Neek
    Dec 15, 2019 at 15:57
  • 1
    @Neek No excuses needed for asking questions! Shutting down is a complex combination of commands depending on the state of the system. Of all the commands ('halt', 'reboot', 'shutdown'), this command is one of the ways to stop the system (or call another script in this case). The special argument '-w' will write a utmp entry and is observed to be used in the CentOS 6 init-scripts. Bit of an inception: halt calls shutdown, shutdown calls init-scripts and init-scripts will use 'halt -w' to write the shutdown timestamp. This kind of trickery is a great argument for the use of systemd.
    – Joffrey
    Dec 16, 2019 at 15:13
  • systemctl reboot Aug 10, 2022 at 5:28
  • @AbhilashSinghChauhan Thank you for pointing this out on this very old question. I added clarifications for CentOS >=7
    – Joffrey
    Aug 10, 2022 at 7:30
42

A graceful shutdown of Centos 6.x should be done by using the command as root:

shutdown -h now

This will attempt to stop all running services before shutting down the server gracefully.

Using this command also prevents mySQL socket issues.

Similarly, for a graceful reboot:

reboot -h now

You can read a previous answer about a similar question here:

https://stackoverflow.com/questions/20407292/centos-another-mysql-daemon-already-running-with-the-same-unix-socket

11
  • 10
    I think you mean shutdown -r now, or perhaps just reboot. The reboot command doesn't have a -h option. Sep 5, 2014 at 1:10
  • 1
    Hi Michael, Centos 6.x does indeed have the option to halt services before rebooting. "shutdown -h now" is a graceful shutdown, and "reboot -h now" is a graceful reboot. Try it on your Centos server...
    – GeckoSEO
    Sep 6, 2014 at 0:02
  • 2
    Really? Do you have some documentation for this? Because, first, that's what reboot does without -h. Second, it's not documented that way at all, as far as I can tell. Sep 6, 2014 at 0:22
  • 2
    The doc you just linked states exactly what I have been telling you! reboot has no -h option, shutdown does. Sep 7, 2014 at 15:23
  • 2
    -1 Uncorrected error in second example. Probable error in first example: the question is about restart, and -h doesn't do that.
    – Bob Stein
    Feb 22, 2017 at 2:30
4

I use the command 'init 6' .

init 6 tells the init process to shutdown all of the spawned processes/daemons as written in the init files (in the inverse order they started) and lastly invoke the shutdown -r now command to reboot the machine.

More information can be found on this question.

2

When giving remote instructions to end-users and customers, I instruct them to use poweroff to shutdown and power the system off.

If they want a warm reboot, I suggest that they use the reboot command.

I suppose one could say that issuing Ctrl-Alt-Delete also accomplishes this ;)

3
  • I do not agree with this reply. reboot and shutdown (Ctrl-Alt-Delete) is forceful and can in some environments, such as those running mySQL, cause a socket "lock", which then requires creating a new socket, or deleting/moving the existing socket before recreating a new one.
    – GeckoSEO
    Sep 4, 2014 at 21:36
  • 1
    @GeckoSEO It was a facetious suggestion referring to the danger of allowing Ctrl-Alt-Delete to be enabled on Linux servers.
    – ewwhite
    Sep 4, 2014 at 21:38
  • my response on the other hand was not facetious, I was simply attempting to clarify the answer for those who are trying to solve this question. Please see my earlier answer, I hope it helps.
    – GeckoSEO
    Sep 4, 2014 at 21:42
2

I have a feeling you may not like this answer, but THIS answer includes some insight none of the others consider. . . You write:

I always used the command:

shutdown -r now

However, sometimes that causes MySQL issues.

The problem here is NOT your method of shutdown, is the piece of software that can't handle a shutdown properly; MySql.

The ONLY way you can really address this is replace MySql (PostgreSql is a great replacement) OR write your own shutdown script that FIRST coddles MySql like a baby, and when it's happy to be shut down THEN shuts down the system.

... As a person who has specialized in database systems for over 26 years, I can tell you that it's only been in very recent times that MySql has started to get its act together regarding competency at the most important facets of what database systems are for - data security / integrity. Formerly, they have paid less attention to these aspects and more attention to performance. But what good is great performance if you lose data? On SOME applications, like, say, Netflix streaming video recording at what point you're viewing, absolute fidelity may not be particularly important - maybe, even, your customers LIKE the video backing up a bit following a failure. But in a banking system, it's a disaster of epic proportions. Only the data owner can know what's right for them.

2
  • 2
    Replace mysql just because problems restarting or shutdown the system? And the last part of the solution does not contribute anything to solve problem of the person who make the question
    – ftrujillo
    Nov 11, 2016 at 15:13
  • This answer is biased against mysql without sources or references. It's not useful in the context of the question; any software that takes too long to exit can cause problems at reboot (especially with systemd and its aggressive timeouts).
    – Harald
    Sep 10, 2019 at 18:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .