0

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(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

How 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 work 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.

In nutshell, I m looking for a way to make sure all four cmd* jobs are completed before moving to sed command in my script. Since the input of sed command, depends on output file generated with cmd* commands so they have to go in sequential order. Looking for a way but couldn't figure out anything yet.

  • It seems like the sed never executes? Do the 4 commands finish? – Jeff Schaller Mar 25 at 10:04
  • fwiw, wait in csh does not take any arguments and will wait for all background jobs. Also the ! -e and -f file operators are contradictory (a file that doesn't exist cannot be a regular file). Please post a reproducible example. – mosvy Mar 25 at 10:47
  • sleep 3600 & wait $! will error out with wait: Too many arguments in both csh and tcsh. So you should get rid of that $! before anything else. – mosvy Mar 25 at 11:01
  • @mosvy, thanks. Sleep would be a trouble given the size of run varies. – user3093942 Mar 25 at 11:58
  • Thanks @JeffSchaller. Yes, it never reaches to sed execution part. Wish to cluster all cmd command under some logic, which apparently would make sure the completion of them before getting to sed commands. Wondering if anyone can help me with that. – user3093942 Mar 25 at 12:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.