4

The goal is to launch Xephyr in a script. The most popular approach is this:

Xephyr :4 &
sleep 1  # or sleep 2
launch_your_program_here

Let's see what happens here. First, unfortunately, Xephyr cannot daemonize itself, so we have to launch it asynchonously (&). Then we don't know when it's initialized, so we give it 1-2 seconds to initialize. This feels hacky. How to do it faster and more reliably?

VasyaNovikov
  • 1,096
  • 1
  • 11
  • 22
  • @Christopher not sure it solves the exact problem described though.. – VasyaNovikov Oct 19 '16 at 18:54
  • A possibility would be to run `DISPLAY=:4 xrefresh` in a loop until it returns no error. But the already given answers provide better solutions. This approach has the advantage to check whether Xephyr accepts connections. – mviereck Oct 20 '16 at 11:52

2 Answers2

4

A possible solution might be using

Xephyr :4
inotifywait --timeout 9 /tmp/.X11-unix/
launch_your_program_here

The second line will block itself waiting for filesystem changes in /tmp/.X11-unix/, which is what happens when Xephyr finishes initializing itself.

VasyaNovikov
  • 1,096
  • 1
  • 11
  • 22
  • An interesting approach, and a good place to check if initialisation was successful! But I see one drawback: if Xephyr fails to initialize for whatever reasons, inotifywait will wait until end of world ;-) – mviereck Oct 19 '16 at 17:22
  • Good catch! I edited the answer (and my local scripts:) to include a timeout. Thanks! – VasyaNovikov Oct 19 '16 at 18:15
4

xinit does this job. You can create a file your_xinitrc. Commands in your_xinitrc will be executed after Xephyr is initialized.

xinit your_xinitrc -- /usr/bin/Xephyr :4

The X server to run must be given with an absolute path if it is not X itself. See man xinit for details. After all commands in your_xinitrc are terminated, xinit terminates Xephyr.

mviereck
  • 2,377
  • 1
  • 18
  • 18
  • 1
    That's cool. I guess it's not helpful for workflows where you launch multiple commands in the same script, but it's very helpful if you need exactly one command to run (inside Xephyr). Thank you! – VasyaNovikov Oct 19 '16 at 04:42
  • @Vasya Novikov : the file `your_xinitrc` may contain as many commands as you like to. You can put your script completly into it. Normally it is used to create authentication cookies and to start a window manager or a session manager. But it can contain any script you like to, or can call other scripts. Interestingly, `your_xinitrc` does not need the executeable bit to be set. Furthermore interesting, you can start Xephyr with option `-auth COOKIEFILE` to await authentication cookies from clients, and create `COOKIEFILE` _afterwards_ in `your_xinitrc`. – mviereck Oct 19 '16 at 17:09
  • I think the difficulty here is that you may already be in a script where you execute `Xephyr` from. With the `xinit` approach you'll then need to create a second script containing some of your logic, which might be an overhead. Also, passing variables between the scripts might be problematic. – VasyaNovikov Jul 30 '21 at 06:11
  • @[mviereck](https://unix.stackexchange.com/users/185747/mviereck) I think the difficulty here is that you may already be in a script where you execute `Xephyr` from. With the `xinit` approach you'll then need to create a second script containing some of your logic, which might be an overhead. Also, passing variables between the scripts might be problematic. – VasyaNovikov Jul 30 '21 at 06:12