GithubHelp home page GithubHelp logo

0xzdh / cmdw Goto Github PK

View Code? Open in Web Editor NEW
5.0 1.0 2.0 57 KB

Command line wrapper to support logging start/stop times of executed commands

License: MIT License

Shell 100.00%
bash-prompt zsh-prompt bash zsh bashrc zshrc

cmdw's Introduction

cmdw - Command Wrapper

This is a command line wrapper to support timestamping start/stop times of command executions. This can also act as a template for custom command logging. It currently aims to support both Bash and Zsh.

This tool implements bash-preexec and is inspired by bash-command-timer.

# -- Example Output
$ echo 'Command Wrapper!'
Command Wrapper!

start    Wed 07 Oct 2020 05:00:00 AM UTC
stop     Wed 07 Oct 2020 05:00:00 AM UTC

Installation:

  1. Write this script to your home folder as .cmdw:
    • vim ~/.cmdw
  2. Source the cmdw script:
    • source ~/.cmdw
  3. For persistence, add it to your .bashrc or similar:
    • source "$HOME/.cmdw"

Install one-liner: source <(curl -s https://raw.githubusercontent.com/0xZDH/cmdw/master/install.sh)

All Users

The install-all.sh script will install and setup cmdw for all users on the system.

One-liner: source <(curl -s https://raw.githubusercontent.com/0xZDH/cmdw/master/install-all.sh)

History Size:

If required, the user can modify the size of the maintained history file by setting CMDWHISTSIZE as an environment variable (Default: 10,000):

  • export CMDWHISTSIZE=1000

For uses that require larger history files, it is best to include and set the CMDWHISTSIZE variable in the terminals rc file prior to the sourcing of the cmdw tool. For example, add the following to your rc file above the cmdw source line to allow the history file length to grow up to 1,000,000 lines:

  • export CMDWHISTSIZE=1000000

Command Referencing:

  1. List the executed commands in .cmdw_history with line numbers via:
    • cat -n .cmdw_history | grep '#'
  2. Then, grab the command and its timestamps using (where N is the identified line number):
    • sed -n 'N,$p' .cmdw_history | head -n3

For easy referencing, use the builtin function:

  • cmdw_history
  • cmdw_history <id>
# -- Example history command output
$ cmdw_history 
 1   echo 'Command Wrapper!'

# -- Example grabbing specific history item to view timestamps
$ cmdw_history 1
# echo 'Command Wrapper!'
start    Wed 07 Oct 2020 05:00:00 AM UTC
stop     Wed 07 Oct 2020 05:00:00 AM UTC

Enable/Disable:

By default, cmdw is enabled. To disable/enable the wrapper, use the built in functions:

  • cmdw_disable
  • cmdw_enable

Or export the following variable to disable the wrapper:

  • export CMDW_ENABLE=0

And to re-enable the wrapper:

  • export CMDW_ENABLE=1

To default disable the wrapper, add the following to your .bashrc or similar after cmdw is sourced:

  • export CMDW_ENABLE=0

Ignoring commands:

Commands to be ignored by cmdw are stored in the CMDW_IGNORE environment variable. The user can directly modify this array in a given terminal session or globally in their .bashrc or similar. To add a command to the list, whether globally or locally per session:

  • CMDW_IGNORE+=('command')

And to allow the cmdw wrapper to log on every command:

  • CMDW_IGNORE=()

Timezone Handling:

Contributed by: Mr.V

By default, cmdw is set to display timestamps in UTC format. To update the timezone, use the built in function:

  • cmdw_timezone
  • cmdw_timezone <timezone>

Or export the following variable to set the timezone:

  • export CMDW_TIMEZONE=<timezone>

To default the wrapper timezone, add the following to your .bashrc or similar after cmdw is sourced:

  • export CMDW_TIMEZONE=<timezone>

cmdw's People

Contributors

0xzdh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

cmdw's Issues

New bash version causing issues

As of Bash version 5.1.*-release this script does not execute correctly. The script continues to run fine on version 5.0.*-release and zsh.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.