Thursday, October 24, 2019

nohup, running command to background

http://felixmilea.com/2014/12/running-bash-commands-background-properly/

copy some text here for later using



Using & with disown

Running disown after running a command in the background, removes the command
from the shell’s job list, which makes the shell not send a HUP signal to the
command when the shell session ends. However, even though the command is no
longer a job, the command’s output is still attached to the shell output.
disown2 Notice that even after running disown, output from iceweasel is still
printed to the console. Also notice that our command isn’t listed under jobs.
disown Once again, redirecting output to /dev/null makes things less messy.
Again, notice that our command isn’t listed as a job.  Using & with nohup

The nohup command makes a command ignore the HUP signal from the start, meaning
that it can keep running after the shell session ends. One thing about the
nohup command that I find sort of annoying is that by default all output is
redirected to a nohup.out file. You can still redirect output to /dev/null to
prevent nohup from creating a nohup.out file. The main advantage of using nohup
over disown is that the process is set to ignore HUP from the beginning and is
accomplished in a single command as opposed to two.  nohup2 This is pretty
smooth, except for the one message being printed out, and the nohuput.out file
being created.  nohup Redirecting output to /dev/null probably makes this the
cleanest solution so far.  Custom command

While nohup or disown in conjunction with & and possibly &>/dev/null works, I
find it pretty annoying to type all that every time I want to run something in
the background, especially when I want to discard its output.

An alternate solution is to implement our own simple bash script that will
remove the command from the shell’s job list and redirect all of its output to
/dev/null unless a file descriptor other than a terminal is provided:

#!/bin/sh

if test -t 1; then exec 1>/dev/null fi

if test -t 2; then exec 2>/dev/null fi

"$@" &

Thank you rentedr in the comments for the cleaner version of this script.

The script works by putting together all the arguments as a string and
evaluating it as a command. The script also checks stdout and stderr to see if
they point to a terminal, and if they do, it will pass /dev/null instead,
otherwise it will let the command inherit the specified stdout and stderr.

Below are some use cases for the command (named it ds on my system, short for “
disown silently”): ds Pretty clean solution, all output goes to /dev/null.
Notice there is no need for using & ds2 Same as above, but redirects stderr to
a file. We can check the file contents using cat and see stderr is indeed being
directed there.  Using screen

    Screen can also be used to detaching a command from the current shell
    session. Other terminal multiplexer like tmux also support this feature.
    Screen accomplishes this differently than the above methods, namely by
    creating a new shell session, detaching that shell session from the current
    shell session, and then running our command in the new shell session. This
    can be nice because while the command is now detached and out of your way,
    we can always reattach to it later and see its output, interact with it,
    etc. So the main difference between screen and the above methods is that
    the command is not only detached from the current shell session, it runs in
    it’s own shell session, and output is still preserved to a terminal if you
    wanted to check it out later. Do note that Screen is a GNU software
    application and while it ships with most Linux distros, it is not part of
    the POSIX standard, meaning your system might not come with it by default
    (unlike all the other methods discussed here).  screen -d -m options are
    required to detach the command to a new shell session. -S option is used to
    name our screen session that way we can easily reattach to it later.
    screen3 we can use “screen -d” to view all detached screen sessions and “
    screen -r” to reconnect to our session by the session id.  screen I
    reconnected to my detached session and I can now see the output from
    minecraft.  Conclusion

    Found a factual mistake in the article? Let me know and I’ll correct it.
    This command along with some other bash scripts I wrote can be found on
    Github: https://github.com/felixmc/Bash-Scripts

No comments:

Post a Comment