Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- foo &
- FOO_PID=$!
- # do other stuff
- kill $FOO_PID
- ^Z
- [1]+ Stopped guard
- my_mac:workspace r$ jobs -l
- [1]+ 46841 Suspended: 18 guard
- $ echo $$
- 3748
- $ sleep 100 &
- [1] 192
- $ echo $!
- 192
- $ kill %1
- [1]+ Terminated sleep 100
- pkill -P $$
- #!/bin/bash
- #
- # So something to show.
- echo "UNO" > UNO.txt
- echo "DOS" > DOS.txt
- #
- # Initialize Pid List
- dPidLst=""
- #
- # Generate background processes
- tail -f UNO.txt&
- dPidLst="$dPidLst $!"
- tail -f DOS.txt&
- dPidLst="$dPidLst $!"
- #
- # Report process IDs
- echo PID=$$
- echo dPidLst=$dPidLst
- #
- # Show process on current shell
- ps -f
- #
- # Start killing background processes from list
- for dPid in $dPidLst
- do
- echo killing $dPid. Process is still there.
- ps | grep $dPid
- kill $dPid
- ps | grep $dPid
- echo Just ran "'"ps"'" command, $dPid must not show again.
- done
- root@umsstd22 [P]:~# ./bgkill.sh
- PID=23757
- dPidLst= 23758 23759
- UNO
- DOS
- UID PID PPID C STIME TTY TIME CMD
- root 3937 3935 0 11:07 pts/5 00:00:00 -bash
- root 23757 3937 0 11:55 pts/5 00:00:00 /bin/bash ./bgkill.sh
- root 23758 23757 0 11:55 pts/5 00:00:00 tail -f UNO.txt
- root 23759 23757 0 11:55 pts/5 00:00:00 tail -f DOS.txt
- root 23760 23757 0 11:55 pts/5 00:00:00 ps -f
- killing 23758. Process is still there.
- 23758 pts/5 00:00:00 tail
- ./bgkill.sh: line 24: 23758 Terminated tail -f UNO.txt
- Just ran 'ps' command, 23758 must not show again.
- killing 23759. Process is still there.
- 23759 pts/5 00:00:00 tail
- ./bgkill.sh: line 24: 23759 Terminated tail -f DOS.txt
- Just ran 'ps' command, 23759 must not show again.
- root@umsstd22 [P]:~# ps -f
- UID PID PPID C STIME TTY TIME CMD
- root 3937 3935 0 11:07 pts/5 00:00:00 -bash
- root 24200 3937 0 11:56 pts/5 00:00:00 ps -f
- pstree -p user
- trap 'kill $( pgrep -P $$ | tr "n" " " )' SIGINT SIGTERM EXIT
Add Comment
Please, Sign In to add comment