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. 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. Being an absolute beginner with bash scripts, I don't know where to start searching. I have tested that 4 instances of imagesnap works nicely when running them manually from Terminal, but that's about it.
To summarise I'm looking to make a bash script that:
- run multiple instances of imagesnap.
- has user input for some of the arguments for imagesnap.
- ideally start all the imagesnap instances at (almost) the same time.
--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
:
- Run multiple scripts from within the main script
or
Use subshells to run multiple instances of
imagesnap
Start each sub script or subshell in parallel if possible.
Since each instance of
imagesnap
will run until terminated it would be great if they could all be stopped with a single command
bash macos webcam
add a comment |
I am trying to launch multiple instances of imagesnap
simultaneously from a single bash script on a Mac. 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. Being an absolute beginner with bash scripts, I don't know where to start searching. I have tested that 4 instances of imagesnap works nicely when running them manually from Terminal, but that's about it.
To summarise I'm looking to make a bash script that:
- run multiple instances of imagesnap.
- has user input for some of the arguments for imagesnap.
- ideally start all the imagesnap instances at (almost) the same time.
--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
:
- Run multiple scripts from within the main script
or
Use subshells to run multiple instances of
imagesnap
Start each sub script or subshell in parallel if possible.
Since each instance of
imagesnap
will run until terminated it would be great if they could all be stopped with a single command
bash macos webcam
add a comment |
I am trying to launch multiple instances of imagesnap
simultaneously from a single bash script on a Mac. 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. Being an absolute beginner with bash scripts, I don't know where to start searching. I have tested that 4 instances of imagesnap works nicely when running them manually from Terminal, but that's about it.
To summarise I'm looking to make a bash script that:
- run multiple instances of imagesnap.
- has user input for some of the arguments for imagesnap.
- ideally start all the imagesnap instances at (almost) the same time.
--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
:
- Run multiple scripts from within the main script
or
Use subshells to run multiple instances of
imagesnap
Start each sub script or subshell in parallel if possible.
Since each instance of
imagesnap
will run until terminated it would be great if they could all be stopped with a single command
bash macos webcam
I am trying to launch multiple instances of imagesnap
simultaneously from a single bash script on a Mac. 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. Being an absolute beginner with bash scripts, I don't know where to start searching. I have tested that 4 instances of imagesnap works nicely when running them manually from Terminal, but that's about it.
To summarise I'm looking to make a bash script that:
- run multiple instances of imagesnap.
- has user input for some of the arguments for imagesnap.
- ideally start all the imagesnap instances at (almost) the same time.
--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
:
- Run multiple scripts from within the main script
or
Use subshells to run multiple instances of
imagesnap
Start each sub script or subshell in parallel if possible.
Since each instance of
imagesnap
will run until terminated it would be great if they could all be stopped with a single command
bash macos webcam
bash macos webcam
edited Dec 30 '18 at 11:07
Jens Jacob
asked Dec 29 '18 at 18:41
Jens JacobJens Jacob
32
32
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
the following quick hack (saved as run-periodically.sh
) might do the right thing:
#!/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. 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…
Thanks a lot for the suggestion. Though it would work nicely, I will try to do this a bit differently. The imagesnap has the ability to take shots with an interval usingimagesnap -t x.xx
. I will see if I can figure out how to use subshells or separate scripts for each instance of imagesnap.
– Jens Jacob
Dec 30 '18 at 11:08
add a comment |
Some progress...
So far I have managed to start multiple versions of imagesnap
. Here I create a new folder and subfolders for each webcam (webcam is the same in the code for testing).
Problem now is that I can't end all instances of imagesnap
by terminating the script. Any ideas how to do this? Also, is wait
useful/necessary at the end of the script?
#!/bin/bash
mkdir /Users/USER/Desktop/webcam/RUN1 #Modify later to take input: 1=RUN1, 2=RUN2, etc.
cd "$_" #Go to new directory
( #start first subshell
mkdir "FaceTime HD Camera1" #Make subfolder for webcam
cd "$_"
imagesnap -t 5 -d "FaceTime HD Camera" #save image every 5 sec
) & #End first subshell and run in parallel
(
mkdir "FaceTime HD Camera2"
cd "$_"
imagesnap -t 5 -d "FaceTime HD Camera"
) &
(
mkdir "FaceTime HD Camera3"
cd "$_"
imagesnap -t 5 -d "FaceTime HD Camera"
) &
(
mkdir "FaceTime HD Camera4"
cd "$_"
imagesnap -t 5 -d "FaceTime HD Camera"
) &
For some reasonimagesnap
does not like being run multiple times with multiple cameras. It randomly chooses a number of cameras to record from.
– Jens Jacob
Jan 2 at 17:51
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53972356%2fhow-to-run-multiple-instances-of-command-line-tool-in-bash-script-user-input%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
the following quick hack (saved as run-periodically.sh
) might do the right thing:
#!/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. 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…
Thanks a lot for the suggestion. Though it would work nicely, I will try to do this a bit differently. The imagesnap has the ability to take shots with an interval usingimagesnap -t x.xx
. I will see if I can figure out how to use subshells or separate scripts for each instance of imagesnap.
– Jens Jacob
Dec 30 '18 at 11:08
add a comment |
the following quick hack (saved as run-periodically.sh
) might do the right thing:
#!/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. 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…
Thanks a lot for the suggestion. Though it would work nicely, I will try to do this a bit differently. The imagesnap has the ability to take shots with an interval usingimagesnap -t x.xx
. I will see if I can figure out how to use subshells or separate scripts for each instance of imagesnap.
– Jens Jacob
Dec 30 '18 at 11:08
add a comment |
the following quick hack (saved as run-periodically.sh
) might do the right thing:
#!/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. 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…
the following quick hack (saved as run-periodically.sh
) might do the right thing:
#!/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. 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…
answered Dec 29 '18 at 22:05
Sam MasonSam Mason
3,25211330
3,25211330
Thanks a lot for the suggestion. Though it would work nicely, I will try to do this a bit differently. The imagesnap has the ability to take shots with an interval usingimagesnap -t x.xx
. I will see if I can figure out how to use subshells or separate scripts for each instance of imagesnap.
– Jens Jacob
Dec 30 '18 at 11:08
add a comment |
Thanks a lot for the suggestion. Though it would work nicely, I will try to do this a bit differently. The imagesnap has the ability to take shots with an interval usingimagesnap -t x.xx
. I will see if I can figure out how to use subshells or separate scripts for each instance of imagesnap.
– Jens Jacob
Dec 30 '18 at 11:08
Thanks a lot for the suggestion. Though it would work nicely, I will try to do this a bit differently. The imagesnap has the ability to take shots with an interval using
imagesnap -t x.xx
. I will see if I can figure out how to use subshells or separate scripts for each instance of imagesnap.– Jens Jacob
Dec 30 '18 at 11:08
Thanks a lot for the suggestion. Though it would work nicely, I will try to do this a bit differently. The imagesnap has the ability to take shots with an interval using
imagesnap -t x.xx
. I will see if I can figure out how to use subshells or separate scripts for each instance of imagesnap.– Jens Jacob
Dec 30 '18 at 11:08
add a comment |
Some progress...
So far I have managed to start multiple versions of imagesnap
. Here I create a new folder and subfolders for each webcam (webcam is the same in the code for testing).
Problem now is that I can't end all instances of imagesnap
by terminating the script. Any ideas how to do this? Also, is wait
useful/necessary at the end of the script?
#!/bin/bash
mkdir /Users/USER/Desktop/webcam/RUN1 #Modify later to take input: 1=RUN1, 2=RUN2, etc.
cd "$_" #Go to new directory
( #start first subshell
mkdir "FaceTime HD Camera1" #Make subfolder for webcam
cd "$_"
imagesnap -t 5 -d "FaceTime HD Camera" #save image every 5 sec
) & #End first subshell and run in parallel
(
mkdir "FaceTime HD Camera2"
cd "$_"
imagesnap -t 5 -d "FaceTime HD Camera"
) &
(
mkdir "FaceTime HD Camera3"
cd "$_"
imagesnap -t 5 -d "FaceTime HD Camera"
) &
(
mkdir "FaceTime HD Camera4"
cd "$_"
imagesnap -t 5 -d "FaceTime HD Camera"
) &
For some reasonimagesnap
does not like being run multiple times with multiple cameras. It randomly chooses a number of cameras to record from.
– Jens Jacob
Jan 2 at 17:51
add a comment |
Some progress...
So far I have managed to start multiple versions of imagesnap
. Here I create a new folder and subfolders for each webcam (webcam is the same in the code for testing).
Problem now is that I can't end all instances of imagesnap
by terminating the script. Any ideas how to do this? Also, is wait
useful/necessary at the end of the script?
#!/bin/bash
mkdir /Users/USER/Desktop/webcam/RUN1 #Modify later to take input: 1=RUN1, 2=RUN2, etc.
cd "$_" #Go to new directory
( #start first subshell
mkdir "FaceTime HD Camera1" #Make subfolder for webcam
cd "$_"
imagesnap -t 5 -d "FaceTime HD Camera" #save image every 5 sec
) & #End first subshell and run in parallel
(
mkdir "FaceTime HD Camera2"
cd "$_"
imagesnap -t 5 -d "FaceTime HD Camera"
) &
(
mkdir "FaceTime HD Camera3"
cd "$_"
imagesnap -t 5 -d "FaceTime HD Camera"
) &
(
mkdir "FaceTime HD Camera4"
cd "$_"
imagesnap -t 5 -d "FaceTime HD Camera"
) &
For some reasonimagesnap
does not like being run multiple times with multiple cameras. It randomly chooses a number of cameras to record from.
– Jens Jacob
Jan 2 at 17:51
add a comment |
Some progress...
So far I have managed to start multiple versions of imagesnap
. Here I create a new folder and subfolders for each webcam (webcam is the same in the code for testing).
Problem now is that I can't end all instances of imagesnap
by terminating the script. Any ideas how to do this? Also, is wait
useful/necessary at the end of the script?
#!/bin/bash
mkdir /Users/USER/Desktop/webcam/RUN1 #Modify later to take input: 1=RUN1, 2=RUN2, etc.
cd "$_" #Go to new directory
( #start first subshell
mkdir "FaceTime HD Camera1" #Make subfolder for webcam
cd "$_"
imagesnap -t 5 -d "FaceTime HD Camera" #save image every 5 sec
) & #End first subshell and run in parallel
(
mkdir "FaceTime HD Camera2"
cd "$_"
imagesnap -t 5 -d "FaceTime HD Camera"
) &
(
mkdir "FaceTime HD Camera3"
cd "$_"
imagesnap -t 5 -d "FaceTime HD Camera"
) &
(
mkdir "FaceTime HD Camera4"
cd "$_"
imagesnap -t 5 -d "FaceTime HD Camera"
) &
Some progress...
So far I have managed to start multiple versions of imagesnap
. Here I create a new folder and subfolders for each webcam (webcam is the same in the code for testing).
Problem now is that I can't end all instances of imagesnap
by terminating the script. Any ideas how to do this? Also, is wait
useful/necessary at the end of the script?
#!/bin/bash
mkdir /Users/USER/Desktop/webcam/RUN1 #Modify later to take input: 1=RUN1, 2=RUN2, etc.
cd "$_" #Go to new directory
( #start first subshell
mkdir "FaceTime HD Camera1" #Make subfolder for webcam
cd "$_"
imagesnap -t 5 -d "FaceTime HD Camera" #save image every 5 sec
) & #End first subshell and run in parallel
(
mkdir "FaceTime HD Camera2"
cd "$_"
imagesnap -t 5 -d "FaceTime HD Camera"
) &
(
mkdir "FaceTime HD Camera3"
cd "$_"
imagesnap -t 5 -d "FaceTime HD Camera"
) &
(
mkdir "FaceTime HD Camera4"
cd "$_"
imagesnap -t 5 -d "FaceTime HD Camera"
) &
answered Dec 30 '18 at 15:41
Jens JacobJens Jacob
32
32
For some reasonimagesnap
does not like being run multiple times with multiple cameras. It randomly chooses a number of cameras to record from.
– Jens Jacob
Jan 2 at 17:51
add a comment |
For some reasonimagesnap
does not like being run multiple times with multiple cameras. It randomly chooses a number of cameras to record from.
– Jens Jacob
Jan 2 at 17:51
For some reason
imagesnap
does not like being run multiple times with multiple cameras. It randomly chooses a number of cameras to record from.– Jens Jacob
Jan 2 at 17:51
For some reason
imagesnap
does not like being run multiple times with multiple cameras. It randomly chooses a number of cameras to record from.– Jens Jacob
Jan 2 at 17:51
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53972356%2fhow-to-run-multiple-instances-of-command-line-tool-in-bash-script-user-input%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown