Guest User

Untitled

a guest
Jan 16th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. foo &
  2. FOO_PID=$!
  3. # do other stuff
  4. kill $FOO_PID
  5.  
  6. ^Z
  7. [1]+ Stopped guard
  8.  
  9. my_mac:workspace r$ jobs -l
  10. [1]+ 46841 Suspended: 18 guard
  11.  
  12. $ echo $$
  13. 3748
  14.  
  15. $ sleep 100 &
  16. [1] 192
  17.  
  18. $ echo $!
  19. 192
  20.  
  21. $ kill %1
  22.  
  23. [1]+ Terminated sleep 100
  24.  
  25. pkill -P $$
  26.  
  27. #!/bin/bash
  28. #
  29. # So something to show.
  30. echo "UNO" > UNO.txt
  31. echo "DOS" > DOS.txt
  32. #
  33. # Initialize Pid List
  34. dPidLst=""
  35. #
  36. # Generate background processes
  37. tail -f UNO.txt&
  38. dPidLst="$dPidLst $!"
  39. tail -f DOS.txt&
  40. dPidLst="$dPidLst $!"
  41. #
  42. # Report process IDs
  43. echo PID=$$
  44. echo dPidLst=$dPidLst
  45. #
  46. # Show process on current shell
  47. ps -f
  48. #
  49. # Start killing background processes from list
  50. for dPid in $dPidLst
  51. do
  52. echo killing $dPid. Process is still there.
  53. ps | grep $dPid
  54. kill $dPid
  55. ps | grep $dPid
  56. echo Just ran "'"ps"'" command, $dPid must not show again.
  57. done
  58.  
  59. root@umsstd22 [P]:~# ./bgkill.sh
  60. PID=23757
  61. dPidLst= 23758 23759
  62. UNO
  63. DOS
  64. UID PID PPID C STIME TTY TIME CMD
  65. root 3937 3935 0 11:07 pts/5 00:00:00 -bash
  66. root 23757 3937 0 11:55 pts/5 00:00:00 /bin/bash ./bgkill.sh
  67. root 23758 23757 0 11:55 pts/5 00:00:00 tail -f UNO.txt
  68. root 23759 23757 0 11:55 pts/5 00:00:00 tail -f DOS.txt
  69. root 23760 23757 0 11:55 pts/5 00:00:00 ps -f
  70. killing 23758. Process is still there.
  71. 23758 pts/5 00:00:00 tail
  72. ./bgkill.sh: line 24: 23758 Terminated tail -f UNO.txt
  73. Just ran 'ps' command, 23758 must not show again.
  74. killing 23759. Process is still there.
  75. 23759 pts/5 00:00:00 tail
  76. ./bgkill.sh: line 24: 23759 Terminated tail -f DOS.txt
  77. Just ran 'ps' command, 23759 must not show again.
  78. root@umsstd22 [P]:~# ps -f
  79. UID PID PPID C STIME TTY TIME CMD
  80. root 3937 3935 0 11:07 pts/5 00:00:00 -bash
  81. root 24200 3937 0 11:56 pts/5 00:00:00 ps -f
  82.  
  83. pstree -p user
  84.  
  85. trap 'kill $( pgrep -P $$ | tr "n" " " )' SIGINT SIGTERM EXIT
Add Comment
Please, Sign In to add comment