Click here to Skip to main content
15,881,380 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.
Posted
Updated 23-Nov-17 21:40pm
v3

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:
Bash
#!/bin/bash

MIN_ACT=10
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
fi
 
Share this answer
 
Comments
C Pottinger 24-Nov-17 9:17am    
Thank you. That taught me enough to come up with this script:
#!/bin/bash

MIN_ACT=200

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.
else
    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
    fi
fi
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