How to execute unix command after few background jobs are completed?What does the number in parentheses shown after Unix command names in manpages mean?How do I list all cron jobs for all users?How can I convert a Unix timestamp to DateTime and vice versa?Unix: How to check permissions of a specific directory?How to run a shell script on a Unix console or Mac terminal?How to colorize diff on the command line?How to permanently set $PATH on Linux/Unix?UNIX shell script do loop execute commandsHave python script run in background of unixExecute SSH Script via Command Line and then Close Shell
Ability To Change Root User Password (Vulnerability?)
Difference between prepositions in "...killed during/in the war"
Was Self-modifying-code possible just using BASIC?
How can powerful telekinesis avoid violating Newton's 3rd Law?
Write a function that checks if a string starts with or contains something
If there's something that implicates the president why is there then a national security issue? (John Dowd)
Proving that a Russian cryptographic standard is too structured
Do you need to let the DM know when you are multiclassing?
Was planting UN flag on Moon ever discussed?
Analogy between an unknown in an argument, and a contradiction in the principle of explosion
Printing Pascal’s triangle for n number of rows in Python
Why does a file move/copy function only move one file at a time when using the “*” wildcard?
What STL algorithm can determine if exactly one item in a container satisfies a predicate?
Why the output signal of my amplifier is heavily distorted
What aircraft was used as Air Force One for the flight between Southampton and Shannon?
Live action TV show where High school Kids go into the virtual world and have to clear levels
How can I make 12 tone and atonal melodies sound interesting?
Possible runaway argument using circuitikz
Why Does Mama Coco Look Old After Going to the Other World?
Is it safe to change the harddrive power feature so that it never turns off?
Is it possible to fly backward if you have really strong headwind?
How do i export activities related to an account with a specific recordtype?
Why does this query, missing a FROM clause, not error out?
Reference to understand the notation of orbital charts
How to execute unix command after few background jobs are completed?
What does the number in parentheses shown after Unix command names in manpages mean?How do I list all cron jobs for all users?How can I convert a Unix timestamp to DateTime and vice versa?Unix: How to check permissions of a specific directory?How to run a shell script on a Unix console or Mac terminal?How to colorize diff on the command line?How to permanently set $PATH on Linux/Unix?UNIX shell script do loop execute commandsHave python script run in background of unixExecute SSH Script via Command Line and then Close Shell
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
My c-shell code has to complete few concurrent background jobs before execution of some unix commands. However, somehow in code my background jobs never returns handle to unix commands.
i.e.
$cmd1 | tee $XD/m1.log&
$cmd2 | tee $XD/m2.log&
$cmd3 | tee $XD/m3.log&
$cmd4 | tee $XD/m4.log&
All 4 commands mentioned above generate 4 different files named $L1
,$L2
,$L3
and $L4
(example: setenv L1 $XD/div.txt
etc.). I need to merge all of these files after removing the first line from them so have used following logic. But seems like sed command never gets executed
wait $!
echo "Job completed"
sed -i '1d' $L2
sed -i '1d' $L3
sed -i '1d' $L4
cat $L1 $L2 $L3 $L4 >> $L
Could you please help me to make then executes post background jobs?
tried
if (! -e $L4 ) then
if ( -f $L4 ) then
wait $!
echo "Job completed"
sed -i '1d' $L2
sed -i '1d' $L3
sed -i '1d' $L4
cat $L1 $L2 $L3 $L4 >> $L
endif
endif
but didn't help either.
$cmd1 | tee $XD/m1.log&
$cmd2 | tee $XD/m2.log&
$cmd3 | tee $XD/m3.log&
$cmd4 | tee $XD/m4.log&
if (! -e $L4 ) then
if ( -f $L4 ) then
wait $!
echo "Job completed"
sed -i '1d' $L2
sed -i '1d' $L3
sed -i '1d' $L4
cat $L1 $L2 $L3 $L4 >> $L
endif
endif
Expected results would be to generate $L
file ($XD/final.txt
), which would happen only when script will be back to 2nd half section containing sed command.
unix tcsh
add a comment |
My c-shell code has to complete few concurrent background jobs before execution of some unix commands. However, somehow in code my background jobs never returns handle to unix commands.
i.e.
$cmd1 | tee $XD/m1.log&
$cmd2 | tee $XD/m2.log&
$cmd3 | tee $XD/m3.log&
$cmd4 | tee $XD/m4.log&
All 4 commands mentioned above generate 4 different files named $L1
,$L2
,$L3
and $L4
(example: setenv L1 $XD/div.txt
etc.). I need to merge all of these files after removing the first line from them so have used following logic. But seems like sed command never gets executed
wait $!
echo "Job completed"
sed -i '1d' $L2
sed -i '1d' $L3
sed -i '1d' $L4
cat $L1 $L2 $L3 $L4 >> $L
Could you please help me to make then executes post background jobs?
tried
if (! -e $L4 ) then
if ( -f $L4 ) then
wait $!
echo "Job completed"
sed -i '1d' $L2
sed -i '1d' $L3
sed -i '1d' $L4
cat $L1 $L2 $L3 $L4 >> $L
endif
endif
but didn't help either.
$cmd1 | tee $XD/m1.log&
$cmd2 | tee $XD/m2.log&
$cmd3 | tee $XD/m3.log&
$cmd4 | tee $XD/m4.log&
if (! -e $L4 ) then
if ( -f $L4 ) then
wait $!
echo "Job completed"
sed -i '1d' $L2
sed -i '1d' $L3
sed -i '1d' $L4
cat $L1 $L2 $L3 $L4 >> $L
endif
endif
Expected results would be to generate $L
file ($XD/final.txt
), which would happen only when script will be back to 2nd half section containing sed command.
unix tcsh
add a comment |
My c-shell code has to complete few concurrent background jobs before execution of some unix commands. However, somehow in code my background jobs never returns handle to unix commands.
i.e.
$cmd1 | tee $XD/m1.log&
$cmd2 | tee $XD/m2.log&
$cmd3 | tee $XD/m3.log&
$cmd4 | tee $XD/m4.log&
All 4 commands mentioned above generate 4 different files named $L1
,$L2
,$L3
and $L4
(example: setenv L1 $XD/div.txt
etc.). I need to merge all of these files after removing the first line from them so have used following logic. But seems like sed command never gets executed
wait $!
echo "Job completed"
sed -i '1d' $L2
sed -i '1d' $L3
sed -i '1d' $L4
cat $L1 $L2 $L3 $L4 >> $L
Could you please help me to make then executes post background jobs?
tried
if (! -e $L4 ) then
if ( -f $L4 ) then
wait $!
echo "Job completed"
sed -i '1d' $L2
sed -i '1d' $L3
sed -i '1d' $L4
cat $L1 $L2 $L3 $L4 >> $L
endif
endif
but didn't help either.
$cmd1 | tee $XD/m1.log&
$cmd2 | tee $XD/m2.log&
$cmd3 | tee $XD/m3.log&
$cmd4 | tee $XD/m4.log&
if (! -e $L4 ) then
if ( -f $L4 ) then
wait $!
echo "Job completed"
sed -i '1d' $L2
sed -i '1d' $L3
sed -i '1d' $L4
cat $L1 $L2 $L3 $L4 >> $L
endif
endif
Expected results would be to generate $L
file ($XD/final.txt
), which would happen only when script will be back to 2nd half section containing sed command.
unix tcsh
My c-shell code has to complete few concurrent background jobs before execution of some unix commands. However, somehow in code my background jobs never returns handle to unix commands.
i.e.
$cmd1 | tee $XD/m1.log&
$cmd2 | tee $XD/m2.log&
$cmd3 | tee $XD/m3.log&
$cmd4 | tee $XD/m4.log&
All 4 commands mentioned above generate 4 different files named $L1
,$L2
,$L3
and $L4
(example: setenv L1 $XD/div.txt
etc.). I need to merge all of these files after removing the first line from them so have used following logic. But seems like sed command never gets executed
wait $!
echo "Job completed"
sed -i '1d' $L2
sed -i '1d' $L3
sed -i '1d' $L4
cat $L1 $L2 $L3 $L4 >> $L
Could you please help me to make then executes post background jobs?
tried
if (! -e $L4 ) then
if ( -f $L4 ) then
wait $!
echo "Job completed"
sed -i '1d' $L2
sed -i '1d' $L3
sed -i '1d' $L4
cat $L1 $L2 $L3 $L4 >> $L
endif
endif
but didn't help either.
$cmd1 | tee $XD/m1.log&
$cmd2 | tee $XD/m2.log&
$cmd3 | tee $XD/m3.log&
$cmd4 | tee $XD/m4.log&
if (! -e $L4 ) then
if ( -f $L4 ) then
wait $!
echo "Job completed"
sed -i '1d' $L2
sed -i '1d' $L3
sed -i '1d' $L4
cat $L1 $L2 $L3 $L4 >> $L
endif
endif
Expected results would be to generate $L
file ($XD/final.txt
), which would happen only when script will be back to 2nd half section containing sed command.
unix tcsh
unix tcsh
edited Apr 13 at 16:14
Kubator
1,13613
1,13613
asked Mar 24 at 20:50
user3093942user3093942
134
134
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Short answer, just use:
wait
wait
without any parameter wait will wait for all children processes. $!
is pid of the last background process started so wait $!
will wait only for process last child process.
Long answer, full script edited:
#!/bin/env tcsh
set cmd1 = 'printf headernline11nline12n'
set cmd2 = 'printf headernline21nline22n'
set cmd3 = 'printf headernline31nline32n'
set cmd4 = 'printf headernline41nline42n'
set XD = '.'
set L = "$XD/m.log"
set L1 = "$XD/m1.log"
set L2 = "$XD/m2.log"
set L3 = "$XD/m3.log"
set L4 = "$XD/m4.log"
touch "$L" "$L1" "$L2" "$L3" "$L4" || exit (1)
( $cmd1; sleep 5 ) | tee "$L1" &
( $cmd2; sleep 2 ) | tee "$L2" &
( $cmd3; sleep 4 ) | tee "$L3" &
( $cmd4; sleep 3 ) | tee "$L4" &
wait
echo "Job completed"
cat "$L1" > "$L"
sed '1d' "$L2" >> "$L"
sed '1d' "$L3" >> "$L"
sed '1d' "$L4" >> "$L"
exit (0)
Test:
% ./user3093942.tcsh
[1] 1542 1543
header
line11
line12
[2] 1545 1546
header
line21
line22
[3] 1548 1549
header
line31
line32
[4] 1551 1552
header
line41
line42
[4] + Done ( $cmd4; sleep 3 ) | tee ./m4.log
[3] + Done ( $cmd3; sleep 4 ) | tee ./m3.log
[2] + Done ( $cmd2; sleep 2 ) | tee ./m2.log
[1] + Done ( $cmd1; sleep 5 ) | tee ./m1.log
Job completed
% cat ./m.log
header
line11
line12
line21
line22
line31
line32
line41
line42
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%2f55328444%2fhow-to-execute-unix-command-after-few-background-jobs-are-completed%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Short answer, just use:
wait
wait
without any parameter wait will wait for all children processes. $!
is pid of the last background process started so wait $!
will wait only for process last child process.
Long answer, full script edited:
#!/bin/env tcsh
set cmd1 = 'printf headernline11nline12n'
set cmd2 = 'printf headernline21nline22n'
set cmd3 = 'printf headernline31nline32n'
set cmd4 = 'printf headernline41nline42n'
set XD = '.'
set L = "$XD/m.log"
set L1 = "$XD/m1.log"
set L2 = "$XD/m2.log"
set L3 = "$XD/m3.log"
set L4 = "$XD/m4.log"
touch "$L" "$L1" "$L2" "$L3" "$L4" || exit (1)
( $cmd1; sleep 5 ) | tee "$L1" &
( $cmd2; sleep 2 ) | tee "$L2" &
( $cmd3; sleep 4 ) | tee "$L3" &
( $cmd4; sleep 3 ) | tee "$L4" &
wait
echo "Job completed"
cat "$L1" > "$L"
sed '1d' "$L2" >> "$L"
sed '1d' "$L3" >> "$L"
sed '1d' "$L4" >> "$L"
exit (0)
Test:
% ./user3093942.tcsh
[1] 1542 1543
header
line11
line12
[2] 1545 1546
header
line21
line22
[3] 1548 1549
header
line31
line32
[4] 1551 1552
header
line41
line42
[4] + Done ( $cmd4; sleep 3 ) | tee ./m4.log
[3] + Done ( $cmd3; sleep 4 ) | tee ./m3.log
[2] + Done ( $cmd2; sleep 2 ) | tee ./m2.log
[1] + Done ( $cmd1; sleep 5 ) | tee ./m1.log
Job completed
% cat ./m.log
header
line11
line12
line21
line22
line31
line32
line41
line42
add a comment |
Short answer, just use:
wait
wait
without any parameter wait will wait for all children processes. $!
is pid of the last background process started so wait $!
will wait only for process last child process.
Long answer, full script edited:
#!/bin/env tcsh
set cmd1 = 'printf headernline11nline12n'
set cmd2 = 'printf headernline21nline22n'
set cmd3 = 'printf headernline31nline32n'
set cmd4 = 'printf headernline41nline42n'
set XD = '.'
set L = "$XD/m.log"
set L1 = "$XD/m1.log"
set L2 = "$XD/m2.log"
set L3 = "$XD/m3.log"
set L4 = "$XD/m4.log"
touch "$L" "$L1" "$L2" "$L3" "$L4" || exit (1)
( $cmd1; sleep 5 ) | tee "$L1" &
( $cmd2; sleep 2 ) | tee "$L2" &
( $cmd3; sleep 4 ) | tee "$L3" &
( $cmd4; sleep 3 ) | tee "$L4" &
wait
echo "Job completed"
cat "$L1" > "$L"
sed '1d' "$L2" >> "$L"
sed '1d' "$L3" >> "$L"
sed '1d' "$L4" >> "$L"
exit (0)
Test:
% ./user3093942.tcsh
[1] 1542 1543
header
line11
line12
[2] 1545 1546
header
line21
line22
[3] 1548 1549
header
line31
line32
[4] 1551 1552
header
line41
line42
[4] + Done ( $cmd4; sleep 3 ) | tee ./m4.log
[3] + Done ( $cmd3; sleep 4 ) | tee ./m3.log
[2] + Done ( $cmd2; sleep 2 ) | tee ./m2.log
[1] + Done ( $cmd1; sleep 5 ) | tee ./m1.log
Job completed
% cat ./m.log
header
line11
line12
line21
line22
line31
line32
line41
line42
add a comment |
Short answer, just use:
wait
wait
without any parameter wait will wait for all children processes. $!
is pid of the last background process started so wait $!
will wait only for process last child process.
Long answer, full script edited:
#!/bin/env tcsh
set cmd1 = 'printf headernline11nline12n'
set cmd2 = 'printf headernline21nline22n'
set cmd3 = 'printf headernline31nline32n'
set cmd4 = 'printf headernline41nline42n'
set XD = '.'
set L = "$XD/m.log"
set L1 = "$XD/m1.log"
set L2 = "$XD/m2.log"
set L3 = "$XD/m3.log"
set L4 = "$XD/m4.log"
touch "$L" "$L1" "$L2" "$L3" "$L4" || exit (1)
( $cmd1; sleep 5 ) | tee "$L1" &
( $cmd2; sleep 2 ) | tee "$L2" &
( $cmd3; sleep 4 ) | tee "$L3" &
( $cmd4; sleep 3 ) | tee "$L4" &
wait
echo "Job completed"
cat "$L1" > "$L"
sed '1d' "$L2" >> "$L"
sed '1d' "$L3" >> "$L"
sed '1d' "$L4" >> "$L"
exit (0)
Test:
% ./user3093942.tcsh
[1] 1542 1543
header
line11
line12
[2] 1545 1546
header
line21
line22
[3] 1548 1549
header
line31
line32
[4] 1551 1552
header
line41
line42
[4] + Done ( $cmd4; sleep 3 ) | tee ./m4.log
[3] + Done ( $cmd3; sleep 4 ) | tee ./m3.log
[2] + Done ( $cmd2; sleep 2 ) | tee ./m2.log
[1] + Done ( $cmd1; sleep 5 ) | tee ./m1.log
Job completed
% cat ./m.log
header
line11
line12
line21
line22
line31
line32
line41
line42
Short answer, just use:
wait
wait
without any parameter wait will wait for all children processes. $!
is pid of the last background process started so wait $!
will wait only for process last child process.
Long answer, full script edited:
#!/bin/env tcsh
set cmd1 = 'printf headernline11nline12n'
set cmd2 = 'printf headernline21nline22n'
set cmd3 = 'printf headernline31nline32n'
set cmd4 = 'printf headernline41nline42n'
set XD = '.'
set L = "$XD/m.log"
set L1 = "$XD/m1.log"
set L2 = "$XD/m2.log"
set L3 = "$XD/m3.log"
set L4 = "$XD/m4.log"
touch "$L" "$L1" "$L2" "$L3" "$L4" || exit (1)
( $cmd1; sleep 5 ) | tee "$L1" &
( $cmd2; sleep 2 ) | tee "$L2" &
( $cmd3; sleep 4 ) | tee "$L3" &
( $cmd4; sleep 3 ) | tee "$L4" &
wait
echo "Job completed"
cat "$L1" > "$L"
sed '1d' "$L2" >> "$L"
sed '1d' "$L3" >> "$L"
sed '1d' "$L4" >> "$L"
exit (0)
Test:
% ./user3093942.tcsh
[1] 1542 1543
header
line11
line12
[2] 1545 1546
header
line21
line22
[3] 1548 1549
header
line31
line32
[4] 1551 1552
header
line41
line42
[4] + Done ( $cmd4; sleep 3 ) | tee ./m4.log
[3] + Done ( $cmd3; sleep 4 ) | tee ./m3.log
[2] + Done ( $cmd2; sleep 2 ) | tee ./m2.log
[1] + Done ( $cmd1; sleep 5 ) | tee ./m1.log
Job completed
% cat ./m.log
header
line11
line12
line21
line22
line31
line32
line41
line42
answered Apr 13 at 13:48
KubatorKubator
1,13613
1,13613
add a comment |
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%2f55328444%2fhow-to-execute-unix-command-after-few-background-jobs-are-completed%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