Artix Linux => Installation / Migration / Configuration => Topic started by: TheYellowArchitect on 04 September 2021, 22:32:20
Title: [SOLVED] How to start new process, independent of current?
Post by: TheYellowArchitect on 04 September 2021, 22:32:20
I am using a file manager, currently `lf` but doesn't matter. I want to create more instances using some arguments from current file manager or terminal. If I type `pwd | st`, it opens a new terminal (`st` is my terminal, idk what it stands for), but when I close the terminal where I wrote this, it closes the child process as well (`st`). Is there a simple way to make it independent? As if I popped a new terminal via keyboard shortcut? E.g. if I open `st` via dmenu, I can open any amount without being linked.
This question is more of linux practice/workflow question, nothing specific, and honestly its not a dealbreaker
Title: Re: How to start new process, independent of current?
Post by: Krellnus on 04 September 2021, 23:33:13
So on Linux when you use a process to spawn another process, that new process is owned by the older one.
When you kill a process, you are actually instructing the process to also kill anything that it spawned.
This makes sense intuitively right? If I start a program via my terminal and it freezes, then I might actually kill the terminal, to kill that terminal.
Now as you have rightly pointed out, there are times when we actually don't want this behaviour to occur.
When this is the case there are two ways to go about it.
The simplest (and for your case probably what you're looking for) is the use of the
command. This tells your current shell to well, disown its child processes. This is as easy as entering the command, however it requires you to have run anything you want to disown asynchronously (in the background) using & at the end of the command.
The other way would be to look into terminal multiplexers such as Tmux and GNU Screen, but for your example they are very much overkill.
They are very useful programs, especially on remote servers, so I'll leave researching them as an exercise to the reader.
Title: Re: How to start new process, independent of current?
Post by: strajder on 05 September 2021, 11:53:53
If I type `pwd | st`, it opens a new terminal (`st` is my terminal, idk what it stands for), but when I close the terminal where I wrote this, it closes the child process as well (`st`). Is there a simple way to make it independent? As if I popped a new terminal via keyboard shortcut? E.g. if I open `st` via dmenu, I can open any amount without being linked.
This question is more of linux practice/workflow question, nothing specific, and honestly its not a dealbreaker
1. Why would you pipe the output of pwd(1) (Print Working Directory) into a terminal emulator? It doesn't make any sense. st doesnt read stdin from a pipe. Maybe workingdir patch (https://st.suckless.org/patches/workingdir/) is what you are looking for.
2. How can you not know what does st stand for, and you are using it (implying downloading and compiling it to install it)? It's right there on its webpage: https://st.suckless.org/
Edit: To be more precise, disown is mentioned, but only in one place, in the section Command Search and Excecution (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_01_01), and with the context that
Quote
If the command name matches the name of a utility listed in the following table, the results are unspecified.
I have additionally tested ksh93, and it has the disown builtin, while mksh and dash do not.
Title: Re: How to start new process, independent of current?
Post by: TheYellowArchitect on 08 September 2021, 21:02:25
Quote
The simplest (and for your case probably what you're looking for) is the use of the `disown` command This tells your current shell to well, disown its child processes. This is as easy as entering the command, however it requires you to have run anything you want to disown asynchronously (in the background) using & at the end of the command.
This doesn't work, e.g. `st disown &` or `lf disown &` though it is a good idea. Did I use the command wrong?
Quote
1. Why would you pipe the output of pwd(1) (Print Working Directory) into a terminal emulator? It doesn't make any sense.
True, it was an example. But ultimately, the most common use of my question, is that I want to mirror my process, e.g. while being in a terminal to pop another terminal in the same file location (`st`) but when I close one, the other also closes lol
Also, same for `lf`, I want to open another one in its place, and the only way I know is `!lf` but if I close one, ggwp.
Quote
3. man 1 nohup
It opens another process, and it does become independent, but current process becomes uninteractive. At least, this is from my usage of `nohup st` or `nohup lf` Edit: `nohup lf &` ggwp, bless the & symbol, I wouldn't think of it if not mentioned above, thank you both <3 Edit2: This is bizzare. The right above command doesn't work. I tested it once to confirm it worked and to type above, but I'm pretty sure it worked back then, maybe I pressed st instead lol. Anyway, `nohup st &` works at least
Title: Re: How to start new process, independent of current?
Post by: Kyv on 08 September 2021, 21:56:19
Prepend command using setsid, `man setsid`. Might want to add -f option. Process can then be put to background ('&') , and/or output sent to >/dev/null 2>&1 if needed.
Title: Re: How to start new process, independent of current?
Post by: ####### on 08 September 2021, 23:41:42
To really completely background something it should be double forked, but I'm not sure the best way to do that in a shell script, and it may be more than you need to do anyway. https://stackoverflow.com/questions/3430330/best-way-to-make-a-shell-script-daemon (https://stackoverflow.com/questions/3430330/best-way-to-make-a-shell-script-daemon) "daemon" builds OK from the AUR and works simply, just using daemon <command> seemed to work. https://aur.archlinux.org/packages/daemon/ (https://aur.archlinux.org/packages/daemon/)
Title: Re: How to start new process, independent of current?
Post by: Krellnus on 09 September 2021, 01:09:55
Title: Re: How to start new process, independent of current?
Post by: TheYellowArchitect on 09 September 2021, 17:18:20
Thanks you very much for the help, aside of learning new things, I got 3 ways to pretty much mirror the terminal, which is pretty much what I wanted. Before this thread, I literally opened a new terminal and had to browse manually to the same terminal location... So, not having to do this every time I want to open another file at the same time (very frequent) will save me tons of time. Thank you!
Title: Re: [SOLVED] How to start new process, independent of current?
Post by: ####### on 09 September 2021, 20:53:18
I often do this kind of thing too, for file managers in the current directory, but for terminals, such as xfce4 terminal, mate terminal, terminator, gnome terminal, I use tabs - just hit SHIFT_CTRL_T or use the right click menu, and a new tab is opened in the current directory. It can also be "torn off" to make a new terminal window or slid along to re-order on the tab bar. SHIFT_CTRL_W to close quickly. Not all terminals / window managers may support all this, also you can use terminals from other desktops if you want, it's generally better to use GTK or QT to match the desktop for less deps, but that's not essential.
Title: Re: [SOLVED] How to start new process, independent of current?
Post by: strajder on 09 September 2021, 21:51:21
I often do this kind of thing too, for file managers in the current directory, but for terminals, such as xfce4 terminal, mate terminal, terminator, gnome terminal, I use tabs - just hit SHIFT_CTRL_T or use the right click menu, and a new tab is opened in the current directory. It can also be "torn off" to make a new terminal window or slid along to re-order on the tab bar. SHIFT_CTRL_W to close quickly. Not all terminals / window managers may support all this, also you can use terminals from other desktops if you want, it's generally better to use GTK or QT to match the desktop for less deps, but that's not essential.
Or launch a new st instance (https://git.suckless.org/dwm/file/config.def.h.html#l66) in dwm with Shift+Mod+Enter.