How to check how long a process has been running in Linux

WBOY
Release: 2024-03-10 08:04:17
forward
1178 people have browsed it

The running time of a process refers to the time that has elapsed since the process was started.

Checking the running time of processes can help us determine which processes in the system have been running for how long. This is critical for identifying issues that cause slow system response and high system resource usage.

We discover and deal with these long-running processes in time, which can improve the stability and performance of the system.

How to check how long a process has been running in Linux

Use ps command

The

ps command is a powerful tool for reporting the current status of system processes.

By combining different options, we can obtain various information about the process, including the process running time.

Use the following command to view the basic information of the process, including process ID (PID), running time, etc.:

ps -eo pid,cmd,etime
Copy after login
  • -e: Show all processes
  • -o: Specify the output format

In the columns output by the above command, etime represents the running time of the process.

For example:

PID CMDELAPSED
 1234 /usr/bin/example-process02:30:45
Copy after login

This means that the process with PID 1234 example-process has been running for 2 hours, 30 minutes and 45 seconds.

Use top command

top is a command line tool for real-time monitoring of system processes, providing a dynamically updated process list.

Run the following command to start top:

top
Copy after login

In the top interface, you can press the Shift E key to switch the display of the running time of the process.

This will cause the process list to contain a TIME column, showing the total time the process has been running.

PID USERPRNIVIRTRESSHR S%CPU%MEM TIME+ COMMAND
1234 user 20 020001210560 8204 R 5.0 0.302:30:45 example-process
Copy after login

In this example, the running time of example-process is 2 hours, 30 minutes and 45 seconds.

Use pstree command

pstree The command displays the hierarchical structure of the process in the form of a tree diagram. By looking at the process tree, we can get a clearer picture of the relationships between processes and see how long they take to run.

Run the following command to use pstree to view the process tree:

pstree -p
Copy after login

Among them, the -p option displays the PID of the process. The output may be as follows:

init(1)─┬─systemd─┬─(sd-pam)
│ ├─...
│ └─(process-1)
├─(process-2)
├─(process-3)
└─...
Copy after login

By looking at this tree structure, you can identify each process and its sub-processes, and learn how long they have been running if needed.

Use /proc file system

The /proc directory in Linux is a virtual file system that provides information about the system and processes.

We can obtain the detailed information of the process, including running time, by reading the file under /proc.

By reading the /proc/[PID]/stat file, we can get detailed information about a specific process, including the running time.

The following is an example:

cat /proc/1234/stat
Copy after login

The output may be as follows:

1234 (example-process) S 0 1234 1234 0 -1 4194560 385 0 0 0 0 0 0 0 20 0 1 0 12345678 1024 18446744073709551615 134512640 134513319 4294956704 18446744073709551615 135481464 0 0 0 0 17 1 0 0 0 0 0 0 0 0 0
Copy after login

In the output, column 22 (from the left) represents the running time of the process, in clock ticks.

Use htop

htop is an interactive and more friendly process viewing tool, which is more powerful and intuitive than the traditional top command.

Run the following command to start htop:

htop
Copy after login

In the htop interface, you can use the arrow keys and function keys to navigate and perform various operations.

Press the F6 key, select and enable TIME to display the running time of the process.

PID USERPRINIVIRTRESSHR S%CPU%MEM TIME+ COMMAND
1234 user20 020001210560 8204 R 5.0 0.302:30.45 example-process
Copy after login

Here, the TIME column displays the running time of the process, presented in the format of hours, minutes, and seconds.

Use Systemd tools

Systemd is a system and service manager that provides a variety of tools to monitor and control processes running on the system.

You can use systemctl to view details of a specific service, including the running time, by running the following command:

systemctl status example-service
Copy after login

The output contains the details of the service, including the running time:

● example-service.service - Example Service
 Loaded: loaded (/etc/systemd/system/example-service.service; enabled; vendor preset: enabled)
 Active: active (running) since Mon 2024-03-01 12:00:00 UTC; 2h 30min ago
 Main PID: 1234 (example-process)
Tasks: 1 (limit: 4915)
 Memory: 10.5M
 CGroup: /system.slice/example-service.service
 └─1234 /usr/bin/example-process
Copy after login

In the output, the Active section shows the active status of the service and the time since it was started.

编写脚本自动化检查

使用Shell脚本

创建一个 Shell 脚本文件,比如 check_process_time.sh,并添加以下内容:

#!/bin/bash

# 设置要检查的进程名称
process_name="example-process"

# 获取进程运行时间
process_time=$(ps -eo cmd,etime | grep "$process_name" | awk '{print $NF}')

echo "Process $process_name has been running for: $process_time"
Copy after login

保存脚本文件后,赋予执行权限并运行:

chmod +x check_process_time.sh
./check_process_time.sh
Copy after login

脚本将输出指定进程的运行时间,方便用户定期执行以监控进程状态。

定时执行脚本

使用 cron 或 systemd 的定时任务功能,你可以定期执行上述脚本,以便及时了解进程的运行时间。

下面是使用 cron 的例子:

# 打开定时任务编辑器
crontab -e
Copy after login

添加以下行来每小时执行一次脚本:

0 * * * * /path/to/check_process_time.sh
Copy after login

保存并退出编辑器。

The above is the detailed content of How to check how long a process has been running in Linux. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:mryunwei.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template