Raspberry Pi_Eng_10.7.3 Executing Program on Background/Foreground


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


10.7.3  Executing Program on Background/Foreground

 

Normally when we execute a command, after one command is completed, the next command is executed. We call this method of processing the foreground mode. In this way, another command can not be entered while you are working on one operation. The Raspberry Pi system always executes the command in foreground mode unless any special instruction is not issued.

Some of the tasks that you perform on the system may end in a short time, but there may be work that takes quite a while. If you have a job that takes a long time, you can start another job after it finishes, but if the next job to process is not related to the current processing job, it is possible to start the next job immediately without waiting for the current processing job to finish.

 

10.7.3.1    "&" Command - Execute on Background

 

While the current processing task is not completed, the way to execute the next one is to execute the first command in background. If you execute the first command in background like this, you can proceed to the next command immediately on the Terminal screen.

To execute a task in the background, use the "&" command after the desired command.

 

[Command Format]

exec-command  &   [ exec-command ]

 

[Command Overview]

   This executes the command specified in front of this command in background.

   User privilege          -- Normal user.

 

[Detail Description]

When a specific job is executed in the background, the screen displays the PID (Process ID) of the job, and turns into the state where next job can be done. This PID is a unique number indicating the task.

 

The output for the result of the background operation is displayed at the completion of the operation, which is displayed in the same manner as the normal operation.

 

[Used Example]

We will use the following "man" command as an example. You can usually get help on a command with the "man" command, which comes up immediately upon execution of the program. Here we will find help for the "mkdir" command, and try to run the task with a background.

 

pi@raspberrypi ~/testdata $ man mkdir &

 [5] 10914

pi@raspberrypi ~/testdata $

 

Then, the screen displays the PID (Process ID) of the job executed in the background, and immediately turns into the state where next command can be entered.

We will now run another command. Then, normal result is displayed on the screen.

 

pi@raspberrypi ~/testdata $ ls l

total 24

-rw-r--r-- 1 pi pi   62 Apr 23 13:06 customer_list.txt

-rw-r--r-- 1 pi pi  123 Apr 23 13:00 customer_product.txt

drwxr-xr-x 4 pi pi 4096 Apr 25 07:56 TestFolder01

drwxr-xr-x 2 pi pi 4096 Apr 10 13:32 TestFolder02

drwxr-xr-x 2 pi pi 4096 Apr 25 08:11 TestFolder03

-rw-rw---- 1 pi pi  113 Apr 23 08:22 user_guide01.txt

 

 


 

10.7.3.2    "jobs" Command Check Current Background Job

 

Use the "jobs" command to confirm the list of the currently running background jobs and their status for each job.

 

[Command Format]

jobs

 

[Command Overview]

   This shows information about the currently running background task list.

   User privilege          -- Normal user.

 

[Detail Description]

   None

 

 [사용 Example

The next is to run the "man" command with background and then look up the status of the background operation.

 

pi@raspberrypi ~/testdata $ man mkdir &

[3] 2472

pi@raspberrypi ~/testdata $ jobs

[1]+  Stopped                 vi

[2]+  Stopped                 python3

[3]+  Stopped                 man mkdir

 

In the above, the job number is first displayed in the "[n]" format, followed by a description of what the job is.

 

 

 


 

10.7.3.3    Moving Foreground Job to Background

 

Even though you execute the current processing job in foreground with the expectation that the job will be completed in a short time, it may not comple for a long time. In this case, if you want to turn the current processing foreground job into background and run another task in the foreground, you can switch the foreground operation to background by pressing [Ctrl + Z]. Then, the job switched to the background is waiting with stopped state.

 

[Used Example]

If you press [Ctrl + Z] button while executing "man mkdir" in the foreground, the task changes into background and you can enter new command in the foreground.

 

pi@raspberrypi ~/testdata $ man mkdir

 

pi@raspberrypi ~/testdata $

 

pi@raspberrypi ~/testdata $ jobs

[1]+  Stopped                 vi

[2]+  Stopped                 python3

[3]+  Stopped                 man mkdir

 


 

10.7.3.4    "fg" Command Switch Back Job to Foreground

 

If you use the "fg" command while the background job is running, the job that is running in the background changes to the foreground mode.

 

[Command Format]

fg

 

[Command Overview]

   This changes a job that is running in the background to the state of foreground.

   User privilege          -- Normal user.

 

[Detail Description]

Working with backgrounds is difficult to manage tasks and not easy to use, unless you are an intermediate user or higher. So a more understandable way to get the same effect as background is to open up a new Terminal screen and work on each one separately.

 

[Used Example]

Here, wou will run the "man" command with background. Then, the screen displays the PID (Process ID) of the job executed in the background, and turns into the state where next command can be entered immediately.

 

pi@raspberrypi ~/testdata $ man mkdir &

 [5] 10914

pi@raspberrypi ~/testdata $

 

 

Now, with the "fg" command, let's switch the the program working in background to the foreground. Then the "man" command that was working in the background is switched to the foreground and the work screen appears as follows.

 

pi@raspberrypi ~/testdata $ fg

 


 

 


 

10.7.3.5    "bg" Command Resume Paused Background Job

 

[Command Format]

bg %n

 

[Command Overview]

   This resumes the suspended job in background.

   User privilege          -- Normal user.

 

[Detail Description]

When you run the command, you must specify job ID number that system specified for that job. This number can be checked by issuing the "jobs" command.

 

[Used Example]

 

pi@raspberrypi ~/testdata $ jobs

[1]+  Stopped                 vi

[2]+  Stopped                 python3

[3]+  Stopped                 man mkdir

pi@raspberrypi ~/testdata $  bg %3

 

 

 


 

10.7.3.6    Changing Background Job into Pausing State

 

You can not immediately stop a job running in the background. If you want to stop the task in the background, you may switch the task to foreground first and then press Ctrl + Z again to stop it in background.

 


 

10.7.3.7    "kill" Command Terminate Job

 

Use the "kill" command to terminate or kill the job. This command is also used to terminate the process, and action to be taken is determined by parameters that are passed.

 

[Command Format]

kill %n

 

[Command Overview]

   This terminates the process running currently. 

   User privilege          -- Normal user.

 

[Detail Description]

You must specify '%' with the job number when executing the command. If you omit this and just type "kill 1" or "kill 2", you will end up with either PID 1 "init" or PID 2 "kthread", which may cause to reboot your computer with a different system program.