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 | Easy (rss) |
Root privileges | Yes |
Requirements | RHEL or CentOS |
Time | 2m |
Restarting CentOS or RHEL server safely over ssh
RHEL/CentOS Linux commands that we can use to restart the server carefully:
- shutdown command : All in one command to halt, power-off or reboot the machine.
- systemctl command : Systemd’s systemctl command can reboot or shutdown your server too.
- reboot command : Symbolic link and aliased to /sbin/systemctl to restart the CentOS/RHEL.
- 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.
- 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:
- Synchronize cached writes to persistent storage as root user by flushing everything to avoid problem with PostgreSQL/MySQL/MariaDB, run:
# sync;sync - Restart the CentOS/RHEL server, run:
# shutdown -r now - 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 - 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
- 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
- Share on Twitter • Facebook • 10 comments... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
“init 6” is always my go-to, but maybe that’s because I’m old school.
in the old days we used telinit 6 or telinit 0 and we liked it.
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.
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.
Ctrl+Alt+Del for restart CentOS.
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.
This really required a whole article? All these low-effort articles lately really are becoming annoying.
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.
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.
sync, then init 0 or init 6