How does one correctly kill processes in bash

Shf

I use the following script to kill process by timeout:

# $1 - name of program and its command line

#launch program and remember PID
eval "$1" &
PID=$!

echo "Program '"$1"' started, PID="$PID

i=1
while [ $i -le 300 ]
do
 ps -p $PID >> /dev/null
 if [ $? -ne 0 ]
  then
   wait $PID
   exit $? #success, return rc of program
  fi

 i=$(($i+1))
 echo "waiting 1 second..."
 sleep 1
done

#program does not want to exit itself, kill it
echo "killing program..."
kill $PID
exit 1 #failed

So far, it have worked excellent, but today, i've noticed a bunch of 'hanging' processes in htop, so i've checked out and it turns out, that $PID in this case is not ID of the program process but of the script itself, and all the times i checked, ID of the program is $PID+1. Now, the question is, am i correct to assume, that it will always be $PID+1 and i won't kill something important by replacing kill $PID with something like kill $PID $($PID+1)

EDIT: $1 may have several urguments, like ./bzip2 -ds sample3.bz2 -k

lurker

You can solve the problem simply with the following change:

From:

eval "$1" &

To:

eval "$1 &"

The reason is explained in this answer.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How to kill all processes apart from specific one

분류에서Dev

Best way to kill processes created by bash script?

분류에서Dev

How do I kill processes in Ubuntu?

분류에서Dev

C++ application does not kill all processes on exit

분류에서Dev

Why does kill -9 0 on a mac simply kill the bash shell?

분류에서Dev

when parent process is killed by 'kill -9', how to terminate child processes

분류에서Dev

How do I kill all processes except PID 1?

분류에서Dev

How to kill all processes owned by `user` on Centos 7?

분류에서Dev

How do I run two processes and find out when one ends in bash

분류에서Dev

How does processes synchronization using signals work?

분류에서Dev

How can I kill all child processes of a certain process from the command line?

분류에서Dev

How do I make one thread Kill another?

분류에서Dev

How to kill one tab of google chrome using pid

분류에서Dev

Filter out and kill non-docker processes

분류에서Dev

How does ps get the executable of processes of other users?

분류에서Dev

Why does bash need && to echo a variable on one line?

분류에서Dev

How to delete one word forward in bash?

분류에서Dev

Sed command does not execute correctly when called from bash script and works fine when called from prompt

분류에서Dev

how to read files listed by ls command one by one in bash?

분류에서Dev

GetLastInputInfo does not correctly work?

분류에서Dev

How does one import a scala package in java?

분류에서Dev

How does one set boot priority to cd?

분류에서Dev

How does tr translate one word to another?

분류에서Dev

How does one generate vertical quotes in Word?

분류에서Dev

pthread for processes with more than one parameter

분류에서Dev

Why does `kill %jobnumber` not work on stopped jobs?

분류에서Dev

How to kill a running iPython process

분류에서Dev

How to kill process of another user?

분류에서Dev

How do we correctly format a LinkedIn post request for a field selector (one containing colons and parentheses)?

Related 관련 기사

  1. 1

    How to kill all processes apart from specific one

  2. 2

    Best way to kill processes created by bash script?

  3. 3

    How do I kill processes in Ubuntu?

  4. 4

    C++ application does not kill all processes on exit

  5. 5

    Why does kill -9 0 on a mac simply kill the bash shell?

  6. 6

    when parent process is killed by 'kill -9', how to terminate child processes

  7. 7

    How do I kill all processes except PID 1?

  8. 8

    How to kill all processes owned by `user` on Centos 7?

  9. 9

    How do I run two processes and find out when one ends in bash

  10. 10

    How does processes synchronization using signals work?

  11. 11

    How can I kill all child processes of a certain process from the command line?

  12. 12

    How do I make one thread Kill another?

  13. 13

    How to kill one tab of google chrome using pid

  14. 14

    Filter out and kill non-docker processes

  15. 15

    How does ps get the executable of processes of other users?

  16. 16

    Why does bash need && to echo a variable on one line?

  17. 17

    How to delete one word forward in bash?

  18. 18

    Sed command does not execute correctly when called from bash script and works fine when called from prompt

  19. 19

    how to read files listed by ls command one by one in bash?

  20. 20

    GetLastInputInfo does not correctly work?

  21. 21

    How does one import a scala package in java?

  22. 22

    How does one set boot priority to cd?

  23. 23

    How does tr translate one word to another?

  24. 24

    How does one generate vertical quotes in Word?

  25. 25

    pthread for processes with more than one parameter

  26. 26

    Why does `kill %jobnumber` not work on stopped jobs?

  27. 27

    How to kill a running iPython process

  28. 28

    How to kill process of another user?

  29. 29

    How do we correctly format a LinkedIn post request for a field selector (one containing colons and parentheses)?

뜨겁다태그

보관