[HOWTO] detach a program from its (BASH) shell

Post tutorials, HOWTO's and other useful resources here.
Post Reply
User avatar
Rava
Contributor
Contributor
Posts: 2252
Joined: 11 Jan 2011, 02:46
Distribution: Porteus 4.0 x86-64 XFCe
Location: Forests of Germany

[HOWTO] detach a program from its (BASH) shell

Post#1 by Rava » 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.

Code: Select all

clipit & 2>&1 >/dev/null
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:

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
And swapon runs in the background anyway, so starting it via

Code: Select all

swapon /mnt/sdx99/mycoolyeahlinuxswapfile & 2>&1 >/dev/null 
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:

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:~$ 
And here is what happens when you start clipit via the ( and ) trick:

Code: Select all

guest@porteus:~$ (clipit & 2>&1 >/dev/null)
guest@porteus:~$ jobs
guest@porteus:~$ 
B)

For silencing the output of a started program, some prefer using

Code: Select all

PROGRAM < /dev/null &>/dev/null &
while I prefer

Code: Select all

PROGRAM & 2>&1 >/dev/null
If anyone knows what the technical advantage is between one approach and the other, please enlighten us!
Cheers!
Yours Rava

User avatar
Ed_P
Contributor
Contributor
Posts: 5136
Joined: 06 Feb 2013, 22:12
Distribution: 4.0 Cinnamon 64-bit ISO
Location: Western NY, USA

Re: [HOWTO] detach a program from its (BASH) shell

Post#2 by Ed_P » 25 Jan 2019, 21:50

Not sure if this helps but .desktop files have this option "Terminal=false" which allows programs to execute, ie "Exec=firefox https://us....", without a terminal shell.

To suppress output I use " > /dev/null 2>&1 & ".
Ed

User avatar
Rava
Contributor
Contributor
Posts: 2252
Joined: 11 Jan 2011, 02:46
Distribution: Porteus 4.0 x86-64 XFCe
Location: Forests of Germany

Re: [HOWTO] detach a program from its (BASH) shell

Post#3 by Rava » 25 Jan 2019, 22:12

Ed_P wrote:
25 Jan 2019, 21:50
Not sure if this helps but .desktop files have this option "Terminal=false" which allows programs to execute, ie "Exec=firefox https://us....", without a terminal shell.
It is not exactly the same, like my above example of swapon.

A user could create a custom swapon command using the trick without a terminal shell, but the moment X got shut down, the swap file would also be swapoff'd.

With swap, the user usually wants to have it active and ready to be used as long as the system is running. E.g. the user might want to shut down X, remove the DE module and activate a different DE module, then startx again. The very user could get into trouble when he switches from, say, Openbox or XFCE to KDE by doing so, and when he used the swap file for the slim DEs, he sure would need the swap file for KDE as well.

You can try it yourself. You just need a swap file, create a .desktop file (that needs root privilege) that swapon's the very same swap file, then exit X e.g. by switching to init 3, and when you log into a virtual console ("text mode"), you can see for yourself if the .desktop swapon'd swap file is still there. I am quite sure exiting X also means all programs that got started via a .desktop file are also terminated.
Cheers!
Yours Rava

User avatar
Ed_P
Contributor
Contributor
Posts: 5136
Joined: 06 Feb 2013, 22:12
Distribution: 4.0 Cinnamon 64-bit ISO
Location: Western NY, USA

Re: [HOWTO] detach a program from its (BASH) shell

Post#4 by Ed_P » 26 Jan 2019, 05:13

Actually Rava I have never used a swap file with a Porteus system, or any other that I'm aware of. And sharing one between DEs doesn't sound like a good idea.
Ed

User avatar
Rava
Contributor
Contributor
Posts: 2252
Joined: 11 Jan 2011, 02:46
Distribution: Porteus 4.0 x86-64 XFCe
Location: Forests of Germany

Re: [HOWTO] detach a program from its (BASH) shell

Post#5 by Rava » 26 Jan 2019, 06:53

Have you ever used a swap partition with Porteus? Have you ever used Swap via /dev/zram0 on Porteus?
Cheers!
Yours Rava

User avatar
Ed_P
Contributor
Contributor
Posts: 5136
Joined: 06 Feb 2013, 22:12
Distribution: 4.0 Cinnamon 64-bit ISO
Location: Western NY, USA

Re: [HOWTO] detach a program from its (BASH) shell

Post#6 by Ed_P » 26 Jan 2019, 16:18

Nope, no swap partition or used Swap. Have never found a need for one.

BTW While I have Porteus installed on a couple of flash drives my primary executions are via ISOs on my notebook's hdd and are selected when I boot via grub2 menus.

Code: Select all

menuentry " Porteus 4.0 ISO - AF" --class slackware   --class icon-porteus  {

     set boot_parms="volume=33 reboot=cold $extramods "

     search -f $iso --set=root
     loopback loop $iso
     linux  (loop)/boot/syslinux/vmlinuz from=$iso $boot_parms
     initrd (loop)/boot/syslinux/initrd.xz
     }

menuentry " Porteus 4.0 ISO - AF+" --class slackware   --class icon-porteus  {

#     set boot_parms="volume=33 reboot=cold noload=mychanges $extramods$linux_folder/Modules"
     set boot_parms="volume=33 reboot=cold $extramods$linux_folder/Modules noload=cinnamon"

     search -f $iso --set=root
     loopback loop $iso
     linux  (loop)/boot/syslinux/vmlinuz from=$iso $boot_parms
     initrd (loop)/boot/syslinux/initrd.xz
     }
Ed

User avatar
Rava
Contributor
Contributor
Posts: 2252
Joined: 11 Jan 2011, 02:46
Distribution: Porteus 4.0 x86-64 XFCe
Location: Forests of Germany

Re: [HOWTO] detach a program from its (BASH) shell

Post#7 by Rava » 26 Jan 2019, 17:42

Ed_P wrote:
26 Jan 2019, 16:18
Nope, no swap partition or used Swap. Have never found a need for one.
Then you are in luck with your systems having enough physical RAM…
Cheers!
Yours Rava

User avatar
Ed_P
Contributor
Contributor
Posts: 5136
Joined: 06 Feb 2013, 22:12
Distribution: 4.0 Cinnamon 64-bit ISO
Location: Western NY, USA

Re: [HOWTO] detach a program from its (BASH) shell

Post#8 by Ed_P » 27 Jan 2019, 02:42

Newer machines have 2GB and up, older machine has 1 GB. But I rarely have more than 4 windows open.
Ed

Post Reply