Shell – Why do some commands ‘hang’ the terminal until they’ve finished

command linejobsshell

Sometimes you run a program from the terminal, say, lxpanel. The terminal won't drop you back to the prompt, it'll hang. You can press Ctrl+C to get back to the prompt, but that will kill lxpanel. However, pressing Alt+F2 (which pops up a window to take a command) and running lxpanel works gracefully.

Why is this? What is different between running a command from the terminal and from the 'run' window that appears when you press Alt+F2?

lxpanel here was just used as an example. I have experienced this with multiple programs

Best Answer

By default the terminal will run the program in the foreground, so you won't end up back at the shell until the program has finished. This is useful for programs that read from stdin and/or write to stdout -- you generally don't want many of them running at once. If you want a program to run in the background, you can start it like this:

$ lxpanel &

Or if it's already running, you can suspend it with Ctrl+Z and then run bg to move it into the background. Either way you will end up with a new shell prompt, but the program is still running and its output will appear in the terminal (so it can suddenly show up while you're in the middle of typing)

Some programs (typically daemons) will fork a separate process when they start, and then let the main process immediately exit. This lets the program keep running without blocking your shell

Related Question