LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 01-05-2023, 11:31 PM   #16
drgibbon
Senior Member
 
Registered: Nov 2014
Distribution: Slackware64 15.0
Posts: 1,221

Rep: Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943

Quote:
Originally Posted by Apprentice+ View Post
Is it possible to run a program on the terminal, freeing the terminal and the PID of this program is not a child of the terminal?
I'm not sure exactly what you're after, but in zsh there are a couple of ways to free the terminal after starting a program; "somecommand &" or "somecommand &!". With just "&" appended, the program is started in the background, but zsh gives you the job number and you can still do job control stuff with it. With "&!" appended, the program is disowned and not subject to job control. In either case, killing the shell will not kill the process that was started (I tested this by running "kwrite &" / "kwrite &!" in a terminal emulator and then killing the term).

The equivalent commands with bash seem to be "sommecommand &", which will background the program but still retain it in the job list, or "somecommand & disown" which seems to have the same effect as "&!" in zsh. The only difference I can see is with just "&" in bash, killing the starting shell does also kill the process that you started (which doesn't happen when you use disown).
 
Old 01-06-2023, 02:21 AM   #17
ozanbaba
Member
 
Registered: May 2003
Location: İzmir
Distribution: Slackware64 15.0 Multilib
Posts: 778

Rep: Reputation: 135Reputation: 135
How about "exec"? It causes the process to replace the given command. It's something you can see in rc scripts for example.

Quote:
Replace the shell with the given command.

Execute COMMAND, replacing this shell with the specified program.
ARGUMENTS become the arguments to COMMAND. If COMMAND is not specified,
any redirections take effect in the current shell.

Options:
-a name pass NAME as the zeroth argument to COMMAND
-c execute COMMAND with an empty environment
-l place a dash in the zeroth argument to COMMAND

If the command cannot be executed, a non-interactive shell exits, unless
the shell option `execfail' is set.

Exit Status:
Returns success unless COMMAND is not found or a redirection error occurs.
 
Old 01-06-2023, 05:25 AM   #18
Ilgar
Senior Member
 
Registered: Jan 2005
Location: Istanbul, Turkey
Distribution: Slackware64 15.0, Slackwarearm 14.2
Posts: 1,157

Rep: Reputation: 237Reputation: 237Reputation: 237
Quote:
Originally Posted by henca View Post
However, if the problem really should be that the new process becomes a child of the parent process, there is a trick to avoid that. That trick is to start an extra process, then the process you don't want as your own child and finally kill that extra process which is the parent of the child process. When the parent dies the child process becomes orphaned. All orphaned processes are adopted by the init process (pid 1). Example:

Code:
$ bash
$ xclock &
$ exit
$ ps -ef | grep xclock
regards Henrik
One can also achieve this using
Code:
bash -c 'xclock &'
 
1 members found this post helpful.
Old 01-06-2023, 06:35 AM   #19
marav
LQ Sage
 
Registered: Sep 2018
Location: Gironde
Distribution: Slackware
Posts: 5,409

Rep: Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145
After 24h of debating on this subject
The real question is : where is the OP ?

And I agree with pan64
Quote:
Originally Posted by pan64 View Post
Actually I don't know what should be solved here.
 
Old 01-06-2023, 06:47 AM   #20
henca
Member
 
Registered: Aug 2007
Location: Linköping, Sweden
Distribution: Slackware
Posts: 994

Rep: Reputation: 675Reputation: 675Reputation: 675Reputation: 675Reputation: 675Reputation: 675
Quote:
Originally Posted by drgibbon View Post
The only difference I can see is with just "&" in bash, killing the starting shell does also kill the process that you started
For some reason I do not see that difference on my system, maybe my bash has been configured differently.

What you are seeing is that some bourne-like shells when they die notifies their child processes that their parent is dying by sending a HUP signal. It is possible for a program to catch the HUP signal and do something else, but the default when a HUP signal is received is to die. If you have a bourne-like shell which sends HUP to its children when it dies and you want to let a background children keep living on you can start it with nohup:

Code:
nohup xclock &
As this thread is marked with "Advanced question" I might just as well continue to write some more about those signals used between processes. You might want to stop reading here unless you want to read a long horrific story about daemons and zombies...

As mentioned, the HUP signal is used to notify a process that its parent has died an that it should HangUP. The main reason for such a signal might be that the program has lost its stdout. However, there is also another common use for the HUP signal. Daemons providing different kinds of services like a mail server or a web server usually have the init process (pid 1) as their parent. In such a case you can be sure that the parent is not going to die before you and it is a rather common practice that those processes instead use the HUP signal to tell the process to reread its configuration files. In Slackware, you can see one such example in /etc/rc.d/rc.autofs where the HUP signal is used to tell the automounter daemon to reload its maps.

There are a number of signals, some, but not all signals are possible to catch and do whatever you like with. The INT (interrupt) signal is an example. A program will get that signal if someone presses ctrl-c in the terminal. By default, the the program will die when ctrl-c is pressed, but it is possible to write a program that does not die from the INT signal and instead chooses to do something else. However, it is not possible to catch the KILL signal, all processes has to die from the KILL signal.

If you want to read more about which signals there are you can read a man page:

Code:
man 7 signal
Please note the 7 above, without any number:

Code:
man signal
You will instead get the lowest numbered signal man page

Code:
man 2 signal
which describes how to write programs that catches signals.

So what about zombie processes? In Unix/Linux, a parent process has the right to know when a child process has died. The parent gets notified with a CHLD signal. However, with this right also comes a responsibility. The parent must make a wait call for the child to die. Unless that is done the child process will become a living dead zombie process.

Some parent processes think that they have more important things to do than waiting for dying child processes. If you write such a program, the responsible way to avoid waiting for dying child processes is to have them orphaned by forking an extra temporary process before the child process is started. When the extra process that started the child process exits, the child process becomes orphaned and gets adopted by the init process. The init process will let any dying process die in piece and zombie processes are avoided.

If you get a system with zombie processes you might note that it isn't possible to kill a zombie, it is already dead. The only way to get rid of those zombie processes is to kill the parents of the zombie processes.

regards Henrik
 
1 members found this post helpful.
Old 01-06-2023, 06:51 AM   #21
henca
Member
 
Registered: Aug 2007
Location: Linköping, Sweden
Distribution: Slackware
Posts: 994

Rep: Reputation: 675Reputation: 675Reputation: 675Reputation: 675Reputation: 675Reputation: 675
Quote:
Originally Posted by marav View Post
After 24h of debating on this subject
The real question is : where is the OP ?
There are people that do other things than spending all their time on this forum. In many parts of the world it is now holiday seasons. Maybe he wrote the question from work before going for a long weekend?

regards Henrik
 
1 members found this post helpful.
Old 01-06-2023, 06:00 PM   #22
notzed
Member
 
Registered: Dec 2020
Location: South Australia
Distribution: slackware64-current
Posts: 95

Rep: Reputation: Disabled
The question makes no sense as all terminals must have at least one child - this the one they're managing the input/output from. So for a terminal session that child will typically be 'bash' but it could be a specific programme like 'top'. So you can't replace 'agetty' or 'xterm' with 'bash' or anything else as such.

So probably the question is regarding a terminal session.

If you run a command in a session you will have terminal->shell->command. You can use exec to replace 'shell' with 'command', e.g. terminal->command, but that's the last thing you can run in that terminal (unless command has an exec function).

If the last command in a script is long running (e.g. an application or daemon start script) it should use 'exec' otherwise the initiating shell is hanging around doing nothing for no good reason.

You can 'detatch' a process from it's parent by doing a double-fork:

Quote:
/bin/bash -c 'command &' &

or equivalently:

(command &) &
In pstree 'command' will no longer have a parent pid connected to the original terminal or shell.

This will spawn terminal->bash->bash->command, but the second bash will exit leaving command without a parent - the kernel will assign it a parent pid of 1 (init). There's a bit more to it regarding signals and file descriptors but that's the idea.
 
Old 01-06-2023, 09:28 PM   #23
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,246

Rep: Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323
No you can't do that.

Read this:

https://xyproblem.info/

Then read this, which I suspect is what's being asked about:

https://www.tecmint.com/keep-remote-...disconnection/

Last edited by dugan; 01-06-2023 at 09:41 PM.
 
Old 01-07-2023, 04:18 AM   #24
Apprentice+
Member
 
Registered: May 2021
Distribution: Slackware64-Current
Posts: 43

Original Poster
Rep: Reputation: Disabled
Thumbs up

Quote:
Originally Posted by pan64 View Post
yes and no. All the programs started from anywhere will be the child of that initiator process (called parent/and child). There is no exception.
Just a remark: the parent process is not the terminal, but the shell running inside the terminal. The terminal process itself (usually) is the parent of that shell.
From the other hand you can detach the child process from the terminal (and parent) using &:
Code:
program arguments &
In this case you will get your prompt (and shell) back and also the program will run in the background.
https://linuxconfig.org/bash-backgro...ess-management
Thank you, your answer was very helpful. I liked the link too!
 
Old 01-07-2023, 04:21 AM   #25
Apprentice+
Member
 
Registered: May 2021
Distribution: Slackware64-Current
Posts: 43

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by marav View Post
No. It remains attached

Right

You must use command like nohup or screen to detach a program from the terminal where it was launched
In the test I did, when I exit the terminal, the program continues to run, and is no longer bound to the terminal. It is not even a child of the terminal anymore. In other words, I can run more than one program, close the terminal, and the programs will be running perfectly, without the link to the PID that no longer exists in the terminal!
 
Old 01-07-2023, 04:26 AM   #26
Apprentice+
Member
 
Registered: May 2021
Distribution: Slackware64-Current
Posts: 43

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Thom1b View Post
Hi,

Did you try this?
Code:
program & disown
What would be the difference in using only & or & disown
What would change?
When I run it for myself who has no knowledge, I see no difference!
 
Old 01-07-2023, 04:31 AM   #27
Apprentice+
Member
 
Registered: May 2021
Distribution: Slackware64-Current
Posts: 43

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by chemfire View Post
disown probably won't solve the problem. If the terminal is a PTY (xterm and similar) vs an actual con device, when the PTY goes away the process will likely crash as soon as it tries to use stdin/stdout/stderr. Another choice might be the nohump. However that does not provide much control as compared with screen, but certainly is lighter weight.
nohup program
It doesn't work, if I close the terminal, the program closes too
 
Old 01-07-2023, 04:39 AM   #28
Apprentice+
Member
 
Registered: May 2021
Distribution: Slackware64-Current
Posts: 43

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by keefaz View Post
pstree is useful to see relationships
Code:
pstree -s -p $pid_of_program
I like this command! I found it very interesting! Thanks!
 
Old 01-07-2023, 04:56 AM   #29
Apprentice+
Member
 
Registered: May 2021
Distribution: Slackware64-Current
Posts: 43

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by drgibbon View Post
I'm not sure exactly what you're after, but in zsh there are a couple of ways to free the terminal after starting a program; "somecommand &" or "somecommand &!". With just "&" appended, the program is started in the background, but zsh gives you the job number and you can still do job control stuff with it. With "&!" appended, the program is disowned and not subject to job control. In either case, killing the shell will not kill the process that was started (I tested this by running "kwrite &" / "kwrite &!" in a terminal emulator and then killing the term).

The equivalent commands with bash seem to be "sommecommand &", which will background the program but still retain it in the job list, or "somecommand & disown" which seems to have the same effect as "&!" in zsh. The only difference I can see is with just "&" in bash, killing the starting shell does also kill the process that you started (which doesn't happen when you use disown).
In bash it worked fine just using & i.e., terminating bash does not kill the child program!
 
1 members found this post helpful.
Old 01-07-2023, 05:00 AM   #30
Apprentice+
Member
 
Registered: May 2021
Distribution: Slackware64-Current
Posts: 43

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by henca View Post
For some reason I do not see that difference on my system, maybe my bash has been configured differently.

What you are seeing is that some bourne-like shells when they die notifies their child processes that their parent is dying by sending a HUP signal. It is possible for a program to catch the HUP signal and do something else, but the default when a HUP signal is received is to die. If you have a bourne-like shell which sends HUP to its children when it dies and you want to let a background children keep living on you can start it with nohup:

Code:
nohup xclock &
As this thread is marked with "Advanced question" I might just as well continue to write some more about those signals used between processes. You might want to stop reading here unless you want to read a long horrific story about daemons and zombies...

As mentioned, the HUP signal is used to notify a process that its parent has died an that it should HangUP. The main reason for such a signal might be that the program has lost its stdout. However, there is also another common use for the HUP signal. Daemons providing different kinds of services like a mail server or a web server usually have the init process (pid 1) as their parent. In such a case you can be sure that the parent is not going to die before you and it is a rather common practice that those processes instead use the HUP signal to tell the process to reread its configuration files. In Slackware, you can see one such example in /etc/rc.d/rc.autofs where the HUP signal is used to tell the automounter daemon to reload its maps.

There are a number of signals, some, but not all signals are possible to catch and do whatever you like with. The INT (interrupt) signal is an example. A program will get that signal if someone presses ctrl-c in the terminal. By default, the the program will die when ctrl-c is pressed, but it is possible to write a program that does not die from the INT signal and instead chooses to do something else. However, it is not possible to catch the KILL signal, all processes has to die from the KILL signal.

If you want to read more about which signals there are you can read a man page:

Code:
man 7 signal
Please note the 7 above, without any number:

Code:
man signal
You will instead get the lowest numbered signal man page

Code:
man 2 signal
which describes how to write programs that catches signals.

So what about zombie processes? In Unix/Linux, a parent process has the right to know when a child process has died. The parent gets notified with a CHLD signal. However, with this right also comes a responsibility. The parent must make a wait call for the child to die. Unless that is done the child process will become a living dead zombie process.

Some parent processes think that they have more important things to do than waiting for dying child processes. If you write such a program, the responsible way to avoid waiting for dying child processes is to have them orphaned by forking an extra temporary process before the child process is started. When the extra process that started the child process exits, the child process becomes orphaned and gets adopted by the init process. The init process will let any dying process die in piece and zombie processes are avoided.

If you get a system with zombie processes you might note that it isn't possible to kill a zombie, it is already dead. The only way to get rid of those zombie processes is to kill the parents of the zombie processes.

regards Henrik
Thanks for the information!
 
  


Reply

Tags
pid, shell, terminal



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: 179 Color Schemes For Your Gtk-Based Linux Terminal (Gnome Terminal, Tilix, Xfce Terminal, More) LXer Syndicated Linux News 0 07-28-2019 04:50 AM
LXer: Tilix: Advanced Tiling Terminal Emulator for Power Users LXer Syndicated Linux News 0 08-01-2017 12:12 PM
LXer: Tilix – An Advanced GTK3 Tiling Terminal Emulator for Linux LXer Syndicated Linux News 1 05-12-2017 08:14 AM
Accessing linux terminal from advanced desktop agd3103 Linux - Newbie 3 05-22-2009 03:34 AM
Better buying "advanced linux prog" or "unix advanced prog" Dominik Programming 3 12-31-2003 01:11 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 02:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration