Search code examples
linuxbashnohup

Simply or Alias running a command with nohup


I have some long running processes that I run with nohup and redirect any output to a log. Since I usually like to monitor directly, at least at the beginning, I add a tail as well. This end up looking something like this.

nohup myprocess.sh >> 20180611.log 2>&1 & tail -f 20180611.log

Not that this is super complicated, but I was hoping I could find a way to create an alias for it. I could probably do this with a wrapper script, but I didn't want to clutter the directory with essentially two scripts per process (hence me thinking of an alias). Thanks!


Solution

  • An alias is a word that is replaced with something else (a longer fragment of a command) when it is the first word of the command. It cannot take arguments.

    You can, however, write a shell function (in your .bashrc) to do the processing you want (and pass the process name and the log file as arguments).

    It could look like this (in .bashrc):

    function no-hup() {
        nohup "$1" >> "$2" 2>&1 & tail -f "$2"
    }
    export -f 'no-hup'
    

    You run it like this:

    no-hup myprocess.sh 20180611.log