search
HomeComputer TutorialsComputer KnowledgeHow 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.

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
  • -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!

Statement
This article is reproduced at:每日运维. If there is any infringement, please contact admin@php.cn delete
Download Hidester VPN/Proxy to Access Your Favorite Content - MiniToolDownload Hidester VPN/Proxy to Access Your Favorite Content - MiniToolApr 22, 2025 am 12:50 AM

Learn about Hidester VPN and Hidester proxy and download Hidester VPN for Windows, Mac, Android, and iOS to use this VPN service to view websites with no limit. For more useful free computer tools and troubleshooting tips, you may visit php.cn Softwa

Windows Keyboard Opening Shortcuts Instead of Typing [Fixed]Windows Keyboard Opening Shortcuts Instead of Typing [Fixed]Apr 22, 2025 am 12:48 AM

Have you ever encountered the trouble of “Windows keyboard opening shortcuts instead of typing”? In this post from php.cn, you will learn how to fix this issue.

Easy Ways to Add the Control Panel Icon to Desktop on Win 10 / 11Easy Ways to Add the Control Panel Icon to Desktop on Win 10 / 11Apr 22, 2025 am 12:46 AM

In this post, php.cn Software will introduce what Control Panel is and how to add the Control Panel icon to desktop on your Windows 10 or Windows 11 computer. You can also learn some related information about desktop icon settings.

Granblue Fantasy Relink Save File Location & Backup Save DataGranblue Fantasy Relink Save File Location & Backup Save DataApr 22, 2025 am 12:45 AM

If you play Granblue Fantasy: Relink on your PC, you may wonder where you can find its save file. In this post, php.cn introduces everything you want to know - Granblue Fantasy Relink save file location and how to back up the savegame of this game.

How to Fix Event ID 1104: The Security Log Is Now Full? - MiniToolHow to Fix Event ID 1104: The Security Log Is Now Full? - MiniToolApr 22, 2025 am 12:44 AM

Event Viewer keeps track of activity for better management. However, if the upper limit of the security log is reached, no more events can be logged. In this post on php.cn Website, we will show you how to deal with Event ID 1104 the security log is

Watch: How to Enable Secure Boot on Gigabyte Motherboard?Watch: How to Enable Secure Boot on Gigabyte Motherboard?Apr 22, 2025 am 12:43 AM

Secure Boot is a security standard that can prevent your computer from booting with untrustworthy software. Enabling it will add an extra layer of security to your device. In this post from php.cn Website, we will show you how to enable Secure Boot o

Windows 11 23H2 Release Date: September 26, 2023 - MiniToolWindows 11 23H2 Release Date: September 26, 2023 - MiniToolApr 22, 2025 am 12:42 AM

Coming to a new year, what Windows 11 users are looking forward to are not only the patch updates but also the annual major update for Windows 11. This post will talk about the Windows 11 23H2 release date. In addition, if you want to recover deleted

How to Turn off Bixby on Samsung Phone? See a Guide! - MiniToolHow to Turn off Bixby on Samsung Phone? See a Guide! - MiniToolApr 22, 2025 am 12:41 AM

Can you completely disable Bixby? How to turn off Bixby on Samsung phones? It is not hard to disable this voice assistant. In this post from php.cn, we will go to any length to help you find the method. Besides, a way to turn off “Hi, Bixby” is also

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools