用于计算并行作业完成时间的shell脚本

时间:2016-05-24 10:54:32

标签: shell

我有一个程序,其中两个进程并行运行如下。

#!/bin/bash
nohup /bin/bash -s <<< "hive -f ./create.hql >&/dev/null" &
pid_c=$!
nohup /bin/bash -s <<< "hive -f ./load.hql >&/dev/null" &
pid_l=$!
wait $pid_c
ex_pid_c=$?
##time1=$(ps -p "$pid_c" -o etime=) not working
if [ $ex_pid_c -eq 0 ]; then
echo "Job completed."
else
echo "Failed"
fi
wait $pid_l
ex_pid_l=$?
##time2=$(ps -p "$pid_l" -o etime=) not working
if [ $ex_pid_l -eq 0 ]; then
echo "Job completed."
else
echo "Failed"
fi

现在我想计算每个工作的完成时间。我该怎么办?

2 个答案:

答案 0 :(得分:0)

您可以使用time命令知道每个命令花了多长时间(结果转到stderr):

#!/bin/bash
nohup /bin/bash -s <<< "time sleep 10 >&/dev/null" &
pid_c=$!
nohup /bin/bash -s <<< "time sleep 1 >&/dev/null" &
pid_l=$!
wait $pid_c
ex_pid_c=$?
##time1=$(ps -p "$pid_c" -o etime=) not working
if [ $ex_pid_c -eq 0 ]; then
  echo "Job completed."
else
  echo "Failed"
fi
wait $pid_l
ex_pid_l=$?
##time2=$(ps -p "$pid_l" -o etime=) not working
if [ $ex_pid_l -eq 0 ]; then
  echo "Job completed."
else
  echo "Failed"
fi

然后nohup.out应该包含如下的执行时间:

jbo@gvavl1030010:~
$ ./a.sh
nohup: appending output to `nohup.out'
nohup: appending output to `nohup.out'
Job completed.
Job completed.
jbo@gvavl1030010:~
$ cat nohup.out


real    0m1.003s
user    0m0.001s
sys     0m0.000s

real    0m10.001s
user    0m0.000s
sys     0m0.000s

希望有所帮助

或者最简单的方法就是以这种方式调用你的shell,比如a.sh

time ./a.sh

nohup: appending output to `nohup.out'
nohup: appending output to `nohup.out'
Job completed.
Job completed.

real    0m10.01s
user    0m0.00s
sys     0m0.01s

答案 1 :(得分:0)