I just want to know difference between in
shutdown -r now
and which is the safest and the best?
There is no difference in them. Internally they do exactly the same thing:
reboot uses the
shutdown command (with the -r switch). The shutdown command used to kill all the running processes, unmount all the file systems and finally tells the kernel to issue the ACPI power command. The source can be found here.
In older distros the reboot command was forcing the processes to exit by issuing the
SIGKILL signal (still found in sources, can be invoked with
-f option), in most recent distros it defaults to the more graceful and init friendly
init 1 -> shutdown -r. This ensures that daemons clean up themselves before shutdown.
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
Today there is not much difference as both commands do exactly the same, and they respect the init scripts used to start services/daemons by invoking the shutdown scripts for them. Except for
reboot -f -r now as stated below
There is a small explanation taken from manpages of why the
reboot -f is not safe:
-f, --force Force immediate halt, power-off, reboot. Don't contact the init system.
Forgot to mention, in upcoming RHEL distributions you should use the new
systemctl command to issue poweroff/reboot. As stated in the manpages of
shutdown they are "a legacy command available for compatibility only." and the
systemctl method will be the only one safe.
On traditional unices,
shutdown -r now are vastly different commands. Under typical usage,
reboot is only safe to use in single user mode.
shutdown -r now is the canonical method to shutdown across different *nix's and safer to use in general and is functionally equivalent to
Shutdown is preferable because it allows you to specify the reason for the drastic action -- something you should always do. The message will be recorded in the log(s) for posterity. For example:
shutdown -r now 'Kernel upgrade requires reboot'
You can also perform a scheduled reboot -- by specifying something other than
now as the reboot time:
shutdown -r 22:00 'Work around kernel memory leak'
Then your users will get periodic reminders to get out as the time approaches -- the process will be more orderly and professional.
On FreeBSD there is a difference between
shutdown -r now. From the
reboot man page:
Normally, the shutdown(8) utility is used when the system needs to be halted or restarted, giving users advance warning of their impending doom and cleanly terminating specific programs.