Get the App
SLTechnology News&Howtos  ›  Development  › 

Case Analysis of foreground and background tasks in Linux

Shulou Source: shulou.com Published: 2022-06-01 17:33:17 09月29日 Update

This article mainly explains the "Linux foreground and background task case analysis", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "Linux foreground and background task case analysis" bar!

For task management, we generally have the following requirements:

Switch the process to the foreground

Switch the process to the background

View background tasks

Terminate a background task

To demonstrate these requirements, we came up with the great Hello World program:

Root@jaking-virtual-machine:~# ls-l# these three scripts have the same source code: total 12-rwxr-xr-x 1 root root 70 Feb 21 17:25 HelloWorld1.sh-rwxr-xr-x 1 root root 70 Feb 21 17:25 HelloWorld2.sh-rwxr-xr-x 1 root root 70 Feb 21 17:26 HelloWorld3.shroot@jaking-virtual-machine:~# cat HelloWorld1.shroud HelloWorld3.shroot@jaking-virtual-machine:~# cat truedo echo while truedo echo "HelloWorld!" Sleep 1done

Start background tasks

Root@jaking-virtual-machine:~#. / HelloWorld1.sh > test1.txt & [1] 65139root@jaking-virtual-machine:~#. / HelloWorld2.sh > test1.txt & [2] 65145root@jaking-virtual-machine:~#. / HelloWorld3.sh > test1.txt & [3] 65155

Jobs-l View background tasks

Root@jaking-virtual-machine:~# jobs-l [1] 65139 Running. / HelloWorld1.sh > test1.txt & [2]-65145 Running. / HelloWorld2.sh > test1.txt & [3] + 65155 Running. / HelloWorld3.sh > test1.txt &

Fg transfers the assigned background task to the foreground.

Root@jaking-virtual-machine:~# fg% 2./HelloWorld2.sh > test 1.txt ^ Z # Ctrl + Z cut the foreground task to the background and stop running [2] + Stopped. / HelloWorld2.sh > test1.txtroot@jaking-virtual-machine:~# jobs-l [1] 65139 Running. / HelloWorld1.sh > test1.txt & [2] + 65145 Stopped. / HelloWorld2.sh > test1. Txt [3]-65155 Running. / HelloWorld3.sh > test1.txt &

Bg reruns tasks that stop running in the background

Root@jaking-virtual-machine:~# bg% 2 [2] +. / HelloWorld2.sh > test1.txt & root@jaking-virtual-machine:~# jobs-l [1] 65139 Running. / HelloWorld1.sh > test1.txt & [2]-65145 Running. / HelloWorld2.sh > test1.txt & [3] + 65155 Running. / HelloWorld3.sh > test1.txt &

Kill kills the background process

Root@jaking-virtual-machine:~# kill 65145root@jaking-virtual-machine:~# jobs-l [1] 65139 Running. / HelloWorld1.sh > test1.txt & [2]-65145 Terminated. / HelloWorld2.sh > test1.txt [3] + 65155 Running. / HelloWorld3.sh > test1.txt & root@jaking-virtual-machine:~# kill% 3root@jaking-virtual-machine:~# jobs-l [ 1]-65139 Running. / HelloWorld1.sh > test1.txt & [2]-65145 Terminated. / HelloWorld2.sh > test1.txt [3] + 65155 Terminated. / HelloWorld3.sh > test1.txt Thank you for your reading The above is the content of "Linux foreground and background task case analysis". After the study of this article, I believe you have a deeper understanding of the problem of Linux foreground and background task example analysis, and the specific use situation still needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Tasks background examples case analysis analysis foreground process learning running content requirements switching great same three that is ideas situations articles and more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Huawei macOS Shulou Information OPPO Reno