How to list all processes that have been running longer than an hour in Linux

There are two ways to list all processes that have been running longer than an hour in Linux: using the ps command or the htop command.

1. Using the ps command:

The ps command is a powerful tool for monitoring and managing processes on a Linux system. To list all processes that have been running longer than an hour using the ps command, you can use the following command:
ps -eo etime,pid,lstart,etime,args --sort -etimes | awk '{if ($1 > 3600) print $0 }'

This command will output a list of all processes, along with their elapsed time (etime), process ID (pid), start time (lstart), and command name (args). The awk filter at the end of the command will only print the processes that have been running longer than 3600 seconds, which is equal to one hour.

Example:
$ ps -eo etime,pid,lstart,etime,args --sort -etimes | awk '{if ($1 > 3600) print $0 }'
   ELAPSED   PID  STARTED      COMMAND
    1 hour  2345  15:13:27   /usr/sbin/sshd
    1 hour  3456  15:13:27   /usr/sbin/mysqld

    1 hour  4567  15:13:27   /usr/bin/apache2

2. Using the htop command:

The htop command is an interactive process viewer that provides a real-time view of all running processes on a Linux system. To list all processes that have been running longer than an hour using the htop command, follow these steps:
Open the htop command.
Press F6 to filter the list of processes by age.
Use the arrow keys to sort the list by elapsed time.
The processes that have been running longer than an hour will be displayed at the top of the list.
Example:
htop
F1: Help    F2: Setup    F3: Filter    F4: Sort    F5: Select   F6: Age   F7: Nice   F8: Prio   F9: Pid    F10: Quit
PID USER      PRI  NI  VIRT  RES  SHR S %CPU %MEM    TIME+ COMMAND
1 root      20   0  10824  464  212 S  0.0  0.0   0:00.01 systemd
2 root      20   0       0   0   0 S  0.0  0.0   0:00.00 kthreadd
Note: To filter the list of processes by age, press F6 and select the "Age" option. To sort the list by elapsed time, press F4 and select the "ELAPSED" option.

Once you have identified the processes that have been running longer than an hour, you can investigate them further to determine if they are still needed. If you are no longer using a process, you can kill it using the kill command.

Important: Be careful when killing processes, as this can cause unexpected behavior or even system crashes. It is always best to investigate a process before killing it to make sure that it is not essential to the system.

Additional information:

You can use the etimes option instead of the etime option in the ps command to output the elapsed time in seconds.

You can also use the grep command to filter the list of processes that are output by the ps command. For example, the following command will list all processes that have been running longer than an hour and are owned by the user root:

ps -eo etime,pid,lstart,etime,args --sort -etimes | awk '{if ($1 > 3600) print $0 }' | grep root

You can use the killall command to kill all processes with a given command name. For example, the following command will kill all processes with the command name apache2:

killall apache2```

Comments