如何制作一个同时创建 40 个程序实例的 bash 脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2220030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 21:39:19  来源:igfitidea点击:

How to make a bash script that creates 40 simultaneous instances of a program?

bashloopsscripting

提问by chris

I am new to bashand Linux. I have a program I have written that I want to create multiple simultaneous instances of.

我是bash和 的新手Linux。我有一个我写的程序,我想同时创建多个实例。

Right now, I do this by opening up 10 new terminals, and then running the program 10 times (the command I run is php /home/calculatedata.php

现在,我通过打开 10 个新终端,然后运行该程序 10 次(我运行的命令是 php /home/calculatedata.php

What is the simplest way to do this using a bash script? Also, I need to know how to kill the instances because they are running an infinite loop.

使用 bash 脚本执行此操作的最简单方法是什么?另外,我需要知道如何杀死实例,因为它们正在运行无限循环。

Thanks!!

谢谢!!

采纳答案by sth

You can use a loop and start the processes in the background with &:

您可以使用循环并在后台启动进程&

for (( i=0; i<40; i++ )); do
   php /home/calculatedata.php &
done

If these processes are the only instances of PHP you have running and you want to kill them all, the easiest way is killall:

如果这些进程是您运行的唯一 PHP 实例并且您想将它们全部杀死,最简单的方法是killall

killall php

回答by ghostdog74

for instance in {1..40}
do
  php myscript &
done

回答by codaddict

How about running the php process in the background:

在后台运行php进程怎么样:

#!/bin/bash
for ((i=1;i<=40;i+=1)); do
  php /home/calculatedata.php &
done

You can terminate all the instances of these background running PHP process by issuing:

您可以通过发出以下命令来终止这些后台运行 PHP 进程的所有实例:

killall php

Make sure you don't have any other php processes running, as they too will be killed. If you have many other PHP processes, then you do something like:

确保您没有任何其他 php 进程正在运行,因为它们也会被杀死。如果您有许多其他 PHP 进程,那么您可以执行以下操作:

ps -ef | grep /home/calculatedata.php | cut_the_pid | kill -9

回答by Tjunier

if you have the seq(1)program (there are chances that you have it), you can do it in a slightly more readable way, like this:

如果你有这个seq(1)程序(有可能你有它),你可以用一种更易读的方式来做,像这样:

for n in $(seq 40); do
   mycmd &
done

In this case the nvariable isn't used. Hope this helps.

在这种情况下,n不使用变量。希望这可以帮助。

回答by nobody

You can start the instances with a simple loop and a terminating "&" to run each job in the background:

您可以使用一个简单的循环和一个终止的“&”来启动实例以在后台运行每个作业:

INSTANCES=40
for ((i=0; $i<$INSTANCES; ++i))
do
    mycmd &
done

回答by Christopher Hicks

This script is a loop that creates instances of the 'while true; do :' loop. It continues making jobs until it's cancelled, and all of the jobs run in the background. You can add variables for index counter and change : to your code.

这个脚本是一个循环,它创建 'while true; 的实例;做:'循环。它会继续创建作业直到它被取消,并且所有作业都在后台运行。您可以为索引计数器添加变量并更改 : 到您的代码。

while true; do while true; do :; done & done

while true; do while true; do :; done & done

to stop:

停止:

killall bash

killall bash