2

Given an interactive bash process and a process which wasn't started from the bash process, is there some way (program) which can make the process a job of the shell?

This may sound impossible, but may be a useful feature. Sometimes I accidentally kill a bash process, and some of its jobs are reparented to the init process. I would like to make them jobs of another bash process for better managing them.

Thanks.

Tim
  • 101,790

1 Answers1

1

I was taught in school that this was impossible. That's apparently no longer true, but it's fairly involved to get around it for a single process.

The two links given to you in comments explain what easy options you have to accomplish what you are wanting to accomplish. However, they either require you to take action before you start the command you want to move

https://unix.stackexchange.com/a/8696/329452 https://unix.stackexchange.com/a/49155/329452

or they only tell you how to change the tty the process is using (so you can move the input and output, but it's not going to be the child of the active process)

https://unix.stackexchange.com/a/4035/329452

There's a lot more answers on those posts, but they're basically duplicates of these.


In order to change the parent of a process, its parent has to die. Back in the day, that would always change the parent to PID 1. I'm not sure when this changed, but since Linux 3.4, the process that acquires all of the orphans can be changed: https://unix.stackexchange.com/a/177361/5132. I'm still looking over the results of that. I would have thought that significant of a change would have enabled systemd to not be nearly so monolithic - but apparently, systemd is monolithic for its own sake. (roughly 90% of the way down on that page)

That having been said, the process that takes on this role has to take on the entire role. It's not just something to do for one process.

Most unix shells do not have any code in them to allow them to recognize a new child that was just grafted in. Some of them are more amenable than others to getting notified of such children ending. I'd expect adding those processes to job control would not be a common shell feature. I think zsh's modules would be able to add support to the shell if that was desired. I haven't actually looked for zsh modules that aren't part of the zsh package that do this yet, nor have I looked for other shells that can handle it yet.

Ed Grimm
  • 671