Raspberry Pi_Kor_20.2.1 Shell과 script

20.2 script를 통한 명령 일괄 실행

여기서는 여러 개의 명령을 한번에 일괄 처리하기 위해서 사용하는 script 도구에 대해서 살펴볼 것이다. script에 대한 개념과 script를 작성하는 방법, script를 실행하는 방법 등에 대해서 논의할 것이다.

20.2.1 Shell과 script

20.2.1.1 script 란?

Linux 시스템에서는 Shell이 명령을 실행하는데 중심적인 역할을 하고 있는데, 여러 가지 Shell 프로그램 중에서 BASH가 기본적인 Shell로 지정되어 있다. BASH는 Terminal 환경에서 사용자가 입력한 명령을 받아서 그 명령의 의미를 해석하고, 지정된 방식에 따라 명령을 처리한 다음, 처리 결과를 사용자에게 되돌려 준다.

Raspberry Pi_Kor_20.1.2 수동 실행과 자동 실행

20.1.2 수동 실행과 자동 실행

처리를 지시하는 주체를 기준으로, 처리방식을 수동 실행 방식과 자동 실행 방식으로 구분할 수 있다.

● 수동 실행

이 방식에서는 어떤 명령의 처리를 사용자가 직접 지시한다. 실행이 필요한 시점을 사용자가 결정하고, 실행을 위해 입력하는 구체적인 명령의 내용도 사용자가 결정하여 명령의 실행을 지시하게 된다. 여기서 실행하는 명령은 하나의 개별 명령일 수도 있고, script로 정의된 여러 명령의 묶음일 수도 있다. 그것이 어떤 형태이던, 실행의 시작을 사용자가 결정하는 것이다.

Raspberry Pi_Kor_20.1.1 개별 실행과 일괄 실행

20.1 명령 실행 방식

Raspberry Pi 시스템에서 명령을 실행해하는 데는 여러 가지 방법이 있다. 사용자가 일일이 하나씩 실행할 수도 있고, 컴퓨터가 알아서 자동으로 처리해 줄 수도 있다. 여기서는 어떤 방식을 사용할 수 있는지에 대해서 살펴보기로 하겠다.

20.1.1 개별 실행과 일괄 실행

한번에 처리하는 명령의 개수를 기준으로, 처리방식을 개별 실행 방식과 일괄 실행 방식으로 구분할 수 있다.

Raspberry Pi_Kor_20.0 Chapter 20 일괄 실행과 자동 실행

Chapter 20 일괄 실행과 자동 실행

Chapter 주요 내용

여기서는 Linux 시스템에서 명령을 실행해하는 여러 가지 방법에 대해서 설명할 것이다. 명령을 하나씩 실행거나, 여러 명령을 한꺼번에 실행할 수도 있다. 또한 수동으로 실행할 수도 있고, 시스템이 자동으로 실행하도록 할 수도 있다.

이와 관련하여 여러 명령을 한번에 처리하기 위해서 사용하는 script 도구에 대해서 살펴볼 것이다. 일정한 주기로 명령을 실행해 주는 도구에 대해서도 살펴 볼 것이다.

다음과 같은 항목에 대한 내용을 포함하고 있다.
■ 개별 실행과 일괄 실행

■ 수동 실행과 자동 실행

■ script를 통한 일괄 실행

■ init script를 통한 자동 실행

■ schedule에 의한 자동 실행

Raspberry Pi_Eng_20.3.5 Automatic Run regardless of Run Level at Booting

20.3.5 Automatic Run regardless of Run Level at Booting

If you want the command or script to always run regardless of run level, you register it in the “/etc/rc.local” file. The script registered here is executed only in run level “2” ~ “5” which is a normal operation status of the system.

Here is an example for the contents of the file “/etc/rc.local”.

Raspberry Pi_Eng_20.3.4 Automatic Execution by Run Level at Booting

20.3.4 Automatic Execution by Run Level at Booting

20.3.4.1 Principles of Registering Init Script

After you have created an init script and saved it in “/etc/init.d”, you need to register it on the system so that it runs automatically according to the run level. If we systematically look into registering on the system, you can see that it is creating a link to the original script file. In other words, running the link will run the original script file.

Raspberry Pi_Eng_20.3.3 Creating Init Script

20.3.3 Creating Init Script

20.3.3.1 Meaning of Init Script

The script that is used when the system boots or shuts down is called “init script”. All init scripts basically should be put under “/etc/init.d/” directory.

There are several types of init scripts as follows according to their usage:

Raspberry Pi_Eng_20.3.2 Booting Sequence

20.3.2 Booting Sequence

When the system first starts up, it executes certain defined tasks in a certain order. This sequence is called “booting sequence”. Next, we will explain the basic framework of booting sequence and how to use it to automatically execute the necessary tasks in booting.

20.3.2.1 Booting Framework

Raspberry Pi_Eng_20.3.1 Run Level

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.

Raspberry Pi_Eng_20.2.6 Running Script

20.2.6 Running Script

20.2.6.1 Preparing to Run Script File

A script is not a binary executable like a normal executable program, but it must be executable as a program itself. Therefore, the execute permission must be granted to the script file. You can use “chmod” command to grant the desired permission. For the permissions of a file, see the description of [13.2 Changing Permission on File].