Skip to main content
Topic solved
This topic has been marked as solved and requires no further attention.
Topic: [SOLVED] How to start new process, independent of current? (Read 1196 times) previous topic - next topic
0 Members and 6 Guests are viewing this topic.

[SOLVED] How to start new process, independent of current?

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

Re: How to start new process, independent of current?

Reply #1
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

Code: [Select]
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.

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.

Re: How to start new process, independent of current?

Reply #2
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 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/

3.
Code: [Select]
$ man 1 nohup

disown is a Bash builtin. Those that use other shells (I'm using mksh for example) don't have access to it.

Re: How to start new process, independent of current?

Reply #3
disown is a Bash builtin. Those that use other shells (I'm using mksh for example) don't have access to it.

Really? I wasn't aware of that, since it's also available in zsh (which is what I use). Thanks for the heads up.

Re: How to start new process, independent of current?

Reply #4
Really? I wasn't aware of that, since it's also available in zsh (which is what I use). Thanks for the heads up.
Well, at the very least it shouldn't be expected to be found in every shell. It isn't a part of POSIX.

You can check that it is a builtin (or not supported) by issuing the command
Code: [Select]
$ type disown
in your shell.

Edit: To be more precise, disown is mentioned, but only in one place, in the section Command Search and Excecution, 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.

Re: How to start new process, independent of current?

Reply #5
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

Re: How to start new process, independent of current?

Reply #6
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.

Re: How to start new process, independent of current?

Reply #7
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
"daemon" builds OK from the AUR and works simply, just using daemon <command> seemed to work.
https://aur.archlinux.org/packages/daemon/


Re: How to start new process, independent of current?

Reply #9
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!

Re: [SOLVED] How to start new process, independent of current?

Reply #10
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.

Re: [SOLVED] How to start new process, independent of current?

Reply #11
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 in dwm with Shift+Mod+Enter.