1

How do I kill all processes with my username that were started within (past hour, past day) etc?

1
  • There is no ready-to-use solution, I think. Why not use ps to list all of your processes, use cut -f`` to extract the process id and the start time and compare with date` to kill the newer ones. The ps command is very different between traditional UNIX, BSD and Linux, so I doubt it's possible to give you a script without knowing your system.
    – Philippos
    Nov 5, 2021 at 8:38

1 Answer 1

3
  1. find your processes that are younger than an hour
  2. extract the pids
  3. kill the pids

process list:

$ ps -e -o pid,user,etimes,comm \
    | awk -v me=$USER '$2 == me && $3 <= 3600 { print }'

Produces

 661162 jaroslav    3006 chrome
 667859 jaroslav    1711 chrome
 669145 jaroslav    1471 chrome
 671222 jaroslav    1016 chrome
 675278 jaroslav     270 chrome
 675578 jaroslav     207 sleep
 676094 jaroslav      91 chrome
 676102 jaroslav      91 chrome
 676528 jaroslav      11 chrome
 676529 jaroslav      11 chrome
 676553 jaroslav      11 chrome
 676602 jaroslav       3 top
 676615 jaroslav       0 ps
 676616 jaroslav       0 awk

extract pids:

$ ps -e -o pid,user,etimes,comm \
    | awk -v me=$USER '$2 == me && $3 <= 3600 { print $1 }'

Kill pids:

 $ ps -e -o pid,user,etimes,comm \
    | awk -v me=$USER '$2 == me && $3 <= 3600 { print $1 }' \
    | xargs -rt kill

The -tr arguments to xargs are optional and will skip xargs if there is no output and report every executed line.

You can even test it with kill -0 which does nothing to stop the process, but will report an error if the process is no longer running.

$ ps -e -o pid,user,etimes,comm \
    | awk -v me=$USER '$2 == me  && $3 <= 3600 { print $1 }' \
    | xargs -rt kill -0 
kill -0 661162 667859 669145 671222 675278 676602 677310 677311 677883 677893 677965 677966 677967 677968
kill: (677966): No such process
kill: (677967): No such process

Realizing that this pipe / script can kill itself after feedback, (notice etimes=0 in the process list above), here is a revised version which ignores very recent processes:

ps -u "$LOGNAME" -o pid,etimes,comm \
  | awk '$2 <= 3600 && $2 > 1 { print $1 }' \
  | xargs -rt kill -0

This is probably not very portable, but should work on Linux (at least ubuntu 18). Hopefully this gives you some idea about how to approach this problem.

<mother-mode> Do run the ps command without awk and xargs and kill first to see what would be killed and be careful if running as root. You could potentially shut down the system or kill some important service that has recently been restarted. </mother-mode>

3
  • For large lists of pids, xargs could end up running kill with a first batch of pids that includes that of xargs, ps or awk (which all run concurrently). You may want to run all that under (trap '' TERM; ps...). Or use kill $(....). kill being builtin is not affected by the limit of size of arguments. Nov 5, 2021 at 14:39
  • You can use ps -u "$LOGNAME" instead of ps -e to get ps to do the filtering by username (also avoids problems with userids shared by more than one username) Nov 5, 2021 at 14:40
  • Great feedback. Thank you. Nov 5, 2021 at 15:08

You must log in to answer this question.

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