preface

These days, it is quite normal to post a message on the OSC every night before shutting down, saying “I’m going to init 0, goodnight everyone”.

It seems to be a very common thing, but I was stumped by a student’s reply last night. What kind of reply was it? Let’s read on.

As you can see from the figure above, the reply is “Init 0 is not different from HALT…” Init 0 calls HALT at the end of the session, but I’m not sure what the difference is. Obviously, the above is not enough to answer the student, and there is no convincing. So with such a question, I began to explore the road.

The related research

When encountering this problem, think of above all or with the help of search engine. Recently, the order of the search engines I use has changed a bit. It used to be Google – Baidu, and now it is bing- Google – Baidu (or Yandex). However, in the past two days, as things have improved a little bit, Finally, I preferred to Google the above questions.

After some research and filtering, I have sorted out several good answers in my opinion as follows (if you want to have a further understanding, you can refer to my quotation later) :

halt(1M) brings down the machine to the PROM (on SPARC) immediately, with very little regard to running the scripts in /etc/rc[0-6].d to take things down cleanly. reboot(1M) is the same, except instead of just stopping the OS, it reboots the machine.

The halt command puts the machine directly into the PROM, with little to no execution of the /etc/rc[0-6] script to terminate some processes. The reboot command is the same as this, except to stop running the operating system and restart the machine.

In this case, some processes may cause data loss due to abnormal terminations.

Note: PROM stands for programmable read-only memory

init(1M) would run the scripts in /etc/rc[0-6].d to (hopefully) take down the running processes cleanly, and then call halt, reboot, or poweroff(1M) depending on the argument you give it.

The init command will run the /etc/rc[0-6] script, which will normally terminate all running processes and then call halt,reboot, or poweroff based on your command-line arguments.

shutdown(1M) allows you to first give a time when the machine will shutdown, prints warnings to users logged in (and possibly to any NFS clients), and then calls init(1M). init then does things like described in the above paragraph.

The shutdown command gives you some time before the machine is about to shutdown, print a warning to those already logged in (and possibly NFS clients), and then call init, and init to do the things described above.

Using shutdown or init is usually the best way to go about things AFAIK.

Using shutdown or init is the best way I know of so far.

Note: AFAIK stands for As Far As I Konw

I try to keep in the habit of using shutdown(1M) since by default it gives a grace period before doing things. This way if you’re /not/ on the machine you think you are (you’re on production, and not a testing server), you still have an opportunity to cancel things before accidentally disrupting service.

To sum up, the basic dependency is as follows:

shutdown->init->/etc/rc[0-6]->halt/reboot/poweroff

Expansion of reading

The meaning of 1 m

Halt (1M), init(1M), shutdown(1M), etc. Here’s how to answer that question:

In Linux, when we encounter a command and do not know how to use it, Linux provides us with a very convenient tool man, such as man ls you can check the specific usage of the ls command. However, some commands are more complex and will be explained in sections. Man [1-8] ls (man 3 ls) section 3

From Wikipedia we can see that some systems of the Linux class have eight sections.

The manual is generally split into eight numbered sections, organized as follows (on Research Unix, BSD, OS X and Linux):

Section Description
1 General commands
2 System calls
3 Library functions, covering in particular the C standard library
4 Special files (usually devices, those found in /dev) and drivers
5 File formats and conventions
6 Games and screensavers
7 Miscellanea
8 System administration commands and daemons

[from wikipedia]

Unix System V uses a different section, one of which is named 1M, indicating that this section describes System management commands and daemons.

Unix System V uses a similar numbering scheme, except in a different order:

Section Description
1 General commands
1M System administration commands and daemons
2 System calls
3 C library functions
4 File formats and conventions
5 Miscellanea
6 Games and screensavers
7 Special files (usually devices, those found in /dev) and drivers

[from wikipedia]\

init level

As I run Fedora, I will introduce the following information about redhat init level:

Red Hat Linux/Fedora runlevels

Code Information
0 Halt
1 Single-user mode
2 Multi-user mode console logins only (without networking)
3 Multi-user mode, console logins only
4 Not used/User-definable
5 Multi-user mode, with display manager as well as console logins (X11)
6 Reboot

[from wikipedia]\

conclusion

This article is a blog post about the differences between init, HALT,reboot, etc. From the above analysis, we conclude that it is best to use shutdown command to shutdown, because it will notify all logged-in users and NFS clients before shutting down, and end all process information normally and shutdown normally.

For me, however, as a single user without any NFS clients, shutting down with init 0 is fine, and all processes will end normally.

If you have different opinions in this article, please leave a comment.

reference

[1] unix.derkeiler.com/Newsgroups/…

[2] en.wikipedia.org/wiki/Man_pa…

[3] en.wikipedia.org/wiki/Runlev…