1

Executing this test.sh script:

#!/bin/bash
sleep 15 & sleep 2000

Then, viewing the running processes with $ ps auxfwww | grep -i [2]000 -B 10, I get the following 11886 and 11887 PIDs:

user      7070  0.0  0.0   4504  1444 ?        Ss   Nov15   0:00          \_ /bin/sh /etc/xdg/xfce4/xinitrc -- /etc/X11/xinit/xserverrc
user      7081  0.0  0.0 324540 14064 ?        Sl   Nov15   0:00          |   \_ xfce4-session
user      7100  0.0  0.3 562128 62800 ?        Sl   Nov15   0:48          |       \_ tilix
user     23644  0.0  0.0  31132  6136 pts/11   Ss   14:32   0:00          |       |   \_ /bin/bash
user     11884  0.0  0.0  20024  3044 pts/11   S+   14:58   0:00          |       |   |   \_ /bin/bash ./test.sh
user     11886  0.0  0.0  14776   700 pts/11   S+   14:58   0:00          |       |   |       \_ sleep 15
user     11887  0.0  0.0  14776   704 pts/11   S+   14:58   0:00          |       |   |       \_ sleep 2000

Is it possible to execute the sleep 2000 command entirely independent of the parent processes? For example:

user      7070  0.0  0.0   4504  1444 ?        Ss   Nov15   0:00          \_ /bin/sh /etc/xdg/xfce4/xinitrc -- /etc/X11/xinit/xserverrc
user      7081  0.0  0.0 324540 14064 ?        Sl   Nov15   0:00          |   \_ xfce4-session
user      7100  0.0  0.3 562128 62800 ?        Sl   Nov15   0:48          |   |   \_ tilix
user     23644  0.0  0.0  31132  6136 pts/11   Ss   14:32   0:00          |   |   |   \_ /bin/bash
user     11884  0.0  0.0  20024  3044 pts/11   S+   14:58   0:00          |   |   |   |   \_ /bin/bash ./test.sh
user     11886  0.0  0.0  14776   700 pts/11   S+   14:58   0:00          |   |   |   |       \_ sleep 15
user     XXXXX  0.0  0.0  XXXXX   XXX XXXXXX   S+   XX:XX   0:00          \_ sleep 2000
0

Yes, it's possible.

What you want here is for the process to be reparented. Unix will reparent process to PID 1 once their original parent processes terminate.

So all you need to do is to have the original parent of the sleep 2000 exit.

You can do that by launching the background sleep 2000 from a subshell.

Something like:

#!/bin/bash
sleep 15 &
( sleep 2000 & )
# do something else here, like:
sleep 1
echo "=== Listing 'sleep' process tree: ==="
ps auxfwww | grep -i [2]000 -B 10
echo "=== All done. ===
  • thanks so much. This method doesnt seem to work with variables, for example: $ var="/bin/sleep";( eval $var 2000 & ) – user321517 Nov 17 '18 at 23:42
  • It works if you don't use eval (which you don't really need.) Just use the variable directly. And, for correctness, you should quote it: var=/bin/sleep; ( "$var" 2000 & ). – filbranden Nov 17 '18 at 23:47
  • I'm somewhat confused; the question and the answer talk about PPID, but that's not part of the output of ps auxfwww at all? – tink Nov 17 '18 at 23:57
  • @FilipeBrandenburger maybe you can assist with this one as well? unix.stackexchange.com/questions/482431 – user321517 Nov 18 '18 at 0:15
  • @tink see the alignment of the sleep in the process tree. Being in the first level suggests to me that it's intended to be right under init... – filbranden Nov 18 '18 at 0:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.