乐闻世界logo
搜索文章和话题

How to get the start time of a long-running Linux process?

1个答案

1

In Linux systems, there are multiple approaches to retrieve the start time of a long-running process. Below are several commonly used methods:

1. Using the ps Command

The ps command is one of the most straightforward methods for displaying information about currently running processes. To obtain a process's start time, you can use the ps command with the -eo option, which enables customization of the output format:

bash
ps -eo pid,comm,lstart,etime | grep "process name or PID"
  • pid represents the process ID.
  • comm represents the command name.
  • lstart displays the process start time.
  • etime represents the elapsed time since the process began.
  • You can also use grep to filter the output, showing only details for a specific process.

For example, to find the start time of the nginx process, you can use:

bash
ps -eo pid,comm,lstart,etime | grep nginx

2. Using the proc File System

In Linux, each running process has a subdirectory named by its PID under the /proc directory. You can examine the stat file within this directory to retrieve detailed process information, including its start time.

bash
cat /proc/[PID]/stat

In the stat file, the 22nd field (counting from 0) indicates the process start time, measured in seconds since system boot (typically in ticks). To convert this to a real date and time, additional calculations are often required, involving the system boot time and the length of a time tick.

3. Using systemd

If the system uses systemd as the init system, you can employ the systemctl command to view the start time of a service, which applies to services managed by systemd:

bash
systemctl status "service name"

This provides information about the service, including the precise time it was loaded.

Example Demonstration

For instance, to determine the start time of the sshd service running on the system, you might first use ps to locate the PID, then inspect the /proc/[PID]/stat file, or directly run systemctl status sshd (if sshd is managed as a systemd service).

These methods effectively help you identify the start time of a specific process within a Linux system.

2024年8月14日 18:09 回复

你的答案