简体   繁体   中英

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"

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.

echo "output 1:" >> my_log_file.log    
time command1... etc etc
echo "------------------------" >> my_log_file.log

This will help as it will allow you to write to your log file and add separators between each 'time' output

 $(out1="output 1:"; var1=$((time my_command ) 2>&1>/dev/null ); echo -e " $out1 $var1" >>my_log_file.log)  &
 $(out2="output 2:"; var2=$((time my_command ) 2>&1>/dev/null ); echo -e " $out2 $var2" >>my_log_file.log)  &
 $(out3="output 3:"; var3=$((time my_command ) 2>&1>/dev/null ); echo -e " $out3 $var3" >>my_log_file.log)  &

...

Otput
 output 1:
real    0m0.002s
user    0m0.000s
sys     0m0.002s
 output 2:
real    0m0.002s
user    0m0.000s
sys     0m0.002s
....

Use in thread variable with proces and proces name , next I pipeline stdr by echo to file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM