Back to basics: Run a command in the background

Using applications, configuring, problems
Post Reply
Message
Author
User avatar
tallboy
Posts: 1760
Joined: Tue 21 Sep 2010, 21:56
Location: Drøbak, Norway

Back to basics: Run a command in the background

#1 Post by tallboy »

I am sure that most of you have experienced the same as I often have done: You start a command/application/program from a terminal window (usually running Bash), but you want to use other commands as well later, so you start the program in the background. It's very simple, using the ampersand:

Code: Select all

# geany &
The problem comes when you are finished with the other commands in the terminal window, and automatically closes it. Bummer! You also just closed the letter to grandma in geany! It was only backgrounded in the shell process running in the terminal window, and closes when the terminal window closes.
There are simple ways to overcome the problem, using built-in commands.
1) disown
It signals that it want to be free, not owned by another process! It is a bash command that removes the program from the terminal shell's table of active jobs. Now you can close the terminal window, and the command/application/program keep running.
2) nohup
This coreutils command let you bypass the Hang Up signal, which is sent when the terminal window closes. (stoneage analog telephone handset modem lingo). As you see in the copies of terminal windows below, it is still running as a job under the shell in the terminal window, but will stay open when the terminal shell send the SIGHUP signal to it's jobs.

Sometime, a program which is background with &, can print a message to the terminal window, which you just have started using for other commands, very annoying! A program backgrounded with nohup, can maybe write a message to a nohup.out file that it creates. Both these cases can also easily be avoided, by redirecting output to /dev/null, which is a bottomless hole.

Code: Select all

# geany &>/dev/null &
or

Code: Select all

#nohup geany &</dev/null &
So, problems solved? Well, almost...
My urxvt terminal window in Dpup Stretch-7.5 has tabs, so I can just open a new tab for other commands, and then close each tab separately with Ctrl-D. When only one tab is left, Ctrl-D closes urxvt. The funny thing is, that when I run for example

Code: Select all

geany &
and closes the urxvt with the close icon on the right side of the window title line, both urxvt and geany close, but when I close the urxvt with Ctrl-D, geany keep running, but now as a process under /bin/busybox. The same happens in LXTerminal window.
I need input from some expert who can explain that one...

If you run hTop while performing some tests, set Display options to Tree view to see where your program is located.
Attachments
geany-1.jpg
(15.52 KiB) Downloaded 210 times
This only redirect messages.jpg
(15.25 KiB) Downloaded 208 times
nohup-geany-redirect.jpg
(16.61 KiB) Downloaded 215 times
True freedom is a live Puppy on a multisession CD/DVD.

s243a
Posts: 2580
Joined: Tue 02 Sep 2014, 04:48
Contact:

#2 Post by s243a »

Try something like:

Code: Select all

nohup geany &
Nohup will daemonize geany. This means that it will continue to run even when the xserver shuts down. So applications have a command line option to deamonize the process. You can also look at some of the stat-up scripts in /etc/init.d to see how they daemonize a given process.
Find me on [url=https://www.minds.com/ns_tidder]minds[/url] and on [url=https://www.pearltrees.com/s243a/puppy-linux/id12399810]pearltrees[/url].

User avatar
rufwoof
Posts: 3690
Joined: Mon 24 Feb 2014, 17:47

#3 Post by rufwoof »

Broadly Ctrl-D is a End of Transmission type signal, different to Ctrl-C which initiates a interrupt/terminate signal. All depends however on how things are set up as they can for example be changed with stty. Looks like in your case Ctrl-D is keeping its child (backgrounded) processes still alive.
[size=75]( ͡° ͜ʖ ͡°) :wq[/size]
[url=http://murga-linux.com/puppy/viewtopic.php?p=1028256#1028256][size=75]Fatdog multi-session usb[/url][/size]
[size=75][url=https://hashbang.sh]echo url|sed -e 's/^/(c/' -e 's/$/ hashbang.sh)/'|sh[/url][/size]

User avatar
tallboy
Posts: 1760
Joined: Tue 21 Sep 2010, 21:56
Location: Drøbak, Norway

#4 Post by tallboy »

s243a, without redirection of output, nohup makes a file nohup.out even if there are no messages.
rufwoof wrote:Looks like in your case Ctrl-D is keeping its child (backgrounded) processes still alive.
Annoying that terminal windows don't follow a default setup on such basic settings. I don't mean that they should have fixed settings, but they should all start out with a default setting, and only let the user modify.
Attachments
nohup-geany.jpg
(21.34 KiB) Downloaded 138 times
after-starting-geany-with-nohup.jpg
(45.49 KiB) Downloaded 142 times
after-closing-urxvt-with-Ctrl-d.jpg
(25.21 KiB) Downloaded 141 times
True freedom is a live Puppy on a multisession CD/DVD.

User avatar
rufwoof
Posts: 3690
Joined: Mon 24 Feb 2014, 17:47

#5 Post by rufwoof »

That looks normal to me. If the parent of a nohup backgrounded task dies, then it has to reattach to a new parent process somewhere. If that was not the case and was instead just left hanging (orphaned) then you'd have to manually reattach it somewhere, such as running reptyr to reattach it to the current terminal.
[size=75]( ͡° ͜ʖ ͡°) :wq[/size]
[url=http://murga-linux.com/puppy/viewtopic.php?p=1028256#1028256][size=75]Fatdog multi-session usb[/url][/size]
[size=75][url=https://hashbang.sh]echo url|sed -e 's/^/(c/' -e 's/$/ hashbang.sh)/'|sh[/url][/size]

User avatar
tallboy
Posts: 1760
Joined: Tue 21 Sep 2010, 21:56
Location: Drøbak, Norway

#6 Post by tallboy »

Yes, the object was mainly to point out that redirecting messages to /dev/null will prevent an (unwanted) extra file to appear.

One can also combine the two first commands to start a geany in the background, then you can colose the terminal window while geany is still running.
Attachments
disowwn-geany.jpg
(9.69 KiB) Downloaded 70 times
True freedom is a live Puppy on a multisession CD/DVD.

Post Reply