0

I'm trying to do some tasks in linux (benchmark test on mysql which will take upto 1 or 2 hour) using shell script and in the meantime itself, i need to run another script in parallel which would take output of the task in every 10 or 20 mins and write the output to a file. Now, once the task is finished, I would like to stop the second script and get the output. But its not happening. Here is what I did so far:

taskscript - Where I'm performing an 1 or 2 hr task

#!/bin/sh
#Here I'm doing some task which takes upto 1 hr
#For simulating test
sleep 10
echo "Task Finished" > "task.txt";

infintequery - Script which will take the output of task in parallel

#!/bin/sh

while true; 
do
sleep 10;
#Here I'm querying task result and store it to a an output file with timestamp
#For simulating test, just writing time only
echo $(date +"%m/%d %T") >> "infinite.txt";
done

mainscript - The main script which runs both the scripts in parallel

#!/bin/sh

sh ./taskscript 1>/dev/null 2>&1 &
pid1=$!

sh ./infintequery 1>/dev/null 2>&1 &
pid2=$!

wait $pid1
ret1=$?
wait $pid2
ret2=$?

if [ $ret1 -eq 0 ]
then 
   echo "Completed succesfully"
   kill $pid2
else
    echo "Failed to complete script"
fi

After I executing ./mainscript, following was the output

  • task.txt was generated after 10 seconds

  • infinite.txt was appended with time every 10 seconds.

  • The script never get finished and infinite.txt gets updated every 10 seconds.

I want the mainscript to stop execution after taskscript was finished, but the mainscript was also not finishing and even after I give Ctrl-C, the infinite.txt is still getting updated.

So guys, please help me to solve this issue. What am I doing wrong here? I'm not that experianced in shell scripting, these codes are all taken from some posts in SO itself.

Thanks In Advance

2 Answers 2

1

This piece of code here:

wait $pid1
ret1=$?
wait $pid2

will ensure it waits for both processes to finish and hence mainscript will never get to the kill statement.

That's because infintequery (I assume that was meant to be infinite but I'll spell it the same way you have) will never exit and mainscript will be forever waiting at the wait $pid2 line.

If you just want to wait for taskscript to finish then kill off infintequery, use something like:

#!/bin/sh

sh ./taskscript 1>/dev/null 2>&1 &
pid1=$!

sh ./infintequery 1>/dev/null 2>&1 &
pid2=$!

wait $pid1
ret1=$?

kill $pid2
if [ $ret1 -eq 0 ]
then 
   echo "Completed successfully"
else
    echo "Failed to complete script"
fi

That will simply wait for taskscript to finish and then kill off infintequery regardless of whether taskscript was successful or not.

Of course, that's assuming taskscript has to be running before infintequery starts (the likely case since the latter will probably end up using some information from the former). If it doesn't have to be running first, you can simplify by not running it in the background:

#!/bin/sh

sh ./infintequery 1>/dev/null 2>&1 &
pid2=$!

sh ./taskscript 1>/dev/null 2>&1
ret1=$?

kill $pid2
if [ $ret1 -eq 0 ]
then 
   echo "Completed successfully"
else
    echo "Failed to complete script"
fi
Sign up to request clarification or add additional context in comments.

3 Comments

So could u pls advise me how to correct this. I'm not familiar with these stuff.
Thanks..that was perfect..I understood wait for pid2 cause my script to hang. But in ur last edited portion, there is no wait and so I cant use that form to get it work.It get finished immediately after executing.
@HRM, in the last code segment, there is no wait $pid1 because it's not actually running in the background (the & has been removed). The main script will start it in the foreground meaning it will wait automatically for it to finish.
0

Another soln without changing your main script and if not comfortable with wait and pids. Add this line in loop in infintequery file.

if [ `grep "Task Finished" input | wc -l` -eq 1 ]; then exit; fi

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.