Skip to content

Instantly share code, notes, and snippets.

@cmdoptesc
Last active September 14, 2023 04:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmdoptesc/e41f671888580225e5c1dc946d0aa121 to your computer and use it in GitHub Desktop.
Save cmdoptesc/e41f671888580225e5c1dc946d0aa121 to your computer and use it in GitHub Desktop.
ps - list processes running longer than x with name y

ps filter for Chrome processes running longer than 10 minutes (600s):

ps -e -o pid,etimes,command | awk '{ if ( $2 > 600 && $3 ~ /chrome/) print $0 }'

If the last print $0 is changed to print $1, it will just show the first column, the PID.


Copied from Nestor U. - https://unix.stackexchange.com/questions/531040/list-processes-that-have-been-running-more-than-2-hours/592472#592472

One liner to find processes that have been running for over 2 hours

ps -e -o pid,etimes,command | awk '{if($2>7200) print $0}'

Explanation:

ps: process snapshot command

-e: list all processes

-o: include only specified columns

  • pid: process id
  • etimes: elapsed time since the process was started, in seconds (note: some versions of ps do not support etimes)
  • command: command with all its arguments as a string

awk: pattern scanning and processing language

$2: second token from each line (default separator is any amount of whitespace)

7200: 7200 seconds = 2 hours

$0: the whole line in awk

Since the default action in the pattern { action } structure in awk is to print the current line, this can be shortened to:

ps -e -o pid,etimes,command | awk '$2 > 7200'

See also: https://www.thegeekstuff.com/2010/02/awk-conditional-statements/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment