LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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, 04:54 AM   #1
Apprentice+
Member
 
Registered: May 2021
Distribution: Slackware64-Current
Posts: 43

Rep: Reputation: Disabled
Question How do I run a program from the terminal? Advanced question.


Good morning everyone!

The default when we run a program from the terminal is that the PID of the program is the child of the terminal, i.e. we have 2 programs running!

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?
 
Old 01-05-2023, 05:10 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,989

Rep: Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337
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
 
2 members found this post helpful.
Old 01-05-2023, 05:46 AM   #3
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
Quote:
Originally Posted by pan64 View Post
From the other hand you can detach the child process from the terminal (and parent) using &:
No. It remains attached
Quote:
In this case you will get your prompt (and shell) back and also the program will run in the background.
Right

You must use command like nohup or screen to detach a program from the terminal where it was launched
 
1 members found this post helpful.
Old 01-05-2023, 06:30 AM   #4
rizitis
Member
 
Registered: Mar 2009
Location: Greece,Crete
Distribution: Slackware64-current, Slint
Posts: 677
Blog Entries: 1

Rep: Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511
Quote:
Originally Posted by pan64 View Post
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.

Is there a possibility the running shell in the terminal to not be child of the terminal?
Or terminal not be the parent of that running shell?
Thats something I will like to learn... thank you.

Last edited by rizitis; 01-05-2023 at 06:31 AM.
 
Old 01-05-2023, 07:19 AM   #5
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Maybe search for informations about programming a daemon (although running a shell with no input and output is questionable)
 
Old 01-05-2023, 10:22 AM   #6
chemfire
Member
 
Registered: Sep 2012
Posts: 426

Rep: Reputation: Disabled
I am not sure I fully understand what you are trying to do.

All processes have a parent. (well except init or you can think of its parent as the kernel). To detach from the controlling terminal and by extension the terminal emulator that hosting it, in the case of xterm and similar a process can become a daemon. This essentially re-parents it to pid 1 - init. In this case the the chains of terminals emulators/shells/etc that eventually created that process can terminate without daemon terminating. The simplest thing as marv mentioned is probably using screen. The recipe there is -

screen -S 'Friendly Name for your Session' -dm -s /path/to/your/application

You can even implement a daemon in common scripting languages like python and ruby - actually both make it fairly simple to do. Similarly both offer the ability to create virtual PTYs in their standard library where you run another process and essentially have io objects you can read and write for stdin/stdout/stderr. You could write a script to create PTY, spawn your process do what you need to do with the standard FDs and demonize, if you don't want to use screen for some specific reason.
 
Old 01-05-2023, 10:25 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,989

Rep: Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337
Quote:
Originally Posted by rizitis View Post
Is there a possibility the running shell in the terminal to not be child of the terminal?
Or terminal not be the parent of that running shell?
Thats something I will like to learn... thank you.
The definition (or meaning) is quite simple, the process which starts another process is the parent, and the new one is the child. In the linux world every process has a parent process (obviously the one which started it). There is only one exception, the very first process has no parent. But if you really want to learn just look for a correct description, what I wrote here is just the first step to start with.
The terminal emulators by default start a program to work with (inside the terminal), it can be a shell or something else, but it is always started by the terminal and also if the program exits the terminal itself will exit too. But again you need to learn about it, this is just an overview.
 
2 members found this post helpful.
Old 01-05-2023, 10:26 AM   #8
Thom1b
Member
 
Registered: Mar 2010
Location: France
Distribution: Slackware
Posts: 486

Rep: Reputation: 339Reputation: 339Reputation: 339Reputation: 339
Hi,

Did you try this?
Code:
program & disown
 
Old 01-05-2023, 10:54 AM   #9
chemfire
Member
 
Registered: Sep 2012
Posts: 426

Rep: Reputation: Disabled
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.
 
Old 01-05-2023, 11:00 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,989

Rep: Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337
Quote:
Originally Posted by chemfire View Post
disown probably won't solve the problem.
Actually I don't know what should be solved here.
 
Old 01-05-2023, 01:55 PM   #11
qunying
Member
 
Registered: Jun 2002
Distribution: Slackware
Posts: 258

Rep: Reputation: 147Reputation: 147
Quote:
Originally Posted by chemfire View Post
Another choice might be the nohump.
I believe it is just a typo, the command should be
Code:
nohup program
 
Old 01-05-2023, 02:34 PM   #12
henca
Member
 
Registered: Aug 2007
Location: Linköping, Sweden
Distribution: Slackware
Posts: 994

Rep: Reputation: 675Reputation: 675Reputation: 675Reputation: 675Reputation: 675Reputation: 675
My guess is that the "real" problem not is the fact that the process (bash) which starts another proces (for example xclock) becomes the parent of that process. The problem was probably that the prompt did not return until the child process terminated. The solution, as others have pointed out is to start the child as a background process with "&" after the command.

If you from bash or any other shell (in a terminal or from console) start a process but forget the "&" all hope is not lost. You can get your prompt back by temporary stopping the child process, that is done by pressing "ctrl-z". Now, with you prompt back, you have three options:
  1. Keep the child process stopped
  2. Type "fg" to get the child process in the foreground, stealing the prompt again
  3. Type "bg" to put the child process running in the background, keeping your prompt

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
 
5 members found this post helpful.
Old 01-05-2023, 05:39 PM   #13
enorbet
Senior Member
 
Registered: Jun 2003
Location: Virginia
Distribution: Slackware = Main OpSys
Posts: 4,799

Rep: Reputation: 4437Reputation: 4437Reputation: 4437Reputation: 4437Reputation: 4437Reputation: 4437Reputation: 4437Reputation: 4437Reputation: 4437Reputation: 4437Reputation: 4437
Interesting. I have a handful of bash scripts and apps in "~/.config/autostart" and I'm pretty sure none of them retain the child/parent relationship. That said though I did notice that recently with upgrade to 15.0 something auto adds "--damonize" to "/usr/bin/conky" when I add it into KDE's system-settings Autostart. It doesn't alter scripts though.
 
Old 01-05-2023, 05:54 PM   #14
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
pstree is useful to see relationships
Code:
pstree -s -p $pid_of_program
 
3 members found this post helpful.
Old 01-05-2023, 06:05 PM   #15
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
Quote:
Originally Posted by enorbet View Post
Interesting. I have a handful of bash scripts and apps in "~/.config/autostart" and I'm pretty sure none of them retain the child/parent relationship. That said though I did notice that recently with upgrade to 15.0 something auto adds "--damonize" to "/usr/bin/conky" when I add it into KDE's system-settings Autostart. It doesn't alter scripts though.
It's the default .desktop in the sources
Code:
system/conky/conky-1.15.0 $ cat conky.desktop 
[Desktop Entry]
Type=Application
Name=conky
Exec=conky --daemonize --pause=1
StartupNotify=false
Terminal=false
Icon=conky-logomark-violet
Categories=System;Monitor;
 
  


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 07:22 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