Ubuntu: Why do some applications via the terminal detach automatically and some don't?



Question:

This is quite an interesting thing I noticed, for instance:

  • I use Atom Editor and I call it via my Terminal using:

    $ atom  

    and press Enter, which triggers the Editor and also make the terminal cursor point to the new line; making it (Terminal) free for further command line utilities.

  • However, when in use RhythmBox using rhythmbox as terminal command, it tends to work on the foreground (I guess) and keeps the terminal occupied till I close Rhythmbox or use CTRL+C.

I tried this with texmaker too and it performs similar thing as Rhythmbox. I know that & will take the process on the background but is there a logical explanation for some applications as mentioned here?

Any hacks or tips so that I can learn how to tweak things like this?

Note

I try using CTRL+Z on the above mentioned applications and the apps then gray out and become unresponsive and I have to force quit on them

Environment

  • Using Ubuntu 16.10

  • Gnome Terminal

  • echo $SHELL give /bin/bash


Solution:1

As has been pointed out in the comments, such behavior is specific to each application. Applications written in C programming language, for example, can employ setsid call to disconnect from controlling terminal, although they require call to fork() syscall first. Python, also has os.setsid() and os.fork() functions; although it is possible to simply use os.fork() to create a child process and kill the parent.

Another very frequent technique that software authors use is launching applications via wrapper script, and calling a new process via nohup. That's exactly what atom does:

$ file $(which atom)  /usr/bin/atom: Bourne-Again shell script, ASCII text executable  $ grep 'nohup' $(which atom)                                                                  nohup "$ATOM_PATH" --executed-from="$(pwd)" --pid=$$ "$@" > "$ATOM_HOME/nohup.out" 2>&1        cat "$ATOM_HOME/nohup.out"  

You can do so as well. For instance, the way I tend to launch programs and detach them from terminal is via a function, that launches desired program with nohup already appended to the command:

runstuff() {      nohup "$@" >/dev/null 2>&1 &   }   

Once you define that in your .bashrc , you can launch firefox like so :

runstuff firefox  

Another way, is via setsid command (which is named same as the C system call, but is actually a standalone binary):

setsid firefox  


Solution:2

If you use Ubuntu with X environemnt you can push Ctrl + F2 and then put the command you need (i.e. rythmbox). It won't have separate terminal where you can push ctrl+C.

You can also try to run nohup rythmbox &. It should run your player in backgroud and make it terminal-close-proof.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »