Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Case Analysis of foreground and background tasks in Linux

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report