Advertisement
Guest User

Untitled

a guest
Dec 16th, 2022
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.79 KB | None | 0 0
  1. root@OpenWrt:~/cake-autorate# cat test.sh
  2. #!/bin/bash
  3.  
  4. kill_all()
  5. {
  6.         trap - INT TERM EXIT
  7.         echo "Killing background with pid=$background_pid"
  8.         kill $background_pid
  9.         wait $background_pid
  10. }
  11.  
  12. kill_background()
  13. {
  14.         trap - TERM EXIT
  15.         echo "killing pinger with pid=$ping_pid"
  16.         kill $ping_pid
  17.         echo "killed pinger"
  18.         exit
  19. }
  20.  
  21. background()
  22. {
  23.         trap '' INT
  24.         trap kill_background TERM EXIT
  25.  
  26.         ping 1.1.1.1&
  27.         ping_pid=$!
  28.  
  29.         echo "Started ping with pid=$ping_pid"
  30.  
  31.         sleep inf
  32.  
  33.         echo "Background exited."
  34. }
  35.  
  36. set -x
  37.  
  38. trap kill_all INT TERM EXIT
  39.  
  40. background&
  41. background_pid=$!
  42. echo "Started test.sh with background_pid=$background_pid"
  43.  
  44. sleep inf
  45.  
  46. echo "script termination"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement