Skip to main content
Fixed a typo; man page is not code
Source Link
Toby Speight
  • 9.4k
  • 3
  • 32
  • 54

Don't use the name to kill it. Since the calling.sh script is calling the process you later want to kill, just use $! (from man bash):

   !      Expands  to the process ID of the job most recently placed into the back‐
          ground, whether executed as an  asynchronous  command  or  using  the  bg

! Expands to the process ID of the job most recently placed into the background, whether executed as an asynchronous command or using the bg builtin

So, if youryou're calling.sh is like this:

called.sh &
## do stuff
pkill called.sh

Change it to this:

called.sh &
calledPid=$!
# do stuff
kill "$calledPid"

Don't use the name to kill it. Since the calling.sh script is calling the process you later want to kill, just use $! (from man bash):

   !      Expands  to the process ID of the job most recently placed into the back‐
          ground, whether executed as an  asynchronous  command  or  using  the  bg

So, if your calling.sh is like this:

called.sh &
## do stuff
pkill called.sh

Change it to this:

called.sh &
calledPid=$!
# do stuff
kill "$calledPid"

Don't use the name to kill it. Since the calling.sh script is calling the process you later want to kill, just use $! (from man bash):

! Expands to the process ID of the job most recently placed into the background, whether executed as an asynchronous command or using the bg builtin

So, if you're calling.sh is like this:

called.sh &
## do stuff
pkill called.sh

Change it to this:

called.sh &
calledPid=$!
# do stuff
kill "$calledPid"
Source Link
terdon
  • 252.7k
  • 69
  • 481
  • 719

Don't use the name to kill it. Since the calling.sh script is calling the process you later want to kill, just use $! (from man bash):

   !      Expands  to the process ID of the job most recently placed into the back‐
          ground, whether executed as an  asynchronous  command  or  using  the  bg

So, if your calling.sh is like this:

called.sh &
## do stuff
pkill called.sh

Change it to this:

called.sh &
calledPid=$!
# do stuff
kill "$calledPid"