Published Book on Amazon
All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 1 | |
All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 2 |
출판된 한글판 도서
최신 라즈베리파이(Raspberry
Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (상) |
|
최신 라즈베리파이(Raspberry
Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (하) |
Original Book Contents
20.3 Automatic Run with Init Script
20.3.1 Run Level
20.3.1.1 Definition of Run Level
On Linux, you need to process all pre-specified tasks when the system first starts or shuts down, or sometimes need to prevent others from using it when performing system recovery tasks. A tool called "run level" is used to limit who can use the system in a certain step or to identify the types of commands or programs that can run in a particular environment.
The run level is a number assigned according to the usage or status of the system. Linux has the following run levels defined.
Level | Meaning | Description |
0 | Halt | Shut down the system |
1 | Single User Mode | Only one user is allowed and only basic system functions are provided. It is usually used for recovery. |
2 | Multi User Mode | Multi-user mode. It does not supports NFS (Network File System) |
3 | Multi User Mode | Multi-user mode. It provides full networking capability. X window is not supported. |
4 | User Defined | Not used in the system. You can define and use it yourself. |
5 | X window | It is used to boot into X window. |
6 | Reboot | Restart |
The run level is normally used by system administrators. For example, to allow only system administrator to use the system for system recovery and not allow other users to access it, you can use run level "1", and if you want to use X window, you can use run level "5". Then the system adjusts its state accordingly.
In the system, service tasks that must be processed for each run level has beeen defined. If a certain run level is specified, the predefined tasks are automatically executed at the run level, and the system switches to the desired state.
20.3.1.2 "runlevel" Command – Check Run Level
On Linux systems, the default run level is specified in the "/etc/inittab" file in the following format. If you want to change the default run level of the system, you can change this value.
# The default runlevel. id:2:initdefault: |
To check the current run level, use "runlevel" command as follows.
[Command Format]
runlevel [option] |
[Command Overview]
■ This shows the current run level.
■ User privilege -- super user.
[Detail Description]
■ None
[Main option]
--help | Print a short help text and exit. |
[Used Example]
The following checking shows that the current run level is in "2" state.
pi@raspberrypi ~ $ runlevel |
N 2 |
20.3.1.3 "telinit" Command – Change Run Level
If necessary for specific purposes, the system administrator can switch to another run level. The system then immediately switches to the other environment according to predefined detail tasks at that run level. For example, if you change to run level "6" from run level "2" that is currently running normally, the system will immediately reboot.
To switch the current run level to another one, use "telinit" command as follows:
[Command Format]
telinit <run-level> |
[Command Overview]
■ This switch the current run level to another.
■ User privilege -- super user.
[Detail Description]
■ None
[Main option]
--help | Print a short help text and exit. |
[Used Example]
The next is checking the current state before switching the run level, and performing the task to switch the run level.
pi@raspberrypi ~ $ runlevel |
N 5 |
pi@raspberrypi3:~ $ sudo telinit 3 |
pi@raspberrypi3:~ $ runlevel |
5 3 |