Click here to Skip to main content
15,664,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi folks,

I'm trying to combine a couple of awk commands and I'm having no luck. The idea is to kill a process if there has been less that a certain amount of download traffic within a certain period of time.

Any help would be appreciated.

What I have tried:

I can get the following to properly find the PID of a running qbittorrent process:
echo $( ps aux | grep "[q]bittorrent" | awk '{ print $2 }' )

And the following will tell me if there has been low download activity over the last few seconds:
collectl -c1 -i5 -sn | awk 'NF==4 && $2<200  { print $1 }'
The output is the amount of KB downloaded in last 5 seconds.

Now I want to combine these - replacing the print $1 in the second command with "kill" followed by the output of the first command.

I've tried several variations, such as
sudo $( collectl -c1 -i5 -sn | awk 'NF==4 && $2<20 { print "kill " $( ps aux | grep "[q]bittorrent" | awk '{ print $2 }' ) }' )
but to no avail.
Updated 23-Nov-17 21:40pm

1 solution

You should put the commands into a shell script. Then you can use variables, conditions and much more.

Untested example without checking for empty variables:

ACTIVITY=$( collectl -c1 -i5 -sn | awk 'NF==4 && $2<200 { print $1 }' )
if [ $ACTIVITY -lt $MIN_ACT ]; then
    TORR_PID=$( ps aux | grep "[q]bittorrent" | awk '{ print $2 }' )
    kill -s SIGTERM $TORR_PID
Share this answer
C Pottinger 24-Nov-17 9:17am    
Thank you. That taught me enough to come up with this script:


echo Looking for running qBitorrent. Please wait...
TORR_PID=$( ps aux | grep '[q]bittorrent' | awk '{ print $2 }' )
if [ -z "$TORR_PID" ]; then
    echo qBittorrent not found.
    echo Collecting traffic infomation. Please wait...
    ACTIVITY=$( collectl -c1 -i5 -sn | awk 'NF==4 { print $1 }' )
    if [ $ACTIVITY -lt $MIN_ACT ]; then
        echo Traffic is low. Killing qBittorrent PID:$TORR_PID. Please wait...
        kill -s SIGTERM $TORR_PID
echo killqb done.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900