Using this command I can find all the processes for wget
:
ps -aef | grep -i wget
But I want a command or a script which can provide me the processes older than one day for wget
and kill
them.
Using this command I can find all the processes for wget
:
ps -aef | grep -i wget
But I want a command or a script which can provide me the processes older than one day for wget
and kill
them.
You can try formatting the output of ps:
ps -ae -o start,pid,command
If the process is older than 24 hours, it shows date instead of time in the first column.
As long as you mean "more than 24 hours" when you say older than a day, this should work.
for wgetpid in $(ps -eo "%t %p %c" | grep "^ *[0-9][0-9]*-" | grep wget | cut -d ' ' -f 2);
do
kill -9 $wgetpid
done
The ps -eo command outputs the time (in format [[dd-]hh:]mm:ss), the pid and the process name.
If the process has been running for less than 24 hours, there's no - in the time.
The first grep command matches lines that start with (^) zero or more spaces (processes running for 1-9 days are indented if there are any processes that have been running for 10 or more days), a number, zero or more numbers (for 2+ digit number of days), then the - that indicates the previous number was days.
The second grep filters that down to wget processes.
The cut command grabs just the 2nd field (the pid).
Then the for loop cycles through those pids, and kills each one.