Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Tue 12 Nov 2019, 10:41
All times are UTC - 4
 Forum index » House Training » Users ( For the regulars )
Back to basics: Run a command in the background
Moderators: Flash, Ian, JohnMurga
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [6 Posts]  
Author Message
tallboy


Joined: 21 Sep 2010
Posts: 1538
Location: Drøbak, Norway

PostPosted: Sat 15 Jun 2019, 23:45    Post subject:  Back to basics: Run a command in the background  

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:
# 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:
# geany &>/dev/null &

or
Code:
#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:
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.
geany-1.jpg
 Description   
 Filesize   15.52 KB
 Viewed   200 Time(s)

geany-1.jpg

This only redirect messages.jpg
 Description   
 Filesize   15.25 KB
 Viewed   200 Time(s)

This only redirect messages.jpg

nohup-geany-redirect.jpg
 Description   
 Filesize   16.61 KB
 Viewed   202 Time(s)

nohup-geany-redirect.jpg


_________________
True freedom is a live Puppy on a multisession CD/DVD.
Back to top
View user's profile Send private message 
s243a

Joined: 02 Sep 2014
Posts: 2144

PostPosted: Sun 16 Jun 2019, 00:11    Post subject:  

Try something like:
Code:

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 minds and on pearltrees.
Back to top
View user's profile Send private message Visit poster's website 
rufwoof


Joined: 24 Feb 2014
Posts: 3610

PostPosted: Sun 16 Jun 2019, 06:09    Post subject:  

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.
_________________
( ͡° ͜ʖ ͡°) :wq
Fatdog multi-session usb

echo url|sed -e 's/^/(c/' -e 's/$/ hashbang.sh)/'|sh
Back to top
View user's profile Send private message 
tallboy


Joined: 21 Sep 2010
Posts: 1538
Location: Drøbak, Norway

PostPosted: Sun 16 Jun 2019, 19:20    Post subject:  

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.
nohup-geany.jpg
 Description   
 Filesize   21.34 KB
 Viewed   131 Time(s)

nohup-geany.jpg

after-starting-geany-with-nohup.jpg
 Description   
 Filesize   45.49 KB
 Viewed   131 Time(s)

after-starting-geany-with-nohup.jpg

after-closing-urxvt-with-Ctrl-d.jpg
 Description   
 Filesize   25.21 KB
 Viewed   132 Time(s)

after-closing-urxvt-with-Ctrl-d.jpg


_________________
True freedom is a live Puppy on a multisession CD/DVD.
Back to top
View user's profile Send private message 
rufwoof


Joined: 24 Feb 2014
Posts: 3610

PostPosted: Mon 17 Jun 2019, 06:01    Post subject:  

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.
_________________
( ͡° ͜ʖ ͡°) :wq
Fatdog multi-session usb

echo url|sed -e 's/^/(c/' -e 's/$/ hashbang.sh)/'|sh
Back to top
View user's profile Send private message 
tallboy


Joined: 21 Sep 2010
Posts: 1538
Location: Drøbak, Norway

PostPosted: Mon 17 Jun 2019, 18:13    Post subject:  

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.
disowwn-geany.jpg
 Description   
 Filesize   9.69 KB
 Viewed   62 Time(s)

disowwn-geany.jpg


_________________
True freedom is a live Puppy on a multisession CD/DVD.
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [6 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » House Training » Users ( For the regulars )
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0404s ][ Queries: 12 (0.0051s) ][ GZIP on ]