0

Let's say I have a command and I want to run it in a specific bash, whose pid is known to me.

Like for example pid of a bash is 100. I want to run a command like ls in this bash with pid = 100, where as I am executing the automation script in another bash with pid = 101.

Is this even possible?

Sai Nikhil
  • 1,237
  • 2
  • 15
  • 39
  • 1
    Why do you want to run a process with a different parent (no, you can't)? What are you trying to achieve? And what has this to do with `python`? – cdarke Feb 19 '18 at 16:02
  • I believe your issue is solved here: https://stackoverflow.com/questions/5998126/send-command-to-a-background-process – Hendrik Evert Feb 19 '18 at 16:03
  • @cdarke: Here's the requirement. Our source control ade get's executed in a new bash with a different pid. I want to write an automation script that will run few other commands after entering into the source control bash. Is this possible? – Sai Nikhil Feb 20 '18 at 07:36
  • It depends on the type of `bash` session and what you mean by *after*. There are some startup files: `.bashrc` is executed when a new *interactive* `bash` shell enters, a *non-interactive* shell will execute a file specified in the variable `BASH_ENV`, which you should `export` for your ade to see it. You will have to experiment to see whether the shell is interactive or non-interactive. – cdarke Feb 20 '18 at 08:02
  • Let us say my commands are as follows: 1) ade useview view_name 2) ade pwv Executing command one will open it's own bash. If I put the above commands in one .sh file and try executing it, command 2 is run in the shell where I execute the script but not inside the shell created by command 1. How to run command 2 inside the shell created by command 1? – Sai Nikhil Feb 20 '18 at 10:34

1 Answers1

0

Of course, there are some hacks that allow you to do things that look a bit like it (pipes, expect etc.). But the main answer is: no.

A simplistic view of what happens normally: When you start a process like ls, the shell does a fork and exec. That means that the new process is a child of the current process. The child will execute with it's own PID. As an example:

pstree -p $$
bash(7695)─┬─pstree(13922)
           └─sleep(13899)

Here you see my shell (PID=7695) and a sleep that is executed by the shell. You see that the PID is different. It always is. It can never have the same PID as another process.

So, the next question is whether you can make another process (your PID=100 shell) fork&exec an ls as a child (so with another PID). The answer is normally no, unless you make some provisions for that (like using a fifo and executing lines that are dumped in the fifo. Executing everything that is dumped in a fifo has some security, eh, lets call them challenges.

My advice: don't go there. Re-evaluate the reasons for doing this.

Ljm Dullaart
  • 4,273
  • 2
  • 14
  • 31