How to restart CentOS or RHEL server safely

I use the reboot command to reboot our PostgreSQL server running on RHEL 7. We have a development server running on CentOS 7 too. However, sometimes I noticed database corruption or file issues. Is there is a safe reboot command that will do a graceful reboot for our CentOS or RHEL 7 server without causing any issues? What is the best way to appropriately restart CentOS/RHEL over ssh?

There is no graceful shutdown or restart. However, modern Linux distro does an outstanding job when you need to reboot the server powered by CentOS or RHEL. Let us different options to restart the CentOS/RHEL 7/8 server.
Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements RHEL or CentOS
Est. reading time 2m

Restarting CentOS or RHEL server safely over ssh

RHEL/CentOS Linux commands that we can use to restart the server carefully:

  1. shutdown command : All in one command to halt, power-off or reboot the machine.
  2. systemctl command : Systemd’s systemctl command can reboot or shutdown your server too.
  3. reboot command : Symbolic link and aliased to /sbin/systemctl to restart the CentOS/RHEL.
  4. halt command : Again, symbolic link and alias set to /sbin/systemctl to halt the CentOS/RHEL. Shut down and halt the system. You still need to press the power-off button manually.
  5. poweroff command : Symlink or soft link to /sbin/systemctl to power off your CentOS/RHEL box. Shut down and poweroff the system complety. Please note that there is no need to press the power off button.

The last three command acts as a shortcut to a longer command and saves some typing. Instead of typing “sudo shutdown -r now“, we can type “sudo reboot“.

Best way to gracefully restart CentOS or RHEL

The procedure is:

  1. Synchronize cached writes to persistent storage as root user by flushing everything to avoid problem with PostgreSQL/MySQL/MariaDB, run:
    # sync;sync
  2. Restart the CentOS/RHEL server, run:
    # shutdown -r now
  3. An alternative and recommended way is to type as shutdown/reboot is soft link to /sbin/systemctl:
    # systemctl reboot
    OR
    # systemctl poweroff # <-- complete power off
  4. Personally, if I were you, I would shutdown the database server before issuing the reboot command. Hence:
    # sync;sync
    # systemctl stop postgresql
    # systemctl stop mysql # MySQL/MariDB
    # systemctl reboot

Understanding reboot or ‘systemctl reboot’ or ‘shutdown -r now’ commands

Open the terminal and run the following command:
ls -l /sbin/{halt,reboot,poweroff,shutdown}

Modern CentOS/RHEL symlinked to systemctl

All of the above commands will:
  • Flush all data to disk.
  • Stop all running processes/services.
  • All file systems are unmounted.
  • The system will reboot.

Please do not pass the --force option to the systemct as it will reboot the box immediately without terminating any processes or unmounting any file systems. This will result in data loss for sure. Therefore avoid the following:
# systemctl --force --force reboot
# systemctl --force --force shutdown

So why --force option provided? It can be used in an emergency when the CentOS/RHEL system manager has crashed, and you need to shutdown the server. Hence, keeping verified backups are important for your systems.

When should I use the old shutdown command?

The shutdown command has additional options, including backward compatibility. For instance, display a message:

sudo shutdown -r now "RHEL 8 database server is going down for kernel update."
## OR ##
sudo shutdown -r 0 "RHEL 8 database server is going down for kernel update."
sudo shutdown -h now "CentOS 7 file-server is going down for disk replacement."

The “now” means immediately. We can pass the time string in the format “hh:mm” for an hour/minutes specifying the time to execute the shutdown specified in 24h clock format. Alternatively, it may be in the syntax “+m” referring to the set number of minutes m from now. Please note that “now” is an alias for “+0”, i.e., triggering an immediate shutdown. If no time argument is specified, “+1” is implied:

sudo shutdown -h +15 "CentOS NFS server is going down for ssd replacement in 15 mintues time. Please save all files"
sudo shutdown -r +5 "Developers save your files as devbox is rebooting in 5 mintues time."

We can cancel a pending shutdown. This may be used cancel the effect of an invocation of shutdown with a time argument that is not “+0” or “now”:
sudo shutdown -c

Command discussed here are compatible with different Linux distros such as Arch, Ubuntu, Debian, Fedora Linux, etc. Always read man pages.

How do I find out CentOS/RHEL systems reboot data and time?

Try the following commands:
last -x reboot | head -1
last -x shutdown | head -1
who -b
uptime -s

How to automate and reboot multiple CentOS/RHEL server hosted in the cloud

We can reboot many Linux machines, wait for it to go down (say for kernel update), come back up, and respond to commands using Ansible. See how to use Ansible to reboot Linux machine or server with playbooks.

Summing up

You learned about the proper way to either shut down or restart your CentOS/RHEL box, and we recommend that you keep verified backups to avoid data loss. See how to install and use rsnspahot on your RHEL/CentOS to make backups. Getting help is easy:
$ man shutdown
$ man systemctl
$ shutdown --help

Sample outputs:

shutdown [OPTIONS...] [TIME] [WALL...]

Shut down the system.

     --help      Show this help
  -H --halt      Halt the machine
  -P --poweroff  Power-off the machine
  -r --reboot    Reboot the machine
  -h             Equivalent to --poweroff, overridden by --halt
  -k             Don't halt/power-off/reboot, just send warnings
     --no-wall   Don't send wall message before halt/power-off/reboot
  -c             Cancel a pending shutdown

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 10 comments so far... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
10 comments… add one
  • Chris Dec 28, 2020 @ 14:20

    init 6” is always my go-to, but maybe that’s because I’m old school.

    • unsigned char Dec 29, 2020 @ 22:41

      in the old days we used telinit 6 or telinit 0 and we liked it.

  • Matthew Tanner Dec 28, 2020 @ 16:17

    Great article! I would add that there shouldn’t be a need to be explicitly stop services or sync before shutting down via systemd. If there are corruption issues occurring, I would suggest addressing the root cause, looking at the associated systemd unit files, adjusting by adding overrides as needed.

    • 🐧 Vivek Gite Dec 28, 2020 @ 16:50

      Yes, I agree. My experience shows that it is best to shut down the write node in the database and reboot it. After that, the read-only replica can be rebooted too. Of course, it all depends upon database cluster type and apps. YMMV.

  • Dupych Dec 28, 2020 @ 18:00

    Ctrl+Alt+Del for restart CentOS.

  • george Dec 28, 2020 @ 18:41

    Whow. Notging about checking the integrity of your filesystems.

    I guess these kids never did an ls -laux

    Rhen are confronted with a plethra of questionmarks on your permissions fields, ownership amd so on. That is when your backups get your attention. Then maybe just maybe you can fix the filesystem corruption. But before you do that you have to determine your problem. Disk, Controller. You have to resolve this before you attempt to boot. You need to develop best practice peocedures that address each and every occurrance of file system corruption.

  • Harold Dec 29, 2020 @ 3:00

    This really required a whole article? All these low-effort articles lately really are becoming annoying.

    • Waring for 5G Dec 29, 2020 @ 18:38

      As a new Linux user I find these very useful. Not everyone is expert since day 0. We have to start somewhere. I say more tip and tricks are useful for users coming from Windows server.

  • Andy Morris Dec 30, 2020 @ 0:46

    The database corruption seems to indicate the Postgres service isn’t shutting down cleanly.
    I’d be trying to troubleshoot that, or at least shutting it down first and then doing a reboot.

  • Aaa Dec 30, 2020 @ 12:57

    sync, then init 0 or init 6

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum