0

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.

1

2 Answers 2

1

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.

1
  • can you please provide me the complete command ?
    – ravi kumar
    Oct 17, 2014 at 15:52
0

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.

3
  • Thanks. This ans was really useful. can someone provide a command or a script which can provide me the processes older than one hour for wget and kill them.
    – ravi kumar
    Nov 1, 2014 at 19:07
  • can someone please provide a script which can provide provide me the processes older than one hour for wget and kill them.
    – ravi kumar
    Nov 3, 2014 at 14:24
  • @ravikumar That requires entirely different logic. I suggest you first accept one of the answers for your existing question (processes older than one day), then ask a new question about processes older than 1 hour. Nov 3, 2014 at 16:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .