计算shell脚本执行时间(随手记)
#!/bin/bash
#该脚本查看局域网中存活的主机,前提主机没有禁ping。
#fast_ping.sh
#其中()& ()中的命令作为字shell来运行,而&会将其放置到后台,多个shell,更快
starttime=`date +'%Y-%m-%d %H:%M:%S'`
for ip in 192.168.1.{1..255}
do
(
ping $ip -c 2 &>/dev/null
if [ $? -eq 0 ];then
echo "$ip is alive"
else
echo "$ip is down"
fi
)&
done
wait
endtime=`date +'%Y-%m-%d %H:%M:%S'`
start_seconds=$(date --date="$starttime" +%s);
end_seconds=$(date --date="$endtime" +%s);
echo "本次运行时间: "$((end_seconds-start_seconds))"s"
shell脚本运行时间,下次用到可直接copy
starttime=`date +'%Y-%m-%d %H:%M:%S'` 需执行的程序 endtime=`date +'%Y-%m-%d %H:%M:%S'` start_seconds=$(date --date="$starttime" +%s); end_seconds=$(date --date="$endtime" +%s); echo "本次运行时间: "$((end_seconds-start_seconds))"s"