🐧 How to safely restart a CentOS or RHEL server

I am using the reboot command to reboot the PostgreSQL server running on RHEL 7.

We also have a development server running on CentOS 7.

However, sometimes I noticed database corruption or file problems.

Is there such a safe reboot command that will perform a soft reboot of our CentOS or RHEL 7 server without any problem?

What’s the best way to restart CentOS / RHEL via ssh?

In general, there is no correct shutdown or restart.

However, a modern Linux distribution does a great job when you need to reboot a CentOS or RHEL based server.

Let’s take a look at different options for restarting the CentOS / RHEL 7/8 server.

Safely restarting a CentOS or RHEL server via ssh

  1. RHEL / CentOS Linux commands we can use to carefully restart the server:
  2. Shutdown command: all in one command to stop, shutdown or reboot the machine.
  3. Systemctl command: Systemd systemctl command can also restart or shutdown your server.
  4. Reboot command: symlink and / sbin / systemctl alias to restart CentOS / RHEL.
  5. Halt command: Again, symlink and alias are set to / sbin / systemctl to stop CentOS / RHEL. Shuts down and stops the system. You still need to manually press the shutdown button.
  6. Poweroff command: symlink or soft link to / sbin / systemctl to shutdown your CentOS / RHEL. Shuts down and completely shuts down the system. Note that there is no need to press the power off button.

The last three commands act as an abbreviation for a longer command and can save you typing time. Instead of typing “sudo shutdown -r now”, we can enter “sudo reboot”.

The procedure is as follows:

To avoid problems with PostgreSQL / MySQL / MariaDB, synchronize cached writes to persistent storage as root by running:

# sync; sync

Reboot your CentOS / RHEL server, run:

# shutdown -r now

Alternative and recommended way, since shutdown / reboot is a soft link to / sbin / systemctl:

# systemctl reboot or # systemctl poweroff # <- complete shutdown

Personally, if I were you, I would shutdown the database server before the restart command:

# sync;sync
# systemctl stop postgresql
# systemctl stop mysql # MySQL/MariDB
# systemctl reboot

What is reboot, systemctl reboot or shutdown -r now commands

Open a terminal and run the following command:

ls -l /sbin/{halt,reboot,poweroff,shutdown}

All of the above commands will be:

  • Stop all running processes / services.
  • Disable all file systems.
  • Erase temporary files on disk
  • The system will reboot.

Please do not pass the –force parameter to systemctl, as it will immediately reboot the computer without killing any processes or unmounting any filesystems.

This will definitely result in data loss.

So avoid the following:

# systemctl --force --force reboot
# systemctl --force --force shutdown

So why is the –force option provided? It can be used in an emergency when the CentOS / RHEL system manager has crashed and you need to shut down the server. Hence, it is important for your systems to keep verified backups.

The shutdown command has additional options, including backward compatibility.

For example, display a message:

sudo shutdown -r now "Сервер базы данных RHEL 8 отключается из-за обновления ядра."
## или ##
sudo shutdown -r 0 "Сервер базы данных RHEL 8 отключается из-за обновления ядра."
sudo shutdown -h now "Файловый сервер CentOS 7 выключается из-за замены диска."

“Now” means immediately.

We can pass a time string in “hh: mm” format for hour / minute, specifying the time to perform the shutdown, specified in 24 hour format.

Alternatively, it can be the syntax “+ m” referring to the set number of minutes m from now.

Note that “now” is an alias for “+0”, i.e. triggers an immediate shutdown.

If no time argument is specified, “+1” is implied:

sudo shutdown -h +15 "Сервер CentOS NFS отключается для замены ssd через 15 минут. Сохраните все файлы"
sudo shutdown -r +5 "Разработчики, сохраните ваши файлы, поскольку devbox перезагружается через 5 минут."

We can cancel a delayed shutdown.

This can be used to reverse the effect of calling shutdown with a time argument that is not “+0” or “now”:

sudo shutdown -c

How to find out the date and time of the reboot of a CentOS / RHEL system?

Try the following commands:

You learned about the correct way to shutdown or restart your CentOS / RHEL and we also recommend that you keep verified backups to avoid data loss.

Sidebar