S
S
synapse_people2019-04-17 12:35:06
linux
synapse_people, 2019-04-17 12:35:06

How to implement processes that depend on each other?

You need to start 1 main process that controls many child processes.
It starts each subprocess in the form of bash -c #CMD#&, it turns out they work as tasks in the current session. So, #CMD# crashes if you terminate the main process (logically), but doesn't crash if you kill bash.
Question: is it possible to make the bash also have its own session (or how?), so that when the main process OR bash is killed, CMD falls after them?
The hierarchy is as follows: MAINPROC -> BASH -> CMD
so if you kill MAINPROC - BASH should fall, followed by CMD (logically) - that's what happens, they all go down.
But if you kill BASH, then MAIN+CMD still work!
I tried setsid, then if you kill BASH + MAINPROC - CMD continues to work for itself and that's it, without them in a new session ....
How can I make this behavior ?
More like now:

7434 pts/0    Ss   00:00:00 bash
16491 pts/0    S    00:00:00  \_ bash
18549 pts/0    S+   00:00:00      \_ php ./...php
18553 pts/0    S+   00:00:00 /bin/bash /tmp/CMDt8sL0c
18559 pts/0    Sl+  00:00:18  \_ /CMD

[email protected]:/srv/$ kill 18553

[email protected]:/srv/$ ps --forest -o pid,tty,stat,time,cmd -g 
  PID TT       STAT     TIME CMD
16726 pts/1    Ss   00:00:00 bash
18763 pts/1    R+   00:00:00  \_ ps --forest -o pid,tty,stat,time,cmd -g
 7434 pts/0    Ss   00:00:00 bash
16491 pts/0    S    00:00:00  \_ bash
18549 pts/0    S+   00:00:00      \_ php ./...php
18559 pts/0    Sl+  00:00:20 /CMD

But CMD did not fall, it continues to work for itself!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Saboteur, 2019-04-17
@saboteur_kiev

Logically, background processes should not fall if the bash is killed.
That's what your & at the end of the line says: bash -c #CMD#&
Processes can exit if a specific signal is sent to them. In a process group, a signal sent to the master process is also sent to all child processes.
But in your case, in the link: MAIN -> BASH -> CMD, you yourself untie the child CMD process from the BASH process with the & command
. At the same time, MAIN is still the leader of the entire process group, so the termination of MAIN affects all children.

Q
q2zoff, 2019-04-17
@q27off

Question: is it possible to make the bash also have its own session (or how?), so that when the main process OR bash is killed, CMD falls after them?
The problem is that you are using "crutches" to create child processes. Use the fork system call directly in your code rather than delegate it to bash.
In general, pay attention to the setpgid and setpgrp siskols. You can put BASH in a separate group, and then shutting down both MAIN and BASH (using a signal) will cause CMD to exit. In the first case, all child processes will be terminated, since the MAIN session leader will terminate. In the second case, BASH and CMD will be terminated, since are members of the same group other than the MAIN group. I hope I made my thoughts clear.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question