


How to check how long a process has been running in Linux
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.
Use ps command
Theps
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
-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
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
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
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
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) └─...
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
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
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
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
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
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
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"
保存脚本文件后,赋予执行权限并运行:
chmod +x check_process_time.sh ./check_process_time.sh
脚本将输出指定进程的运行时间,方便用户定期执行以监控进程状态。
定时执行脚本
使用 cron
或 systemd
的定时任务功能,你可以定期执行上述脚本,以便及时了解进程的运行时间。
下面是使用 cron
的例子:
# 打开定时任务编辑器 crontab -e
添加以下行来每小时执行一次脚本:
0 * * * * /path/to/check_process_time.sh
保存并退出编辑器。
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to execute .sh file in Linux system? In Linux systems, a .sh file is a file called a Shell script, which is used to execute a series of commands. Executing .sh files is a very common operation. This article will introduce how to execute .sh files in Linux systems and provide specific code examples. Method 1: Use an absolute path to execute a .sh file. To execute a .sh file in a Linux system, you can use an absolute path to specify the location of the file. The following are the specific steps: Open the terminal

Detailed explanation of the Linux process priority adjustment method. In the Linux system, the priority of a process determines its execution order and resource allocation in the system. Reasonably adjusting the priority of the process can improve the performance and efficiency of the system. This article will introduce in detail how to adjust the priority of the process in Linux and provide specific code examples. 1. Overview of process priority In the Linux system, each process has a priority associated with it. The priority range is generally -20 to 19, where -20 represents the highest priority and 19 represents

Do you know how to run m files in matlab? Below, the editor will bring you a tutorial on how to run m files in matlab. I hope it will be helpful to you. Let’s learn with the editor! 1. First open the matlab software and select the upper left "Open" the corner, as shown in the picture below. 2. Then select the m file to be run and open it, as shown in the figure below. 3. Press F5 in the window to run the program, as shown in the figure below. 4. We can view the running results in the command line window and workspace, as shown in the figure below. 5. You can also run the file by clicking "Run" directly, as shown in the figure below. 6. Finally, you can view the running results of the m file in the command line window and workspace, as shown in the figure below. The above is the matlab method that the editor brought to you

1. Open the software and import a piece of material, as shown in the picture below. 2. Then ctrl+shift+u to remove color. 3. Then press ctrl+J to copy the layer. 4. Then reverse ctrl+I, and then set the layer blending mode to Color Dodge. 5. Click Filter--Others--Minimum. 6. In the pop-up dialog box, set the radius to 2 and click OK. 7. Finally, you can see the line draft extracted.

We've designed this Windows PowerShell scripting tutorial for beginners, whether you're a tech enthusiast or a professional looking to improve your scripting skills. If you have no prior knowledge of PowerShell scripting, this article will start with the basics and be tailored for you. We'll help you master the installation steps for a PowerShell environment and walk you through the main concepts and features of PowerShell scripts. If you're ready to learn more about PowerShell scripting, let's embark on this exciting learning journey together! What is WindowsPowerShell? PowerShell is a hybrid command system developed by Microsoft

Why do processes in Linux sleep? In the Linux operating system, a process can become dormant due to a number of different reasons and conditions. When a process is in a dormant state, it means that the process is temporarily suspended and cannot continue execution until certain conditions are met before it can be awakened to continue execution. Next, we will introduce in detail several common situations when a process enters hibernation in Linux, and illustrate them with specific code examples. Waiting for I/O to complete: When a process initiates an I/O operation (such as reading

Methods for using URL files to open Internet resources include: double-clicking to open using a web browser. Open it with a text editor, copy the link address and paste it into the browser address bar. Through the command line, use the "start" or "open" command to specify the URL file path. Create a script file that contains the command to open the URL file.

1. Ctrl + N: Create a new document. 2. Ctrl + O: Open a file. 3. Ctrl + S: Save the current file. 4. Ctrl + Shift + S: Save as. 5. Ctrl + W: Close the current document. 6. Ctrl + Q: Exit Photoshop. 7. Ctrl + Z: Undo. 8. Ctrl + Y: Redo. 9. Ctrl + X: Cut the selected content. 10. Ctrl + C: Copy the selected content.
