Not a script person. I am trying to build a repetitive test to see how long a command takes to run, marks if it's successful or not, and adds a time stamp, and appends everything to a log file.

Oh, and this is in bash on a mac.

To try to parse out just the "real" I've tried things like:

time ./login.sh | sed -n 's/.*real://p' >> output.txt


(time ./login.sh) 1> /dev/null 2> output.txt

The login.sh script runs, where it appends Succeeded or Failed:

application login -u "user" -p '******' "host.domain.com" | grep "Succeeded" &> /dev/null
if [ $? == 0 ]; then
    echo "  Suceeded" >> output.txt
    echo "  Failed" >> output.txt

I'm going to put all of that into a For Loop with a sleep command so that it occurs every 5 minutes:

for n in {1..1000};
    # Command that Adds the time stamp
    # the command to find the real time above once I get it working
    sleep 5m

Desired Output would be akin to:

Sun Jul 29 16:15:06 PDT 2019 real   0m0.815s  Suceeded
Sun Jul 29 16:20:06 PDT 2019 real   0m0.635s  Suceeded
Sun Jul 29 16:25:06 PDT 2019 real   0m1.053s  Suceeded
Sun Jul 29 16:30:06 PDT 2019 real   0m15.653s  Failed

2 Answers 2


Use /usr/bin/time instead of the shell built-in time. Then capture the stderr into a separate file from the stdout of your application command. Since the elapsed real time is the first word of time's output, that's easy to grab with awk.

grep has a -q switch so no need to redirect to /dev/null.

Finally, sleep accepts an argument in seconds, so I don't think sleep 5m is going to do what you want. The syntax is accepted though, so I let it stand.

for n in {1..1000}

    /usr/bin/time application login -u "user" \
             -p '******' "host.domain.com" > login.out 2>time.out

    realtime=$(awk '{print $1}' time.out)

    echo -n "$(date) real $realtime " >> output.txt

    if grep -q "Succeeded" login.out; then
        echo "Succeeded" >> output.txt
        echo "Failed"   >> output.txt

    sleep 5m


Don't grep/awk/sed, just use time's format options, e.g. time -f "%e" command will output just the elapsed real number of seconds the command took, and ONLY that.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .