Save in a variable the number of seconds a process took to run

后端 未结 5 799
走了就别回头了
走了就别回头了 2021-02-04 17:15

I want to run a process in bash and save in an env variable the number of seconds it took to run. How would I do such a thing?

5条回答
  •  难免孤独
    2021-02-04 17:43

    Using $SECONDS wasn't working for me in a script run by cron (though it worked when I ran it directly; I used cron for the first time today, so there could be some user error too). @Farzy's answer using TIMEFORMAT worked but I didn't want to redirect the output from the timed command. Here's an alternative to $SECONDS if you're in a similar situation:

    start=$(date +%s)
    your_command
    seconds=$(($(date +%s) - $start))
    

提交回复
热议问题