0

I run the command (Xorg & sleep 3; xeyes) & to test Xorg, and group it into a single subshell background job to make management easy. This works properly, and opens xeyes in the new Xorg session after 3 seconds.

Upon running the command, I'll get an output such as the following:

[1] 635

After running ps -ef to check new processes, I'll get an output like the following:

root    635    361    0    4:52    tty1    00:00:00    -bash
root    636    365    0    4:52    tty2    00:00:00    /usr/lib/Xorg
root    639    365    0    4:52    tty1    00:00:00    xeyes

This seems to be a pretty standard output, and as expected. After verifying my X server works as expected, I attempt to kill this group with kill %1. Upon running this, my processes now look as follows:

root    636    1    0    4:52    tty2    00:00:00    /usr/lib/Xorg

Why has Xorg failed to exit? Why has the subshell exited successfully, closing xeyes correctly, but not brought Xorg along with it? Why has the parent process of Xorg now changed to 1 instead of the subshell? Shouldn't the subshell send a kill signal to all of its child processes upon exit?

Additionally, if I kill the group instead with kill 635, which many resources say should be equivalent to kill %1, my process state is even more bizzare:

root    636    1    0    4:52    tty2    00:00:00    /usr/lib/Xorg
root    639    1    0    4:52    tty1    00:00:00    xeyes

What??? Why have both processes failed to exit now, and are now children of PID 1? What's going on here, and what am I doing wrong?

An in-depth explanation of what exactly is going on here would be appreciated, in addition to just telling me what to do instead.

1 Answer 1

1

If you want to observe job control, you'd want to use the -j option to ps which will list the process group id and session id.

Here, I see:

[...]
UID        PID  PPID  PGID   SID  C STIME TTY          TIME CMD
chazelas  6805  4172  6805  6805  0 06:47 pts/7    00:00:00 /bin/zsh
chazelas  6825  6805  6825  6805  0 06:48 pts/7    00:00:00 xeyes
root      6826  6825  6826  6826  0 06:48 tty2     00:00:00 /usr/lib/xorg/Xorg :4
[...]

You see Xorg is a child of xeyes as my shell is a bit more optimised than bash and runs xeyes in the subshell's process as it's the last command of the subshell. And no, a subshell is not going to kill its children when it terminates, that would render the shell unusable (and here it's obvious it couldn't as the subshell has been replaced by xeyes).

The same in bash:

$ ps -Afj
[...]
UID        PID  PPID  PGID   SID  C STIME TTY          TIME CMD
chazelas  7230  6805  7230  6805  0 06:54 pts/7    00:00:00 bash
chazelas  7246  7230  7246  6805  0 06:54 pts/7    00:00:00 bash
root      7247  7246  7247  7247  2 06:54 tty2     00:00:00 /usr/lib/xorg/Xorg :4
chazelas  7274  7246  7246  6805  0 06:54 pts/7    00:00:00 xeyes
[...]

There's an extra useless bash process that just waits for xeyes to terminate and won't do anything afterwards, but otherwise it's the same as in zsh, you see that a new process group has been created by the shell (6825 for zsh, 7246 for bash), but Xorg is not in that process group.

That's not because of the & after Xorg, commands started in a subshell are not started in a new job, that's because Xorg itself starts a whole new session itself (let alone a process group) to attach that tty2 terminal.

So as Xorg has removed itself from the process group, doing that kill %1 won't kill it.

Note that kill %1 sends a SIGTERM signal to the job's process group, not an individual pid. To send a signal to a process group you need:

kill -- -7246

Which in my case above would have killed pids 7246 (bash subshell) and 7274 (xeyes), but not Xorg (7247) as it's not in the 7246 process group.

2
  • Appreciate it! All that you've said makes sense, but how would I go about doing what I'm looking to do (run multiple commands asynchronously in a single job)? Is what I'm looking to do even possible, or logical?
    – quixotrykd
    Jul 26, 2019 at 6:32
  • 1
    @quixotrykd, your approach is right in general, but not for Xorg. For Xorg, you'd probably want to use frameworks like xinit Jul 26, 2019 at 6:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .