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

detailed bash log with preexec and precmd #148

Open
alperyilmaz opened this issue Oct 6, 2023 · 2 comments
Open

detailed bash log with preexec and precmd #148

alperyilmaz opened this issue Oct 6, 2023 · 2 comments

Comments

@alperyilmaz
Copy link

Hi, as I mentioned in previous issue #147 I'm recording bash history in a custom way. As of now, I'm trying to save the command (and current directory) to a file using preexec. Can I use precmd to keep duration and exit code of the command and then also save them?

Actually, is it possible for preexec() to keep the command as variable and then pass it to precmd as input? In that case precmd() has everything (i.e. command, folder, duration, exit code) to save to a log file.

@dseomn
Copy link
Contributor

dseomn commented Oct 6, 2023

I don't remember if it's possible to pass variables between the two, but when I wanted to do something similar, I decided to separately log when the command began and when it ended. With timestamps, so the duration can be calculated. That way if the command crashed the shell or the system, or caused the disk to run out of space, there would still be a chance that the command was logged before it started running. In case it's useful to you, here's the code I use to log shell commands with preexec and precmd: https://github.com/dseomn/dotfiles/blob/598a37a20bf5c413e634119efd8f8f922571fc60/.config/shell/interactive.d/50-history.bash#L56-L101

I think the only part that uses something other than standard bash features or bash-preexec is BPE_LAST_COMMAND_DID_BG on line 76 which is from https://github.com/dseomn/dotfiles/blob/public/.config/shell/interactive.d/20-bash-preexec.bash. https://github.com/dseomn/dotfiles/blob/public/.local/bin/shell-history is the command that actually logs the args passed to it from bash, but all the interesting bits are in bash.

@dimo414
Copy link
Collaborator

dimo414 commented Oct 6, 2023

Here's an example of capturing duration and exit code via the preexec/precmd hooks (this example actually uses the PROMPT_COMMAND and DEBUG trap directly by default, but if bash-preexec is found in the shell environment it delegates to bash-preexec instead).

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