Skip to content
Advertisement

Time multiple commands running in parallel in shell and save it in file

I have a bash script for which i want to know the time it took to run for individual commands and save them in a log file. Also I need some of these commands to run as a different process. The problem I am facing is I am not able to tell which output belongs to which command for example my script has:

echo "output 1:"
(time my_commnad1) 2>> my_log_file.log &

echo "output 2:"
(time my_commnad2) 2>> my_log_file.log &


echo "output 3:"
(time my_commnad3) 2>> my_log_file.log &

echo "output 4:"
(time my_commnad4) 2>> my_log_file.log &
wait

desired output(note here I can see which output belongs to which command ):

output 1:
real    2m39.892s
user    8m37.383s
sys 0m3.338s

output 2:
real    2m39.892s
user    8m37.383s
sys 0m3.338s

output 3:
real    2m39.892s
user    8m37.383s
sys 0m3.338s

output 4:
real    2m39.892s
user    8m37.383s
sys 0m3.338s

but i get something like:

output1:
output2
output3:
output4:

(not sure if they are in order cant tell which one is which), Here I am not sure which output belongs to which command as they complete independently

real    2m39.892s
user    8m37.383s
sys 0m3.338s

real    2m39.892s
user    8m37.383s
sys 0m3.338s

real    2m39.892s
user    8m37.383s
sys 0m3.338s

real    2m39.892s
user    8m37.383s
sys 0m3.338s

I tried many things like coupling the commands and then timing , creating a function for two commands and then time them and many other things but every time something caused an issue or didn’t give me the desired results. I am guessing this should be an easy task for someone who has some experience in bash scripting.Thanks for your time

Update: where my_command is something like:

ffmpeg -i $INPUT_VIDEO_FILE -profile:v baseline -level 4.0 -vf "scale=-2:360,subtitles='dynamic_subtitle.ass':force_style='FontName=Aaargh/Aaargh.ttf,PrimaryColour=&H664c4c4c"  -start_number 0 -hls_time 10 -hls_list_size 0 -f hls s3_temp/$PROJECT_ID/videos/$VIDEO_ID/$VIDEO_ID"_360_.m3u8"

Advertisement

Answer

With some help I was able to find a way around this issue what worked for me is this

(time my_commnad1 && echo "output 1" >> my_log_file.log) 2>> my_log_file.log &

(time my_commnad2 && echo "output 2" >> my_log_file.log) 2>> my_log_file.log &

(time my_commnad3 && echo "output 3" >> my_log_file.log) 2>> my_log_file.log &

(time my_commnad4 && echo "output 4" >> my_log_file.log) 2>> my_log_file.log &
wait

the output i got was like this were the time is above the tag mentioned:

real    2m39.892s
user    8m37.383s
sys 0m3.338s
output 1:


real    2m39.892s
user    8m37.383s
sys 0m3.338s
output 2:


real    2m39.892s
user    8m37.383s
sys 0m3.338s
output 3:


real    2m39.892s
user    8m37.383s
sys 0m3.338s
output 4:

Moreover I couldn’t test Jacek Zaleski’s answer properly though as it gave me some errors with syntax when I tried it in my case.Thanks anyway to everyone for the time and effort. Hope this helps someone.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement