pyt*_*hon 3 parallel-processing bash shell
我有一个 bash 脚本,它在后台运行 4 个不同的进程,你可以看到下面的代码:
declare -a arr=("seed_automation_data_1" "seed_automation_data_2" "seed_automation_data_3" "seed_automation_data_4")
command="bundle exec rake db:seed:"
for i in "${arr[@]}"
do
$command$i &
done
Run Code Online (Sandbox Code Playgroud)
这个 bash 脚本实际上正在运行一个rake taskin rails framework.
$command$i &
这个特定的行在后台启动了四个不同的进程:-
bundle exec rake db:seed:seed_automation_data_1
bundle exec rake db:seed:seed_automation_data_2
bundle exec rake db:seed:seed_automation_data_3
bundle exec rake db:seed:seed_automation_data_4
Run Code Online (Sandbox Code Playgroud)
Since there are four different process running in the background I am not able to know when the bash script is FINISHED or calculate the execution-time of it.
Is there a way I can print some statements which will show that the script is finished running?
Have a look at the wait function in bash. It simply waits for all child processes to finish. Then you can easily calculate the time elapsed e.g. using the SECONDS internal variable (explained here):
SECONDS=0
declare -a arr=("seed_automation_data_1" "seed_automation_data_2" "seed_automation_data_3" "seed_automation_data_4")
command="bundle exec rake db:seed:"
for i in "${arr[@]}"
do
$command$i &
done
wait
echo $SECONDS
Run Code Online (Sandbox Code Playgroud)