[HOWTO] detach a program from its (BASH) shell
Posted: 25 Jan 2019, 17:23
When you want or need to start a program or programs, meant to be running independently in the background, not bothering the shell with its output, be it error messages or normal info messages, and also not being killed when the shell that program got started is exited.
I presume the shell being a bash.
As example I use clipit.
lets run clipit in the background, and the shell is not bothered with info output.
But when the shell is exited, clipit also gets killed. Especially when you run other programs like so, say, Porteus not recognizes your linux swap file you created on a linux journalling filesystem partition, you do not want the swapon to be killed, just because the X terminal got killed, so swapon /mnt/sdx99/mycoolyeahlinuxswapfile will fail the moment the shell you executed that command exits.
Of course for swapon to work you have to first create a swap file, we presume that was already done:
And swapon runs in the background anyway, so starting it via would help not at all.
Now the trick to detach a program from its shell: ( and ) do the trick. But it depends on your shell, this syntax is not supported by every shell. Bash does support this syntax.
I again use clipit as example, first I exited every instance via "killall clipit"
Here is what happens, as seen in "jobs" showing background jobs of the current shell when just started clipit in the background, but not detached:
And here is what happens when you start clipit via the ( and ) trick:
For silencing the output of a started program, some prefer using
while I prefer
If anyone knows what the technical advantage is between one approach and the other, please enlighten us!
I presume the shell being a bash.
As example I use clipit.
Code: Select all
clipit & 2>&1 >/dev/null
But when the shell is exited, clipit also gets killed. Especially when you run other programs like so, say, Porteus not recognizes your linux swap file you created on a linux journalling filesystem partition, you do not want the swapon to be killed, just because the X terminal got killed, so swapon /mnt/sdx99/mycoolyeahlinuxswapfile will fail the moment the shell you executed that command exits.
Of course for swapon to work you have to first create a swap file, we presume that was already done:
Code: Select all
# file /mnt/sdx99/mycoolyeahlinuxswapfile
/mnt/sdx99/mycoolyeahlinuxswapfile: Linux/i386 swap file (new style), version 1 (4K pages), size WHATEVER pages, LABEL=LINUXSWAP, UUID=sOme-Random-UUID
Code: Select all
swapon /mnt/sdx99/mycoolyeahlinuxswapfile & 2>&1 >/dev/null
Now the trick to detach a program from its shell: ( and ) do the trick. But it depends on your shell, this syntax is not supported by every shell. Bash does support this syntax.
I again use clipit as example, first I exited every instance via "killall clipit"
Here is what happens, as seen in "jobs" showing background jobs of the current shell when just started clipit in the background, but not detached:
Code: Select all
guest@porteus:~$ clipit & 2>&1 >/dev/null
[1] 8780
guest@porteus:~$ jobs
[1]+ Running clipit &
guest@porteus:~$ killall clipit
guest@porteus:~$
[1]+ Terminated clipit
guest@porteus:~$ jobs
guest@porteus:~$
Code: Select all
guest@porteus:~$ (clipit & 2>&1 >/dev/null)
guest@porteus:~$ jobs
guest@porteus:~$
For silencing the output of a started program, some prefer using
Code: Select all
PROGRAM < /dev/null &>/dev/null &
Code: Select all
PROGRAM & 2>&1 >/dev/null