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

How to use & number and pipe symbol in linux

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail how to use the & sign and pipe symbol in linux. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

It is also important to master the use of connection symbols between connection commands. In fact, the use of commands is not difficult. For example, mkdir, touch, and find can be simply summarized as "create a new directory", "update files", and "find files in the directory tree" respectively.

But if you want to understand

Mkdir test_dir 2 > / dev/null | | touch images.txt & & find. -iname "* jpg" > backup/dir/images.txt &

The purpose of this series of commands, and why it is written in this way, is not so simple.

The key point is the connection symbol between the commands. Mastering the use of these symbols will not only give you a better understanding of the overall working principle, but also let you know how to effectively combine different commands to improve your work efficiency.

In this and the next article, I will show you how to use the & sign and the pipe symbol (|) in different scenarios.

Work behind the scenes

Let me give you a simple example of how to use the & sign to run the following command in the background:

Cp-R original/dir/ backup/dir/

The purpose of this command is to recursively copy the contents of original/dir/ into backup/dir/. Although it looks simple, if the file in the original directory is too large, the terminal will be stuck during execution.

So, you can put an & sign at the end of the command and put the task in the background to perform:

Cp-R original/dir/ backup/dir/ &

After the task is put in the background, you can immediately continue to work on the same terminal, and even closing the terminal does not affect the normal execution of the task. It is important to note that if you ask this task to output to standard output (such as echo or ls), even if you use &, you will wait for those output tasks to finish running in the foreground.

When you use & to put a process to run in the background, Bash prompts the process ID of that process. Every process running in the Linux system has a unique process ID, you can use the process ID to pause, resume or terminate the corresponding process, so the process ID is very important.

At this time, as long as you are still in the terminal that starts the process, you can use the following commands to manage the background process:

The jobs command can display the processes currently running at the terminal, including those running in the foreground and in the background. It assigns a sequence number to each executing process task (this sequence number is not a process ID), and you can use these sequence numbers to refer to each process task.

$jobs [1]-Running cp-I-R original/dir/* backup/dir/ & [2] + Running find. -iname "* jpg" > backup/dir/images.txt &

The fg command allows the process tasks running in the background to be run in the foreground, which makes it easier to interact. According to the sequence number of the process task provided by the jobs command, and preceded by the% symbol, the corresponding process task can be put to the foreground to run.

$fg 1 # put the cp task with serial number 1 above to the foreground to run cp-I-R original/dir/* backup/dir/

If the process task is paused, the fg command starts it up.

Using the ctrl+z key combination, you can pause a task running in the foreground, just pause, rather than terminate the task. When a task is restarted using the fg or bg command, the task starts execution from the paused location. However, the sleep command is a special case, and the time when the sleep task is paused is counted in the sleep time. Because the sleep command is based on the time of the system clock, not the actual running time. That is, if you run sleep 30 and then pause the task for more than 30 seconds, the task will be terminated and exited immediately when it resumes execution. The bg command places the task in the background and starts if the task is paused.

$bg 1 [1] + cp-I-R original/dir/* backup/dir/ &

As mentioned above, the above commands can only be used in the same terminal. If the terminal that starts the process task is closed, or if it is switched to another terminal, the above commands cannot be used.

If you want to manage background processes on another terminal, you need other tools. For example, you can use the kill command to terminate a process from another terminal:

Kill-s STOP

The PID here is the process ID that the Bash displays when you put the process in the background. If you didn't record the process ID at that time, you can also use the ps command (for process) to get the process ID of all running processes, like this:

Ps | grep cp

After execution, all processes that contain the cp string, such as the cp process in the above example, are displayed. The corresponding process ID is also displayed:

$ps | grep cp14444 pts/3 00:00:13 cp

In this example, the process ID is 14444, so you can use the following command to pause the background process:

Kill-s STOP 14444 note that STOP here is equivalent to the effect of the ctrl+z key combination mentioned earlier, that is, just pause the process.

If you want to start a paused process, you can CONT the process:

Kill-s CONT 14444 gives a list of common signals that can be sent to a process. If you want to terminate a process, you can send a TERM signal: kill-s TERM 14444 if the process does not respond to the TERM signal and refuses to exit, you can also send a KILL signal to force the termination of the process: kill-s KILL 14444 forced termination of the process may have a certain risk, but such a signal can still come in handy if the process consumes resources uncontrollably.

In addition, if you are not sure whether the process ID is correct, you can add the x parameter to the ps command:

$ps x | grep cp14444 pts/3 D 0:14 cp-I-R original/dir/Hols_2014.mp4original/dir/Hols_2015.mp4 original/dir/Hols_2016.mp4original/dir/Hols_2017.mp4 original/dir/Hols_2018.mp4 backup/dir/

So you can see if it's the process ID you need.

Finally, I introduce a command that combines ps and grep:

$pgrep cp8181926334047546172889613633966801373514444

Pgrep can directly display the process ID of a process with the string cp.

You can add some parameters to make its output clearer:

$pgrep-lx cp14444 cp

Here, the-l parameter asks pgrep to display the name of the process, and the-x parameter makes pgrep exactly match the command cp. If you want more details about this command, try running pgrep-ax.

This is the end of the article on "how to use the & sign and pipe symbol in linux". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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