How to "correctly" start an application from a shell

There are a few ways way to execute a program and detach it from a terminal. One is to run it in the background of a subshell, like this (replace firefox with your favorite program):

(firefox &)

Another is to disown the process:

firefox & disown firefox

If you are curious about how app launchers work, dmenu provides 1 binary and 2 shell scripts: dmenu, dmenu_path and dmenu_run, respectively.

dmenu_run pipes the output of dmenu_path into dmenu, which in turn pipes into whatever your $SHELL variable is set to. If it is empty, it will use /bin/sh.

#!/bin/sh
dmenu_path | dmenu "$@" | ${SHELL:-"/bin/sh"} &

dmenu_path is a bit more complex, but in short it provides a list of binaries in your $PATH environment variable, and uses a cache if possible.

#!/bin/sh
cachedir=${XDG_CACHE_HOME:-"$HOME/.cache"}
if [ -d "$cachedir" ]; then
        cache=$cachedir/dmenu_run
else
        cache=$HOME/.dmenu_cache # if no xdg dir, fall back to dotfile in ~
fi
IFS=:
if stest -dqr -n "$cache" $PATH; then
        stest -flx $PATH | sort -u | tee "$cache"
else
        cat "$cache"
fi

It isn't necessary to have programs running in shells. Another way to write dmenu_run, without piping into a shell would be:

#!/bin/sh
$(dmenu_path | dmenu "$@") &

Well, you seem to have a pretty good understanding of it.  To clarify some of what you have,

  • sh -c /path/to/Program is fairly similar to

    $ sh
    % /path/to/Program
    % Ctrl+D                             (or you could type “exit”)
    $ 

    where you start a new shell process, provide the application command path to the new shell, and then let the new shell terminate.  I have shown the new shell giving a different prompt for illustration purposes; this probably wouldn’t happen in real life.  The sh -c "command" construct is mostly useful for doing tricky stuff, like wrapping multiple commands into a bundle, so they look like a single command (sort of a single-use unnamed script), or building complicated commands, possibly from shell variables.  You would hardly ever use it just for running a single program with simple argument(s).

  • 2>&1 means redirect the standard error to the standard output. This doesn’t really have much to do with &; rather, you use it when a command sends error messages to the screen even if you say command > file and you want to capture the error messages in the file.
  • Redirecting output to nohup.out is a trivial side-effect of nohup.  The primary purpose of nohup command & is to run command asynchronously (commonly known as “in the background”, or as a “shell independent process”, to use your words) and configure it so it has a better chance of being able to continue to run if you terminate the shell (e.g., logout) while the command is still running.

bash(1) and the Bash Reference Manual are good sources of information.


I like G-Man's answer a lot. But I'm responding because I think you are confusing concerns. As Wayne points out, the best answer is "whatever gets the results you want".

In Unix process management, every process has a parent. The one exception to this is the init process which is started by the OS at boot. It is normal behavior for a parent process to take all of it's child processes with it when it dies. This is done by sending the SIGHUP signal to all child processes; the default handling of SIGHUP terminates the process.

The shell spawning of user processes is no different than if you coded the fork(2)/exec(3) calls in the language of your choice. The shell is your parent, and if the shell terminates (e.g., you log off), then the child process(es) it spawns goes with it. The nuances you describe are just ways of modifying that behavior.

exec /path/to/program is like calling exec(3). Yes, it will replace your shell with program, keeping whatever parent launched the shell.

sh -c /path/to/program kind of pointlessly creates a child shell process that will create a child process of program. It is only valuable if /path/to/program is actually a sequence of script instructions, and not an executable file. (sh /path/to/script.sh can be used to run a shell script that lacks execute permissions in an inferior shell)

/path/to/program creates a "foreground" process, meaning that the shell waits for the process to complete before taking any other action. In system call context, it is like fork(2)/exec(3)/waitpid(2). Note that the child inherits stdin/stdout/stderr from the parent.

/path/to/program & (ignoring redirection) creates a "background process". The process is still a child of the shell, but the parent is not waiting for it to terminate.

nohup /path/to/program invokes nohup(1) to prevent the sending of SIGHUP to program if the controlling terminal is closed. Whether it is in the foreground or background is a choice (although most commonly the process is backgrounded). Note that nohup.out is only the output if you don't otherwise redirect stdout.

When you put a process in the background, if the parent process dies, one of two things happens. If the parent is a controlling terminal, then SIGHUP will be sent to the children. If it is not, then the process may be "orphaned", and is inherited by the init process.

When you redirect input/output/error, you are simply connecting the file descriptors that every process has to different files than the ones it inherits from its parent. None of this affects process ownership or tree depth (but it always makes sense to redirect all 3 away from a terminal for background processes).

With all that said, I don't think you should be concerned with process creation by the shell or sub-shells or sub-processes, unless there is a specific problem you are addressing related to process management.

Tags:

Shell

Exec