Linux tricks and tips!

Automate workflows with entr

This is one of my favorites. entr tool provides a separation-of-concerns style method of automating arbitrary commands when s or directory changes.

Examples

    # Auto-compile or install your software project any time you update the code:
    ag -l | entr -p ./setup.py install
    find . -iname "*.py" | entr -p ./setup.py install

    ag -l | make install

    # Same thing is possible to e.g. run your test suite
            

Bash leave no command history

Sometimes it's helpful to not pollute your .bash_command history. You will lose the functionality of up-arrows to view previous command and any sort of saving the commands, however. Only known to work in BASH.

    How to leave no .bash_history

    : > ~/.bash_history # truncates history file
    kill -9$$           # kills pid of current shell

    Alternative:

    unset HISTFILE
    set +o history
    history -c
            

ltrace wrapper

This is another sweet one. Sometimes you want to debug a binary, and watch what sort of system calls and/or library calls. The way you use this is create a new file, strace_wrapper.sh or ltrace_wrapper.sh copied below and edit to run the file you want to run at the bottom. Open two terminals, we want two windows to see the program on one side and the system/library calls on the other. First, run the strace_wrapper.sh and it will give you a PID. Second, run strace -p $PID in the other terminal. Finally, on the first terminal where thhe program is running hit enter to continue program. You will see the syscalls in the case of strace (may require root) or library calls to shared libraries in the case of ltrace. You can also use the file variable so you don't have to type the PID every time you run it.

    #!/bin/sh

    # the pid could be sent to a file or named pipe the-thing-that-does-
    # strace could be watching, though in most cases a human could copy it
    echo "PID to strace is $$"
    echo "$$" > /tmp/strace.pid

    trap : USR2

    # block while human copies pid over to strace (or some program acts on
    # the above pid being written in that other terminal). Busy loop or
    # something instead if this program must not consume input (`sleep`
    # complicates the signal handling)
    read blocking

    # then when strace is up, send this process a USR2 signal. this will
    # mean there will be some strace of this script in addition to the
    # target, hence the kinda sorta caveat.
    exec ./EXECUTABLE_FILE_NAME_HERE

            

Making HTTP and HTTPS requests manually


    Making HTTP and HTTPS requests manually

    telnet alistapart.com

    GET / HTTP/1.1<shift-enter to go to newline without sending>
    Host: alistaparrt.com<enter>

    openssl s_client -connect alistapart.com:443

    GET / HTTP/1.1<shift-enter to go to newline without sending>
    Host: alistapart.com<enter>
            

Execute python code on remote computer (Linux or ssh-server supporting only)

    Execute Python on Remote
    cat <file>.py | ssh remotehost.com python -