Vahid Pazirandeh
Vahid Pazirandeh

Reputation: 1592

How do I put an already running CHILD process under nohup

My question is very similar to that posted in: How do I put an already-running process under nohup?

Say I execute foo.sh from my command line, and it in turn executes another shell script, and so on. For example:

foo.sh
 \_ bar.sh
    \_ baz.sh

Now I press Ctrl+Z to suspend "foo.sh". It is listed in my "jobs -l".

How do I disown baz.sh so that it is no longer a grandchild of foo.sh? If I type "disown" then only foo.sh is disowned from its parent, which isn't exactly what i want. I'd like to kill off the foo.sh and bar.sh processes and only be left with baz.sh.

My current workaround is to "kill -18" (resume) baz.sh and go on with my work, but I would prefer to kill the aforementioned processes. Thanks.

Upvotes: 1

Views: 230

Answers (1)

Barmar
Barmar

Reputation: 781761

Use ps to get the PID of bar.sh, and kill it.

imac:barmar $ ps -l -t p0 -ww
  UID   PID  PPID        F CPU PRI NI       SZ    RSS WCHAN     S     ADDR TTY           TIME CMD
  501  3041  3037     4006   0  31  0  2435548    760 -      Ss    8c6da80 ttyp0      0:00.74 /bin/bash --noediting -i
  501 68228  3041     4006   0  31  0  2435544    664 -      S     7cbc2a0 ttyp0      0:00.00 /bin/bash ./foo.sh
  501 68231 68228     4006   0  31  0  2435544    660 -      S     c135a80 ttyp0      0:00.00 /bin/bash ./bar.sh
  501 68232 68231     4006   0  31  0  2435544    660 -      S     a64b7e0 ttyp0      0:00.00 /bin/bash ./baz.sh
  501 68233 68232     4006   0  31  0  2426644    312 -      S     f9a1540 ttyp0      0:00.00 sleep 100
    0 68243  3041     4106   0  31  0  2434868    480 -      R+    a20ad20 ttyp0      0:00.00 ps -l -t p0 -ww
imac:barmar $ kill 68231
./foo.sh: line 3: 68231 Terminated              ./bar.sh
[1]+  Exit 143                ./foo.sh
imac:barmar $ ps -l -t p0 -ww
  UID   PID  PPID        F CPU PRI NI       SZ    RSS WCHAN     S     ADDR TTY           TIME CMD
  501  3041  3037     4006   0  31  0  2435548    760 -      Ss    8c6da80 ttyp0      0:00.74 /bin/bash --noediting -i
  501 68232     1     4006   0  31  0  2435544    660 -      S     a64b7e0 ttyp0      0:00.00 /bin/bash ./baz.sh
  501 68233 68232     4006   0  31  0  2426644    312 -      S     f9a1540 ttyp0      0:00.00 sleep 100
    0 68248  3041     4106   0  31  0  2434868    480 -      R+    82782a0 ttyp0      0:00.00 ps -l -t p0 -ww

Upvotes: 1

Related Questions