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