Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display command execution time #4

Closed
alexbardas opened this issue May 26, 2015 · 7 comments
Closed

Display command execution time #4

alexbardas opened this issue May 26, 2015 · 7 comments

Comments

@alexbardas
Copy link

I want to display the time spent running a command. I've defined the following functions:

preexec () {
    date_before=$(date +%s.%N)
}

precmd () {
    if [ -z "$date_before" ]; then
        return 0
    fi

    local date_after
    date_after=$(date +%s.%N)
    printf "\n%.2fs\n\n" $(bc<<<$date_after-$date_before)
}

On bash version 4.3.33(1)-release (x86_64-apple-darwin14.1.0) on a mac works as expected. However, on a ubuntu 14.04 version 4.3.11(1)-release (x86_64-pc-linux-gnu) it doesn't.

It looks like the order of execution on mac is prexec -> precmd, while on linux is precmd - > preexec, but even if I swap the function names on linux it still doesn't work as expected.

Also, I've used gdate instead of date on mac. (from coreutils)

Any ideas how to make it work on linux?

@rcaloras
Copy link
Owner

FYI can you just use the time command? e.g. time echo "hello" My guess is you need to do this for every command though :)

Weird, I use on both mac and linux and it's fine.

Here's what my system looks like for linux. Same as yours.

elementz@Kashmir:~/git/bashhub (master)$ uname -a
Linux Kashmir 3.13.0-24-generic #47-Ubuntu SMP Fri May 2 23:30:00 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
elementz@Kashmir:~/git/bashhub (master)$ echo $BASH_VERSION 
4.3.11(1)-release
elementz@Kashmir:~/git/bashhub (master)$ bash --version
GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)

I can also use your setup fine and it works on my linux box:

elementz@Kashmir:~/git/bashhub (master)$ preexec () {
     date_before=$(date +%s.%N)
 }
elementz@Kashmir:~/git/bashhub (master)$ 

elementz@Kashmir:~/git/bashhub (master)$ precmd () {
     if [ -z "$date_before" ]; then
         return 0
     fi

     local date_after
     date_after=$(date +%s.%N)
     printf "\n%.2fs\n\n" $(bc<<<$date_after-$date_before)
}
elementz@Kashmir:~/git/bashhub (master)$ sleep 1

1.01s

You might be using something else in your PROMPT_COMMAND. What does it return?

elementz@Kashmir:~/git/bashhub (master)$ echo "$PROMPT_COMMAND"
__bp_precmd_invoke_cmd;  __bp_interactive_mode;

Something also may be interfering with Bash's DEBUG trap. Do you have any other bash plugins you are using?

@d630
Copy link
Contributor

d630 commented May 28, 2015

Run also set -o verbose or set -o xtrace to see what is going on

@alexbardas
Copy link
Author

The environments (mac and ubuntu) were almost identical, but there was only 1 difference: on ubuntu I am also using z. After I've installed z on mac, it has also started to misbehave.
Given that z is a popular plugin, is there any possibility to run both z and preexec ?

echo "$PROMPT_COMMAND"
__bp_precmd_invoke_cmd; history -a; echo -ne "\\033]0;${PWD##*\/}\\007"; __bp_interactive_mode;
_z --add "$(command pwd -P 2>/dev/null)" 2>/dev/null;

3.61s
set -o verbose
__bp_precmd_invoke_cmd; history -a; echo -ne "\\033]0;${PWD##*\/}\\007"; __bp_interactive_mode;
__bp_preexec_invoke_exec
type -t $precmd_function
date +%s.%N
bc<<<$date_after-$date_before

37.54s

__bp_preexec_invoke_exec
__bp_preexec_invoke_exec
__bp_preexec_invoke_exec
_z --add "$(command pwd -P 2>/dev/null)" 2>/dev/null;
__bp_preexec_invoke_exec
__bp_trim_whitespace "$1"
__bp_trim_whitespace "$command"
__bp_trim_whitespace "$command"
__bp_trim_whitespace "$command"
__bp_trim_whitespace "$command"
__bp_trim_whitespace "$command"
history 1 | sed -e "s/^[ ]*[0-9]*[ ]*//g"
type -t $preexec_function
date +%s.%N
command pwd -P 2>/dev/null
[[ -n $(git branch 2> /dev/null) ]] && echo " "
git branch 2> /dev/null
parse_git_branch
git rev-parse --abbrev-ref HEAD  2> /dev/null

@rcaloras
Copy link
Owner

Just installed z and played with it a bit. Cool plugin. I believe there's a simple fix :)

Just make sure bash-preexec.sh is imported/sourced after you import z.sh. The __bp_interactive_mode function needs to be the last thing in your prompt command. Preexec needs that function to be able to tell when you're entering commands on the comamnd line vs something like your git prompt or z which is invoked as part of PROMPT_COMMAND.

So your PROMPT_COMMAND should have __bp_interactive_mode last like this:

19:32:38-rcaloras-~$ echo $PROMPT_COMMAND 
__bp_precmd_invoke_cmd; _z --add "$(command pwd -P 2>/dev/null)" 2>/dev/null; __bp_interactive_mode;

give that a shot.

@d630
Copy link
Contributor

d630 commented May 28, 2015

You must source z before bash-preexec

@rcaloras
Copy link
Owner

Added f4c5adb for clarity

@alexbardas
Copy link
Author

Thanks, it solved my issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants