How to «correctly» start an application from a shell
Update 1: I can illustrate what e.g. dmenu does reconstructing it from repeated calls to ps -efl under different conditions. It spawns a new shell /bin/bash and as a child of this shell the application /path/to/Program . As long as the child is around so long will the shell be around. (How it manages this is beyond me. ) In contrast if you issue nohup /path/to/Program & from a shell /bin/bash then the program will become the child of this shell BUT if you exit this shell the program’s parent will be the uppermost process. So if the first process was e.g. /sbin/init verbose and it has PPID 1 then it will be the parent of the program. Here’s what I tried to explain using a graph: chromium was launched via dmenu , firefox was launched using exec firefox & exit :
systemd-+-acpid |-bash---chromium-+-chrome-sandbox---chromium-+-chrome-sandbox---nacl_helper | | `-chromium---5*[chromium-+-] | | |-] | | |-] | | |-] | | `-3*[]] | |-chromium | |-chromium-+-chromium | | |- | | `- | |- | |-3*[] | |- | |-5*[] | |- | |- | |- | |- | |- | |- | |- | |- | |- | |- | |- | |- | |- | |-2*[] | |- | |- | |- | |- | |- | |- | |- | |- | |- | |- | |- | |- | |- | |- | `- |-2*[dbus-daemon] |-dbus-launch |-dhcpcd |-firefox-+-4*[] | |- | |- | |- | |-3*[] | |- | |- | |- | |- | |- | |- | |- | |- | |- | |- | |- | |- | |- | |- | |- | |- | `- |-gpg-agent |-login---bash---startx---xinit-+-Xorg.bin-+-xf86-video-inte | | `- | `-dwm-+-dwmstatus | `-xterm---bash-+-bash | `-pstree |-systemd---(sd-pam) |-systemd-journal |-systemd-logind |-systemd-udevd |-wpa_actiond `-wpa_supplicant
Update 2: I guess the question can also be boiled down to: What should be the parent of a process? Should it e.g. be a shell or should it be the init process i.e. the process with PID 1 ?
dang, garbage — you ask some good questions. but I think wayne’s on the nose here — your latest edit asks about init — to which the answer might be. maybe? it depends on how/if you plan to talk to it, what init you use, and where the data channels are. In general that stuff will tend to work itself out — that’s what init is for. In any case, usually when you daemonize a process then init . Or if you want job control, current shell.
Hahaha, cheers @mikeserv; 4:37 am in the morning here and already the first laugh of the day. True, that stuff always somehow works out. I will remove dmenu and see how I get along with what I learned. I find exec /path/to/Program & exit or /bin/bash -c /path/to/Program & exit to be quite usable. But they all make 1 i.e. init the parent of the Program which is fine with me as long as this makes sense and does not violate any basic *nix principles.
@lord.garbage — that’s because you exec & , I think. I usually just do my stuff from the terminal. maybe you’d get some use out of ben crowell’s question here. I have an answer there, but all of them are very good. anyway, when you background a process and its parent dies like: sh -c ‘cat & kill $$’ you orphan it, and it winds up getting reaped eventually. that’s init’s job — that’s why they all fall to it.
Maybe a simpler question for now is: How is it possible to get the above process tree from the shell: systemd—bash—chromium . All methods I try will ultimately lead to a process tree of the following form systemd—chromium when I spawn firefox from the shell. How is the shell demonized here? It is not associated with any terminal.
3 Answers 3
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):
Another is to disown the process:
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 .
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=$ 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:
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.
Linux equivalent of the DOS «start» command?
In windows I can do start abcd.pdf that opens the pdf file in the registered viewer. Is there anything similar in Linux?
Just to add the importance of the final & , in python it does not execute the next line of code without it. So if you are automating, that & right there is the most important thing, if you want to wait until it ends, not put it there, but if you do not want to wait put it there, that is what I observed, @vartec highlighted, very important part.
maybe it´s not a seperate window that gets started, but you can run some executables in background using «&»
means your script will not wait until myexecutable has finished but goes on immediately. maybe this is what you are looking for. regards
xdg-open is a good equivalent for the MS windows commandline start command: xdg-open file opens that file or url with its default application xdg-open . opens the currect folder in the default file manager
One of the most useful terminal session programs is screen.
screen -dmS title executable
You can list all your screen sessions by running
And you can connect to your created screen session (also allowing multiple simultaneous/synchronized sessions) by running
This will open up the emulated terminal in the current window where executable is running. You can detach a screen session by pressing C-a C-d, and can reattach as many times as you wish.
If you really want your program started in a new terminal window, you could do something like this: xterm yourtextmodeprogram or gnome-terminal -e yourtextmodeprogram or konsole -e mc
Trouble is that you cannot count on a particular terminal emulator being installed, so (again: if you really want to do this) you would need to look for the common ones and then execute the first one encountered.
As Joachim mentioned: The normal way to do this is to background the command (read about shell job control somewhere, if you want to dig deeper).
There are also cases where you want to start a persistent shell, i.e. a shell session which lives on when you close the terminal window. There are two ways to do this: