Results 1 to 8 of 8

Thread: bash - kill process and all sub-processes

  1. #1

    Default bash - kill process and all sub-processes

    Code:
         elif [[ "$1" == '-ksv' ]]  || [[ "$1" == '--killscanvirus' ]]; then
              pkill -INT -x scanvirus
              pkill -INT -x clamscan
              shutdown -c
              printf "scans shutdown\n"
    
         elif [[ "$1" == '-kls' ]]  || [[ "$1" == '--killlastscan' ]]; then
              pkill -INT -n -x scanvirus
              pkill -INT -n -x clamscan
              shutdown -c
              printf "last scan shutdown\n"
    The first function works fine, but it's based on limited understanding of process trees. The scanvirus script has many sub-shells. Control-c works fine for live scans. For background scans, such as:
    'scanvirus hupc'
    high-speed, update, power-down, copy virus files

    scanvirus -ksv works for that. Now, it works if you want do 2+ scans at once. Shutting them down doesn't work right.

    I'v looked online for solutions. I need to understand the commands dealing with process trees. How to kill the 'scanvirus' process and all tree branches?

    Point me to good tutorial or help me understand it.

    Thanks.

  2. #2
    Join Date
    Jun 2008
    Location
    Podunk
    Posts
    31,734
    Blog Entries
    15

    Default Re: bash - kill process and all sub-processes

    Hi
    Create pidfiles on start to identify and kill off.....
    Cheers Malcolm °¿° SUSE Knowledge Partner (Linux Counter #276890)
    SUSE SLE, openSUSE Leap/Tumbleweed (x86_64) | GNOME DE
    If you find this post helpful and are logged into the web interface,
    please show your appreciation and click on the star below... Thanks!

  3. #3
    Join Date
    Sep 2012
    Posts
    6,791

    Default Re: bash - kill process and all sub-processes

    Pragmatic solution - start your program as systemd service and let systemd do cleanup for you.

    Legacy solution - start you script in new process group, then you can send signals to all processes in this group. If you make a search for "bash change process group" you will get even complete code examples how to change process group of script.

    Roundabout solution - start your script attached to pseudo terminal and emulate ^C.

    I am sure there are some more options.

  4. #4
    Join Date
    Jan 2014
    Location
    Erlangen
    Posts
    2,974

    Default Re: bash - kill process and all sub-processes

    AMD Athlon 4850e (2009), openSUSE 13.1, KDE 4, Intel i3-4130 (2014), i7-6700K (2016), i5-8250U (2018), AMD Ryzen 5 3400G (2020), openSUSE Tumbleweed, KDE Plasma 5

  5. #5

    Default Re: bash - kill process and all sub-processes

    Here's is one useful command I found.

    Why are their two equal 'scanvirus -a c' commands under the same process group?



    Code:
    # ps -a -o pid,pgid,cmd
      PID  PGID CMD
     2781  2781 su -
     2789  2789 -bash
     2884  2837 dbus-launch --autolaunch 1b1b8badbac04cf4a493a279b3d80889 --binary-syntax --close-stderr
     4754  4727 openvpn --config /root/.pvpn-cli/connect.ovpn --auth-user-pass /root/.pvpn-cli/pvpnpass --dev proton0 --dev-type tun
     5828  5828 /bin/bash /usr/bin/scanvirus -a c
     5878  5828 clamscan -r --copy=/var/log/VirusVault/linux_Y2021.M07.D14.H10.M38.S59.1626327539 --cross-fs=no --follow-file-symlinks=1 --follow-dir-symlinks=1 --
     5879  5828 /bin/bash /usr/bin/scanvirus -a c
     6348  6348 ps -a -o pid,pgid,cmd

  6. #6
    Join Date
    Jan 2014
    Location
    Erlangen
    Posts
    2,974

    Default Re: bash - kill process and all sub-processes

    Quote Originally Posted by lord_valarian View Post
    Here's is one useful command I found.

    Why are their two equal 'scanvirus -a c' commands under the same process group?



    Code:
    # ps -a -o pid,pgid,cmd
      PID  PGID CMD
     2781  2781 su -
     2789  2789 -bash
     2884  2837 dbus-launch --autolaunch 1b1b8badbac04cf4a493a279b3d80889 --binary-syntax --close-stderr
     4754  4727 openvpn --config /root/.pvpn-cli/connect.ovpn --auth-user-pass /root/.pvpn-cli/pvpnpass --dev proton0 --dev-type tun
     5828  5828 /bin/bash /usr/bin/scanvirus -a c
     5878  5828 clamscan -r --copy=/var/log/VirusVault/linux_Y2021.M07.D14.H10.M38.S59.1626327539 --cross-fs=no --follow-file-symlinks=1 --follow-dir-symlinks=1 --
     5879  5828 /bin/bash /usr/bin/scanvirus -a c
     6348  6348 ps -a -o pid,pgid,cmd
    Nothing useful above, in my opinion of course.
    Code:
    erlangen:~ # ll /sys/fs/cgroup/user.slice/user-1000.slice/user@1000.service/app.slice/app-firefox-3a3b257f18b346f7a0265028d632e6bb.scope 
    total 0 
    -r--r--r-- 1 karl users 0 Jul 15 06:02 cgroup.controllers 
    -r--r--r-- 1 karl users 0 Jul 15 06:02 cgroup.events 
    -rw-r--r-- 1 karl users 0 Jul 15 06:02 cgroup.freeze 
    -rw-r--r-- 1 karl users 0 Jul 15 06:02 cgroup.max.depth 
    -rw-r--r-- 1 karl users 0 Jul 15 06:02 cgroup.max.descendants 
    -rw-r--r-- 1 karl users 0 Jul 15 06:02 cgroup.procs 
    -r--r--r-- 1 karl users 0 Jul 15 06:02 cgroup.stat 
    -rw-r--r-- 1 karl users 0 Jul 15 06:02 cgroup.subtree_control 
    -rw-r--r-- 1 karl users 0 Jul 15 06:02 cgroup.threads 
    -rw-r--r-- 1 karl users 0 Jul 15 06:02 cgroup.type 
    -rw-r--r-- 1 karl users 0 Jul 15 06:02 cpu.pressure 
    -r--r--r-- 1 karl users 0 Jul 15 06:02 cpu.stat 
    -rw-r--r-- 1 karl users 0 Jul 15 06:02 io.pressure 
    -r--r--r-- 1 karl users 0 Jul 15 06:02 memory.current 
    -r--r--r-- 1 karl users 0 Jul 15 06:02 memory.events 
    -r--r--r-- 1 karl users 0 Jul 15 06:02 memory.events.local 
    -rw-r--r-- 1 karl users 0 Jul 15 06:02 memory.high 
    -rw-r--r-- 1 karl users 0 Jul 15 06:02 memory.low 
    -rw-r--r-- 1 karl users 0 Jul 15 06:02 memory.max 
    -rw-r--r-- 1 karl users 0 Jul 15 06:02 memory.min 
    -r--r--r-- 1 karl users 0 Jul 15 06:02 memory.numa_stat 
    -rw-r--r-- 1 karl users 0 Jul 15 06:02 memory.oom.group 
    -rw-r--r-- 1 karl users 0 Jul 15 06:02 memory.pressure 
    -r--r--r-- 1 karl users 0 Jul 15 06:02 memory.stat 
    -r--r--r-- 1 karl users 0 Jul 15 06:02 memory.swap.current 
    -r--r--r-- 1 karl users 0 Jul 15 06:02 memory.swap.events 
    -rw-r--r-- 1 karl users 0 Jul 15 06:02 memory.swap.high 
    -rw-r--r-- 1 karl users 0 Jul 15 06:02 memory.swap.max 
    -r--r--r-- 1 karl users 0 Jul 15 06:02 pids.current 
    -r--r--r-- 1 karl users 0 Jul 15 06:02 pids.events 
    -rw-r--r-- 1 karl users 0 Jul 15 06:02 pids.max 
    erlangen:~ # systemd-cgls /sys/fs/cgroup/user.slice/user-1000.slice/user@1000.service/app.slice/app-firefox-3a3b257f18b346f7a0265028d632e6bb.scope 
    Directory /sys/fs/cgroup/user.slice/user-1000.slice/user@1000.service/app.slice/app-firefox-3a3b257f18b346f7a0265028d632e6bb.scope: 
    ├─ 1904 /usr/lib64/firefox/firefox
    ├─ 1996 /usr/lib/mozilla/kmozillahelper
    ├─ 2120 /usr/lib64/firefox/firefox -contentproc -childID 3 -isForBrowser -prefsLen 258 -prefMapSize 256566 -jsInit 285176 -parentBuildID 20210705185941 -appdir /usr/lib64/firefox/browser 1904 true tab
    ├─ 2159 /usr/lib64/firefox/firefox -contentproc -childID 4 -isForBrowser -prefsLen 5536 -prefMapSize 256566 -jsInit 285176 -parentBuildID 20210705185941 -appdir /usr/lib64/firefox/browser 1904 true tab
    ├─ 2588 /usr/lib64/firefox/firefox -contentproc -parentBuildID 20210705185941 -prefsLen 6510 -prefMapSize 256566 -appdir /usr/lib64/firefox/browser 1904 true rdd
    ├─ 8050 /usr/lib64/firefox/firefox -contentproc -childID 15 -isForBrowser -prefsLen 7072 -prefMapSize 256566 -jsInit 285176 -parentBuildID 20210705185941 -appdir /usr/lib64/firefox/browser 1904 true tab
    ├─19530 /usr/lib64/firefox/firefox -contentproc -childID 29 -isForBrowser -prefsLen 7292 -prefMapSize 256566 -jsInit 285176 -parentBuildID 20210705185941 -appdir /usr/lib64/firefox/browser 1904 true tab
    ├─24485 /usr/lib64/firefox/firefox -contentproc -childID 21 -isForBrowser -prefsLen 7208 -prefMapSize 256566 -jsInit 285176 -parentBuildID 20210705185941 -appdir /usr/lib64/firefox/browser 1904 true tab
    ├─24952 /usr/lib64/firefox/firefox -contentproc -childID 23 -isForBrowser -prefsLen 7186 -prefMapSize 256566 -jsInit 285176 -parentBuildID 20210705185941 -appdir /usr/lib64/firefox/browser 1904 true tab
    └─27851 /usr/lib64/firefox/firefox -contentproc -childID 26 -isForBrowser -prefsLen 7187 -prefMapSize 256566 -jsInit 285176 -parentBuildID 20210705185941 -appdir /usr/lib64/firefox/browser 1904 true tab
    erlangen:~ #
    
    AMD Athlon 4850e (2009), openSUSE 13.1, KDE 4, Intel i3-4130 (2014), i7-6700K (2016), i5-8250U (2018), AMD Ryzen 5 3400G (2020), openSUSE Tumbleweed, KDE Plasma 5

  7. #7
    Join Date
    Sep 2012
    Posts
    6,791

    Default Re: bash - kill process and all sub-processes

    Quote Originally Posted by lord_valarian View Post
    Why are their two equal 'scanvirus -a c' commands under the same process group?
    When shell script runs builtin command in subprocess it does not change subprocess command line. Actually child is full copy of parent.

  8. #8

    Default Re: bash - kill process and all sub-processes

    I got it working, kills last ran scanvirus. Until, I tested it with a background scan. It failed. I need to find the date the process group was created in this format.


    I found this method. I need a method that works with all cases.

    Code:
    scan_created_time=$(stat -c %Y /proc/"${line[0]}")

    I found that 'ps' has a command to do this, but not in the format I need. So far, I'v not found it yet.



    Thanks.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •