我希望这种方法可以帮助你。我认为一切都非常便携,除了“BC”也许。如果你需要的话,我可以给你一个“无BC”的版本。祝你好运!
#!/bin/bash
timeout=10; ## This is how long to wait before doing some batshit!
printed=1; ## this is how many times you want the message displayed (For #instance, you might want a message EVERY X seconds)
starttime="$(date +%F) $(date +%T.%3N)"
################### HERE GOES YOUR BACKGROUND PROCESS
sleep 30 &
#######################################################
processId=$! ## And here we got the procees Id
#######################################################
while [ ! -z "$(ps -ef | grep $processId | grep -v grep)" ]
do
endtime="$(date +%F) $(date +%T.%3N)";
timeelapsed=$(echo " $(date -d "$endtime" "+%s") - $(date -d "$starttime" "+%s") " | bc);
if [[ ($timeelapsed -gt $timeout) && ($printed -ne 0) ]]
then
echo "This is taking more than $timeout seconds";
printed=$((printed - 1));
starttime="$(date +%F) $(date +%T.%3N)"
fi
done
### Do something once everything finished
echo "The background process ended!!"