简体   繁体   English

如何在bash脚本中运行命令行工具的多个实例? +脚本的用户输入

[英]How to run multiple instances of command-line tool in bash script? + user input for script

I am trying to launch multiple instances of imagesnap simultaneously from a single bash script on a Mac. 我正在尝试从Mac上的单个bash脚本同时启动多个imagesnap实例。 Also, it would be great to give (some of) the arguments by user input when running the script. 同样,在运行脚本时通过用户输入提供(某些)自变量也很好。

I have 4 webcams connected, and want to take series of images from each camera with a given interval. 我连接了4个网络摄像头,并希望以给定的间隔从每个摄像机拍摄一系列图像。 Being an absolute beginner with bash scripts, I don't know where to start searching. 作为bash脚本的绝对初学者,我不知道从哪里开始搜索。 I have tested that 4 instances of imagesnap works nicely when running them manually from Terminal, but that's about it. 我已经测试了从终端手动运行图像快照的4个实例,它们可以很好地工作,但是仅此而已。

To summarise I'm looking to make a bash script that: 总而言之,我正在寻找一个bash脚本,它可以:

  • run multiple instances of imagesnap. 运行imagesnap的多个实例。
  • has user input for some of the arguments for imagesnap. 用户输入了imagenap的某些参数。
  • ideally start all the imagesnap instances at (almost) the same time. 理想情况下(几乎)同时启动所有imagenap实例。

--EDIT-- - 编辑 -

After thinking about this I have a vague idea of how this script could be organised using the ability to take interval images with imagesnap -t x.xx : 考虑了这一点之后,我对如何使用imagesnap -t x.xx拍摄间隔图像的能力如何组织此脚本有了一个模糊的想法:

  • Run multiple scripts from within the main script 从主脚本中运行多个脚本

or 要么

  • Use subshells to run multiple instances of imagesnap 使用子Shell运行imagesnap多个实例

  • Start each sub script or subshell in parallel if possible. 如果可能,请并行启动每个子脚本或子Shell。

  • Since each instance of imagesnap will run until terminated it would be great if they could all be stopped with a single command 由于imagesnap每个实例将一直运行到终止,因此如果可以使用单个命令将其全部停止,那将是一个很好的选择

the following quick hack (saved as run-periodically.sh ) might do the right thing: 以下快速技巧(保存为run-periodically.sh )可能会做正确的事情:

#!/bin/bash

interval=5
start=$(date +%s)

while true; do
    # run jobs in the background
    for i in 1 2 3 4; do
        "$@" &
    done
    # wait for all background jobs to finish
    wait
    # figure out how long we have to sleep
    end=$(date +%s)
    delta=$((start + interval - end))
    # if it's positive sleep for this amount of time
    if [ $delta -gt 0 ]; then
        sleep $delta || exit
    fi
    start=$((start + interval))
done

if you put this script somewhere appropriate and make it executable, you can run it like: 如果将此脚本放在适当的位置并使其可执行,则可以像以下方式运行它:

run-periodically.sh imagesnap arg1 arg2

but while testing, I ran with: 但是在测试时,我跑了:

sh run-periodically.sh sh -c "date; sleep 2"

which will cause four copies of "start a shell that displays the date then waits a couple of seconds" to be run in parallel every interval seconds. 这将导致interval几秒钟并行运行“启动一个显示日期然后等待几秒钟的外壳程序”的四个副本。 if you want to run different things in the different jobs, then you might want to put them into this script explicitly or maybe another script which this one calls… 如果您想在不同的工作中运行不同的事物,那么您可能希望将它们显式地放入此脚本或该脚本所调用的另一脚本中……

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM