Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement "Multithreading" execution script File in Shell

Shulou Source: shulou.com Published: 2022-06-03 09:19:41 11月15日 Update

This article introduces the knowledge of "how to implement multi-threaded script files in Shell". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

That is, for example, I have 100 executable files that have no special sequential execution relationship with each other, such as CODE:

The code is as follows:

Job_1

Job_2

Job_2

.

Job_100

Want to use csh/bash for multithreaded call execution.

For example, if you open five threads at a time, then job_1,2,3,4,5 starts first, and if any of these threads executes completion first, it continues to execute the next file that has not been executed at the beginning, such as job_6,7,8...., which always executes all 100files with the specified number of threads.

I wanted to use "&" to put it in the background, but in this way I can specify 5 at a time, but I can't know when any of these programs will be finished, so I can't move on to the next program.

The perfect solution:

The code is as follows:

-(dearvoid@LinuxEden:Forum)-(~ / tmp)-

[$$= 6718 $? = 0]; cat job_1

#! / bin/bash

((RANDOM% 5 + 1))

Echo "$0 sleeping for $n seconds..."

Sleep $n

Echo "$0 exiting..."

-(dearvoid@LinuxEden:Forum)-(~ / tmp)-

[$$= 6718 $? = 0]; for ((I = 2; I

Tags: Threads files scripts real code content background more knowledge programs practical successful learning and then successively dilemmas actual situations articles schemes cases Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker MySQL Xiaomi Microsoft macOS