How to plan a task to run after another already running task in bash?
up vote
9
down vote
favorite
I'm looking for something like command1 ; command2
i.e. how to run command2
after command1
but I'd like to plan execution of command2
when command1
is already running.
It can be solved by just typing the command2
and confirming by Enter supposed that the command1
is not consuming standard input and that the command1
doesn't produce to much text on output making it impractical to type (typed characters are blended with the command1
output).
bash job-control jobs
add a comment |
up vote
9
down vote
favorite
I'm looking for something like command1 ; command2
i.e. how to run command2
after command1
but I'd like to plan execution of command2
when command1
is already running.
It can be solved by just typing the command2
and confirming by Enter supposed that the command1
is not consuming standard input and that the command1
doesn't produce to much text on output making it impractical to type (typed characters are blended with the command1
output).
bash job-control jobs
add a comment |
up vote
9
down vote
favorite
up vote
9
down vote
favorite
I'm looking for something like command1 ; command2
i.e. how to run command2
after command1
but I'd like to plan execution of command2
when command1
is already running.
It can be solved by just typing the command2
and confirming by Enter supposed that the command1
is not consuming standard input and that the command1
doesn't produce to much text on output making it impractical to type (typed characters are blended with the command1
output).
bash job-control jobs
I'm looking for something like command1 ; command2
i.e. how to run command2
after command1
but I'd like to plan execution of command2
when command1
is already running.
It can be solved by just typing the command2
and confirming by Enter supposed that the command1
is not consuming standard input and that the command1
doesn't produce to much text on output making it impractical to type (typed characters are blended with the command1
output).
bash job-control jobs
bash job-control jobs
edited 1 hour ago
asked 15 hours ago
czerny
4731311
4731311
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
up vote
13
down vote
start a command
command1
pause the execution by pressing Ctrl+Z
find the job number of the paused command (it's usually already printed to console when to console when to command is paused) by executing
jobs
let
command1
continue in background
bg
plan execution of
command2
to await finish ofcommand1
wait -n <command1 job number> ; command2
Documentation Job Control Builtins
You can 'accept' your own answer (click the tick icon) into order to show other users that there is a good answer to your question.
– sudodus
14 hours ago
1
@sudodus Own answer can be accepted at earliest 2 days after the question was asked. stackoverflow.blog/2009/01/06/accept-your-own-answers
– czerny
14 hours ago
6
In simple cases I sometimes control-z then usefg ; command
. Or evenfg && command
to only run the 2nd command / pipeline if the first one was successful. (You can still usebg
while thinking / typing, if it doesn't spew text on the terminal.)
– Peter Cordes
12 hours ago
1
I'm a bit confused as to what this approach offers beyond the typicalcommand1; command2
syntax. Could you please clarify this for me?
– Haxiel
8 hours ago
1
command1 & proceed_after=$!
then when you've got the next command figured outwait -n $proceed_after; command 2
– studog
7 hours ago
|
show 1 more comment
up vote
6
down vote
Generally what I do is: Ctrl+Z fg && command2
Ctrl+Z to pause it and let you type more in the shell.- Optionally
bg
, to resume command1 in the background while you type out command2.
fg && command2
to resume command1 in the foreground and queue up command2 afterwards if command1 succeeds. You can of course substitute;
or||
for the&&
if you so desire.
1
Stick abg
in there and win!
– Joshua
5 hours ago
1
@Joshua okay, what do I win now :P
– The Guy with The Hat
5 hours ago
add a comment |
up vote
2
down vote
There are several alternatives.
With one ampersand, 'push to background', the second program starts after the first one starts, but they will probably run alongside each other.
command1 & command2
With two ampersands, 'logical and', the second program will start only if the first program finished successfully.
command1 && command2
With a semicolon separating the commands in the command line, the the second program will start, when the first program finished, even if it failed or was interrupted.
command1 ; command2
You can use
wait <PID>
to wait for the first command to finish, if it is already running from the same shell (in the same terminal window).
Otherwise if the first command is already running from another shell (in another window), you can use a small while loop using ps to check if the PID is still found by
ps
. When it is no longer found, the second command will be started.
This demo example for
bash
checks iftop
is running via the PID, and runs the command
echo "*** $USER, I am ready now ***"
if/when
top
is no longer running.
pid=$(ps -A|grep top|cut -d ' ' -f 1);
while [ "$pid" != "" ]; do ps -A|grep "$pid" > /dev/null;
if [ $? -eq 0 ]; then sleep 5;else pid=""; fi; done;
echo "*** $USER, I am ready now ***"
add a comment |
up vote
1
down vote
The standard way is just to let command 1 start in the background and then have command 2 start either in the background or in the foreground. The full command is this:
command1 & command2
&
is not a connector between these two commands. It can be used with command1
alone to have it started in the background:
command1 &
And this is right syntax for starting both commands in the background:
command1 & command2 &
Thank you for your answer. I don't mind if commands are running in background or foreground. I'd like to run them serially (one starts when the previous one ends) and I'd like to plan the execution of the second one when the first one is already running.
– czerny
14 hours ago
@czerny So you want the second one to start while the first one is still running, when it's already finished, or?
– Tomasz
14 hours ago
I'd like the second command to be started when the first command finishes
– czerny
14 hours ago
1
@czerny Then just use;
. Or do you want to do something while the first one is still running?
– Tomasz
13 hours ago
add a comment |
up vote
-2
down vote
If you have a choice to use two terminals, here's what you can do:
- Assuming you are already running command1 in a terminal (let's call it terminal1)
- Start a new terminal (let's call it terminal2)
- Find process ID of command1 (let's call it command1_pid). In terminal2, run command
ps -ef | grep <command1>
(you might want to properly form the filter string in grep command so that you do not accidently find another process having as a substring). - Run command
wait <command1_pid> ; command2
Basically, wait
is a bash built-in command that waits until the provided PIDs exit and then return. You are simply delegating your waiting for command1 to finish, on to bash's wait
command.
New contributor
2
It doesn't seem to work for me. Runningwait
in different terminal outputs following error message:bash: wait: pid 19531 is not a child of this shell
.
– czerny
14 hours ago
@czerny That's normal, I don't think this answer was tested. POSIXwait
system calls only work on child processes, so one bash process can't wait for the children of a different instance of bash. man7.org/linux/man-pages/man2/wait.2.html#ERRORS sayswait
andwaitpid
return withECHILD
error status in this case.
– Peter Cordes
12 hours ago
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
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',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2funix.stackexchange.com%2fquestions%2f487955%2fhow-to-plan-a-task-to-run-after-another-already-running-task-in-bash%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
13
down vote
start a command
command1
pause the execution by pressing Ctrl+Z
find the job number of the paused command (it's usually already printed to console when to console when to command is paused) by executing
jobs
let
command1
continue in background
bg
plan execution of
command2
to await finish ofcommand1
wait -n <command1 job number> ; command2
Documentation Job Control Builtins
You can 'accept' your own answer (click the tick icon) into order to show other users that there is a good answer to your question.
– sudodus
14 hours ago
1
@sudodus Own answer can be accepted at earliest 2 days after the question was asked. stackoverflow.blog/2009/01/06/accept-your-own-answers
– czerny
14 hours ago
6
In simple cases I sometimes control-z then usefg ; command
. Or evenfg && command
to only run the 2nd command / pipeline if the first one was successful. (You can still usebg
while thinking / typing, if it doesn't spew text on the terminal.)
– Peter Cordes
12 hours ago
1
I'm a bit confused as to what this approach offers beyond the typicalcommand1; command2
syntax. Could you please clarify this for me?
– Haxiel
8 hours ago
1
command1 & proceed_after=$!
then when you've got the next command figured outwait -n $proceed_after; command 2
– studog
7 hours ago
|
show 1 more comment
up vote
13
down vote
start a command
command1
pause the execution by pressing Ctrl+Z
find the job number of the paused command (it's usually already printed to console when to console when to command is paused) by executing
jobs
let
command1
continue in background
bg
plan execution of
command2
to await finish ofcommand1
wait -n <command1 job number> ; command2
Documentation Job Control Builtins
You can 'accept' your own answer (click the tick icon) into order to show other users that there is a good answer to your question.
– sudodus
14 hours ago
1
@sudodus Own answer can be accepted at earliest 2 days after the question was asked. stackoverflow.blog/2009/01/06/accept-your-own-answers
– czerny
14 hours ago
6
In simple cases I sometimes control-z then usefg ; command
. Or evenfg && command
to only run the 2nd command / pipeline if the first one was successful. (You can still usebg
while thinking / typing, if it doesn't spew text on the terminal.)
– Peter Cordes
12 hours ago
1
I'm a bit confused as to what this approach offers beyond the typicalcommand1; command2
syntax. Could you please clarify this for me?
– Haxiel
8 hours ago
1
command1 & proceed_after=$!
then when you've got the next command figured outwait -n $proceed_after; command 2
– studog
7 hours ago
|
show 1 more comment
up vote
13
down vote
up vote
13
down vote
start a command
command1
pause the execution by pressing Ctrl+Z
find the job number of the paused command (it's usually already printed to console when to console when to command is paused) by executing
jobs
let
command1
continue in background
bg
plan execution of
command2
to await finish ofcommand1
wait -n <command1 job number> ; command2
Documentation Job Control Builtins
start a command
command1
pause the execution by pressing Ctrl+Z
find the job number of the paused command (it's usually already printed to console when to console when to command is paused) by executing
jobs
let
command1
continue in background
bg
plan execution of
command2
to await finish ofcommand1
wait -n <command1 job number> ; command2
Documentation Job Control Builtins
answered 14 hours ago
czerny
4731311
4731311
You can 'accept' your own answer (click the tick icon) into order to show other users that there is a good answer to your question.
– sudodus
14 hours ago
1
@sudodus Own answer can be accepted at earliest 2 days after the question was asked. stackoverflow.blog/2009/01/06/accept-your-own-answers
– czerny
14 hours ago
6
In simple cases I sometimes control-z then usefg ; command
. Or evenfg && command
to only run the 2nd command / pipeline if the first one was successful. (You can still usebg
while thinking / typing, if it doesn't spew text on the terminal.)
– Peter Cordes
12 hours ago
1
I'm a bit confused as to what this approach offers beyond the typicalcommand1; command2
syntax. Could you please clarify this for me?
– Haxiel
8 hours ago
1
command1 & proceed_after=$!
then when you've got the next command figured outwait -n $proceed_after; command 2
– studog
7 hours ago
|
show 1 more comment
You can 'accept' your own answer (click the tick icon) into order to show other users that there is a good answer to your question.
– sudodus
14 hours ago
1
@sudodus Own answer can be accepted at earliest 2 days after the question was asked. stackoverflow.blog/2009/01/06/accept-your-own-answers
– czerny
14 hours ago
6
In simple cases I sometimes control-z then usefg ; command
. Or evenfg && command
to only run the 2nd command / pipeline if the first one was successful. (You can still usebg
while thinking / typing, if it doesn't spew text on the terminal.)
– Peter Cordes
12 hours ago
1
I'm a bit confused as to what this approach offers beyond the typicalcommand1; command2
syntax. Could you please clarify this for me?
– Haxiel
8 hours ago
1
command1 & proceed_after=$!
then when you've got the next command figured outwait -n $proceed_after; command 2
– studog
7 hours ago
You can 'accept' your own answer (click the tick icon) into order to show other users that there is a good answer to your question.
– sudodus
14 hours ago
You can 'accept' your own answer (click the tick icon) into order to show other users that there is a good answer to your question.
– sudodus
14 hours ago
1
1
@sudodus Own answer can be accepted at earliest 2 days after the question was asked. stackoverflow.blog/2009/01/06/accept-your-own-answers
– czerny
14 hours ago
@sudodus Own answer can be accepted at earliest 2 days after the question was asked. stackoverflow.blog/2009/01/06/accept-your-own-answers
– czerny
14 hours ago
6
6
In simple cases I sometimes control-z then use
fg ; command
. Or even fg && command
to only run the 2nd command / pipeline if the first one was successful. (You can still use bg
while thinking / typing, if it doesn't spew text on the terminal.)– Peter Cordes
12 hours ago
In simple cases I sometimes control-z then use
fg ; command
. Or even fg && command
to only run the 2nd command / pipeline if the first one was successful. (You can still use bg
while thinking / typing, if it doesn't spew text on the terminal.)– Peter Cordes
12 hours ago
1
1
I'm a bit confused as to what this approach offers beyond the typical
command1; command2
syntax. Could you please clarify this for me?– Haxiel
8 hours ago
I'm a bit confused as to what this approach offers beyond the typical
command1; command2
syntax. Could you please clarify this for me?– Haxiel
8 hours ago
1
1
command1 & proceed_after=$!
then when you've got the next command figured out wait -n $proceed_after; command 2
– studog
7 hours ago
command1 & proceed_after=$!
then when you've got the next command figured out wait -n $proceed_after; command 2
– studog
7 hours ago
|
show 1 more comment
up vote
6
down vote
Generally what I do is: Ctrl+Z fg && command2
Ctrl+Z to pause it and let you type more in the shell.- Optionally
bg
, to resume command1 in the background while you type out command2.
fg && command2
to resume command1 in the foreground and queue up command2 afterwards if command1 succeeds. You can of course substitute;
or||
for the&&
if you so desire.
1
Stick abg
in there and win!
– Joshua
5 hours ago
1
@Joshua okay, what do I win now :P
– The Guy with The Hat
5 hours ago
add a comment |
up vote
6
down vote
Generally what I do is: Ctrl+Z fg && command2
Ctrl+Z to pause it and let you type more in the shell.- Optionally
bg
, to resume command1 in the background while you type out command2.
fg && command2
to resume command1 in the foreground and queue up command2 afterwards if command1 succeeds. You can of course substitute;
or||
for the&&
if you so desire.
1
Stick abg
in there and win!
– Joshua
5 hours ago
1
@Joshua okay, what do I win now :P
– The Guy with The Hat
5 hours ago
add a comment |
up vote
6
down vote
up vote
6
down vote
Generally what I do is: Ctrl+Z fg && command2
Ctrl+Z to pause it and let you type more in the shell.- Optionally
bg
, to resume command1 in the background while you type out command2.
fg && command2
to resume command1 in the foreground and queue up command2 afterwards if command1 succeeds. You can of course substitute;
or||
for the&&
if you so desire.
Generally what I do is: Ctrl+Z fg && command2
Ctrl+Z to pause it and let you type more in the shell.- Optionally
bg
, to resume command1 in the background while you type out command2.
fg && command2
to resume command1 in the foreground and queue up command2 afterwards if command1 succeeds. You can of course substitute;
or||
for the&&
if you so desire.
edited 4 hours ago
answered 10 hours ago
The Guy with The Hat
1716
1716
1
Stick abg
in there and win!
– Joshua
5 hours ago
1
@Joshua okay, what do I win now :P
– The Guy with The Hat
5 hours ago
add a comment |
1
Stick abg
in there and win!
– Joshua
5 hours ago
1
@Joshua okay, what do I win now :P
– The Guy with The Hat
5 hours ago
1
1
Stick a
bg
in there and win!– Joshua
5 hours ago
Stick a
bg
in there and win!– Joshua
5 hours ago
1
1
@Joshua okay, what do I win now :P
– The Guy with The Hat
5 hours ago
@Joshua okay, what do I win now :P
– The Guy with The Hat
5 hours ago
add a comment |
up vote
2
down vote
There are several alternatives.
With one ampersand, 'push to background', the second program starts after the first one starts, but they will probably run alongside each other.
command1 & command2
With two ampersands, 'logical and', the second program will start only if the first program finished successfully.
command1 && command2
With a semicolon separating the commands in the command line, the the second program will start, when the first program finished, even if it failed or was interrupted.
command1 ; command2
You can use
wait <PID>
to wait for the first command to finish, if it is already running from the same shell (in the same terminal window).
Otherwise if the first command is already running from another shell (in another window), you can use a small while loop using ps to check if the PID is still found by
ps
. When it is no longer found, the second command will be started.
This demo example for
bash
checks iftop
is running via the PID, and runs the command
echo "*** $USER, I am ready now ***"
if/when
top
is no longer running.
pid=$(ps -A|grep top|cut -d ' ' -f 1);
while [ "$pid" != "" ]; do ps -A|grep "$pid" > /dev/null;
if [ $? -eq 0 ]; then sleep 5;else pid=""; fi; done;
echo "*** $USER, I am ready now ***"
add a comment |
up vote
2
down vote
There are several alternatives.
With one ampersand, 'push to background', the second program starts after the first one starts, but they will probably run alongside each other.
command1 & command2
With two ampersands, 'logical and', the second program will start only if the first program finished successfully.
command1 && command2
With a semicolon separating the commands in the command line, the the second program will start, when the first program finished, even if it failed or was interrupted.
command1 ; command2
You can use
wait <PID>
to wait for the first command to finish, if it is already running from the same shell (in the same terminal window).
Otherwise if the first command is already running from another shell (in another window), you can use a small while loop using ps to check if the PID is still found by
ps
. When it is no longer found, the second command will be started.
This demo example for
bash
checks iftop
is running via the PID, and runs the command
echo "*** $USER, I am ready now ***"
if/when
top
is no longer running.
pid=$(ps -A|grep top|cut -d ' ' -f 1);
while [ "$pid" != "" ]; do ps -A|grep "$pid" > /dev/null;
if [ $? -eq 0 ]; then sleep 5;else pid=""; fi; done;
echo "*** $USER, I am ready now ***"
add a comment |
up vote
2
down vote
up vote
2
down vote
There are several alternatives.
With one ampersand, 'push to background', the second program starts after the first one starts, but they will probably run alongside each other.
command1 & command2
With two ampersands, 'logical and', the second program will start only if the first program finished successfully.
command1 && command2
With a semicolon separating the commands in the command line, the the second program will start, when the first program finished, even if it failed or was interrupted.
command1 ; command2
You can use
wait <PID>
to wait for the first command to finish, if it is already running from the same shell (in the same terminal window).
Otherwise if the first command is already running from another shell (in another window), you can use a small while loop using ps to check if the PID is still found by
ps
. When it is no longer found, the second command will be started.
This demo example for
bash
checks iftop
is running via the PID, and runs the command
echo "*** $USER, I am ready now ***"
if/when
top
is no longer running.
pid=$(ps -A|grep top|cut -d ' ' -f 1);
while [ "$pid" != "" ]; do ps -A|grep "$pid" > /dev/null;
if [ $? -eq 0 ]; then sleep 5;else pid=""; fi; done;
echo "*** $USER, I am ready now ***"
There are several alternatives.
With one ampersand, 'push to background', the second program starts after the first one starts, but they will probably run alongside each other.
command1 & command2
With two ampersands, 'logical and', the second program will start only if the first program finished successfully.
command1 && command2
With a semicolon separating the commands in the command line, the the second program will start, when the first program finished, even if it failed or was interrupted.
command1 ; command2
You can use
wait <PID>
to wait for the first command to finish, if it is already running from the same shell (in the same terminal window).
Otherwise if the first command is already running from another shell (in another window), you can use a small while loop using ps to check if the PID is still found by
ps
. When it is no longer found, the second command will be started.
This demo example for
bash
checks iftop
is running via the PID, and runs the command
echo "*** $USER, I am ready now ***"
if/when
top
is no longer running.
pid=$(ps -A|grep top|cut -d ' ' -f 1);
while [ "$pid" != "" ]; do ps -A|grep "$pid" > /dev/null;
if [ $? -eq 0 ]; then sleep 5;else pid=""; fi; done;
echo "*** $USER, I am ready now ***"
edited 2 hours ago
answered 14 hours ago
sudodus
73616
73616
add a comment |
add a comment |
up vote
1
down vote
The standard way is just to let command 1 start in the background and then have command 2 start either in the background or in the foreground. The full command is this:
command1 & command2
&
is not a connector between these two commands. It can be used with command1
alone to have it started in the background:
command1 &
And this is right syntax for starting both commands in the background:
command1 & command2 &
Thank you for your answer. I don't mind if commands are running in background or foreground. I'd like to run them serially (one starts when the previous one ends) and I'd like to plan the execution of the second one when the first one is already running.
– czerny
14 hours ago
@czerny So you want the second one to start while the first one is still running, when it's already finished, or?
– Tomasz
14 hours ago
I'd like the second command to be started when the first command finishes
– czerny
14 hours ago
1
@czerny Then just use;
. Or do you want to do something while the first one is still running?
– Tomasz
13 hours ago
add a comment |
up vote
1
down vote
The standard way is just to let command 1 start in the background and then have command 2 start either in the background or in the foreground. The full command is this:
command1 & command2
&
is not a connector between these two commands. It can be used with command1
alone to have it started in the background:
command1 &
And this is right syntax for starting both commands in the background:
command1 & command2 &
Thank you for your answer. I don't mind if commands are running in background or foreground. I'd like to run them serially (one starts when the previous one ends) and I'd like to plan the execution of the second one when the first one is already running.
– czerny
14 hours ago
@czerny So you want the second one to start while the first one is still running, when it's already finished, or?
– Tomasz
14 hours ago
I'd like the second command to be started when the first command finishes
– czerny
14 hours ago
1
@czerny Then just use;
. Or do you want to do something while the first one is still running?
– Tomasz
13 hours ago
add a comment |
up vote
1
down vote
up vote
1
down vote
The standard way is just to let command 1 start in the background and then have command 2 start either in the background or in the foreground. The full command is this:
command1 & command2
&
is not a connector between these two commands. It can be used with command1
alone to have it started in the background:
command1 &
And this is right syntax for starting both commands in the background:
command1 & command2 &
The standard way is just to let command 1 start in the background and then have command 2 start either in the background or in the foreground. The full command is this:
command1 & command2
&
is not a connector between these two commands. It can be used with command1
alone to have it started in the background:
command1 &
And this is right syntax for starting both commands in the background:
command1 & command2 &
answered 15 hours ago
Tomasz
9,17052964
9,17052964
Thank you for your answer. I don't mind if commands are running in background or foreground. I'd like to run them serially (one starts when the previous one ends) and I'd like to plan the execution of the second one when the first one is already running.
– czerny
14 hours ago
@czerny So you want the second one to start while the first one is still running, when it's already finished, or?
– Tomasz
14 hours ago
I'd like the second command to be started when the first command finishes
– czerny
14 hours ago
1
@czerny Then just use;
. Or do you want to do something while the first one is still running?
– Tomasz
13 hours ago
add a comment |
Thank you for your answer. I don't mind if commands are running in background or foreground. I'd like to run them serially (one starts when the previous one ends) and I'd like to plan the execution of the second one when the first one is already running.
– czerny
14 hours ago
@czerny So you want the second one to start while the first one is still running, when it's already finished, or?
– Tomasz
14 hours ago
I'd like the second command to be started when the first command finishes
– czerny
14 hours ago
1
@czerny Then just use;
. Or do you want to do something while the first one is still running?
– Tomasz
13 hours ago
Thank you for your answer. I don't mind if commands are running in background or foreground. I'd like to run them serially (one starts when the previous one ends) and I'd like to plan the execution of the second one when the first one is already running.
– czerny
14 hours ago
Thank you for your answer. I don't mind if commands are running in background or foreground. I'd like to run them serially (one starts when the previous one ends) and I'd like to plan the execution of the second one when the first one is already running.
– czerny
14 hours ago
@czerny So you want the second one to start while the first one is still running, when it's already finished, or?
– Tomasz
14 hours ago
@czerny So you want the second one to start while the first one is still running, when it's already finished, or?
– Tomasz
14 hours ago
I'd like the second command to be started when the first command finishes
– czerny
14 hours ago
I'd like the second command to be started when the first command finishes
– czerny
14 hours ago
1
1
@czerny Then just use
;
. Or do you want to do something while the first one is still running?– Tomasz
13 hours ago
@czerny Then just use
;
. Or do you want to do something while the first one is still running?– Tomasz
13 hours ago
add a comment |
up vote
-2
down vote
If you have a choice to use two terminals, here's what you can do:
- Assuming you are already running command1 in a terminal (let's call it terminal1)
- Start a new terminal (let's call it terminal2)
- Find process ID of command1 (let's call it command1_pid). In terminal2, run command
ps -ef | grep <command1>
(you might want to properly form the filter string in grep command so that you do not accidently find another process having as a substring). - Run command
wait <command1_pid> ; command2
Basically, wait
is a bash built-in command that waits until the provided PIDs exit and then return. You are simply delegating your waiting for command1 to finish, on to bash's wait
command.
New contributor
2
It doesn't seem to work for me. Runningwait
in different terminal outputs following error message:bash: wait: pid 19531 is not a child of this shell
.
– czerny
14 hours ago
@czerny That's normal, I don't think this answer was tested. POSIXwait
system calls only work on child processes, so one bash process can't wait for the children of a different instance of bash. man7.org/linux/man-pages/man2/wait.2.html#ERRORS sayswait
andwaitpid
return withECHILD
error status in this case.
– Peter Cordes
12 hours ago
add a comment |
up vote
-2
down vote
If you have a choice to use two terminals, here's what you can do:
- Assuming you are already running command1 in a terminal (let's call it terminal1)
- Start a new terminal (let's call it terminal2)
- Find process ID of command1 (let's call it command1_pid). In terminal2, run command
ps -ef | grep <command1>
(you might want to properly form the filter string in grep command so that you do not accidently find another process having as a substring). - Run command
wait <command1_pid> ; command2
Basically, wait
is a bash built-in command that waits until the provided PIDs exit and then return. You are simply delegating your waiting for command1 to finish, on to bash's wait
command.
New contributor
2
It doesn't seem to work for me. Runningwait
in different terminal outputs following error message:bash: wait: pid 19531 is not a child of this shell
.
– czerny
14 hours ago
@czerny That's normal, I don't think this answer was tested. POSIXwait
system calls only work on child processes, so one bash process can't wait for the children of a different instance of bash. man7.org/linux/man-pages/man2/wait.2.html#ERRORS sayswait
andwaitpid
return withECHILD
error status in this case.
– Peter Cordes
12 hours ago
add a comment |
up vote
-2
down vote
up vote
-2
down vote
If you have a choice to use two terminals, here's what you can do:
- Assuming you are already running command1 in a terminal (let's call it terminal1)
- Start a new terminal (let's call it terminal2)
- Find process ID of command1 (let's call it command1_pid). In terminal2, run command
ps -ef | grep <command1>
(you might want to properly form the filter string in grep command so that you do not accidently find another process having as a substring). - Run command
wait <command1_pid> ; command2
Basically, wait
is a bash built-in command that waits until the provided PIDs exit and then return. You are simply delegating your waiting for command1 to finish, on to bash's wait
command.
New contributor
If you have a choice to use two terminals, here's what you can do:
- Assuming you are already running command1 in a terminal (let's call it terminal1)
- Start a new terminal (let's call it terminal2)
- Find process ID of command1 (let's call it command1_pid). In terminal2, run command
ps -ef | grep <command1>
(you might want to properly form the filter string in grep command so that you do not accidently find another process having as a substring). - Run command
wait <command1_pid> ; command2
Basically, wait
is a bash built-in command that waits until the provided PIDs exit and then return. You are simply delegating your waiting for command1 to finish, on to bash's wait
command.
New contributor
New contributor
answered 14 hours ago
Prasanna
1012
1012
New contributor
New contributor
2
It doesn't seem to work for me. Runningwait
in different terminal outputs following error message:bash: wait: pid 19531 is not a child of this shell
.
– czerny
14 hours ago
@czerny That's normal, I don't think this answer was tested. POSIXwait
system calls only work on child processes, so one bash process can't wait for the children of a different instance of bash. man7.org/linux/man-pages/man2/wait.2.html#ERRORS sayswait
andwaitpid
return withECHILD
error status in this case.
– Peter Cordes
12 hours ago
add a comment |
2
It doesn't seem to work for me. Runningwait
in different terminal outputs following error message:bash: wait: pid 19531 is not a child of this shell
.
– czerny
14 hours ago
@czerny That's normal, I don't think this answer was tested. POSIXwait
system calls only work on child processes, so one bash process can't wait for the children of a different instance of bash. man7.org/linux/man-pages/man2/wait.2.html#ERRORS sayswait
andwaitpid
return withECHILD
error status in this case.
– Peter Cordes
12 hours ago
2
2
It doesn't seem to work for me. Running
wait
in different terminal outputs following error message: bash: wait: pid 19531 is not a child of this shell
.– czerny
14 hours ago
It doesn't seem to work for me. Running
wait
in different terminal outputs following error message: bash: wait: pid 19531 is not a child of this shell
.– czerny
14 hours ago
@czerny That's normal, I don't think this answer was tested. POSIX
wait
system calls only work on child processes, so one bash process can't wait for the children of a different instance of bash. man7.org/linux/man-pages/man2/wait.2.html#ERRORS says wait
and waitpid
return with ECHILD
error status in this case.– Peter Cordes
12 hours ago
@czerny That's normal, I don't think this answer was tested. POSIX
wait
system calls only work on child processes, so one bash process can't wait for the children of a different instance of bash. man7.org/linux/man-pages/man2/wait.2.html#ERRORS says wait
and waitpid
return with ECHILD
error status in this case.– Peter Cordes
12 hours ago
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2funix.stackexchange.com%2fquestions%2f487955%2fhow-to-plan-a-task-to-run-after-another-already-running-task-in-bash%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