如何获取通过ssh远程创建的进程的时间?

发布于 2024-12-08 21:40:05 字数 413 浏览 0 评论 0原文

我目前正在编写一个脚本,其目的是杀死运行时间超过阈值的进程。 “杀手”在一堆主机上运行,​​作业由服务器发送到每个主机。我的想法是使用“ps”来获取作业的运行时间,但它打印的只是

17433 17433 ? 00:00:00 python

无论我等待什么时间。

我试图找到一个简化的示例,以避免发布我编写的所有代码。我们将 S 称为服务器,将 H 称为主机。

如果我执行以下步骤:

1)从服务器ssh登录@H 2)python myscript.py(现已登录主机) 3) ps -U 登录(仍在主机上)

就时间而言,我得到的结果与上面 00:00:00 相同。

如何获得真实的执行时间?当我在我的机器上本地执行所有操作时,它工作得很好。

我非常感谢你的帮助。

五、

I am currently writing a script whose purpose is to kill a process whose running time exceeds a threshold. The "killer" is run on a bunch of hosts and the jobs are send by a server to each host. My idea was to use 'ps' to get the running time of the job but all it prints is

17433 17433 ? 00:00:00 python

whatever the time I wait.

I tried to find a simplified example to avoid posting all the code I wrote. Let's call S the server and H the host.

If I do the following steps:

1) ssh login@H from the server
2) python myscript.py (now logged on the host)
3) ps -U login (still on the host)

I get the same result than the one above 00:00:00 as far as the time is concerned.

How can I get the real execution time ? When I do everything locally on my machine, it works fine.

I thank you very much for your help.

V.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

¢蛋碎的人ぎ生 2024-12-15 21:40:05

或者,您可以查看 /var/run 中 pid 文件的创建情况,假设您的进程创建了一个 pid 文件,然后使用 find 命令查看它是否超过特定阈值:

find /var/run/ -name "myProcess.pid" -mtime +1d

如果文件名符合条件(最后修改时间),则将返回文件名至少 1 天前)。您可能还想检查以确保进程确实正在运行,因为它可能已经崩溃并留下了 pid。

Alternatively, you can look at the creation of the pid file in /var/run, assuming your process created one and use the find command to see if it exceeds a certain threshold:

find /var/run/ -name "myProcess.pid" -mtime +1d

This will return the filename if it meets the criteria (last modified at least 1 day ago). You probably also want to check to make sure the process is actually running as it may have crashed and left the pid behind.

小猫一只 2024-12-15 21:40:05

如果您想知道该进程已经存在多长时间,您可以尝试

stat -c %Y /proc/`pgrep python`

..这将在纪元时间内将其返回给您。如果您想一次性杀死,我建议使用上面提到的查找(但也许将其指向 /proc

If you want how long the process has been alive, you could try

stat -c %Y /proc/`pgrep python`

..which will give it back to you in epoch time. If alternately you want the kill in one go, I suggest using the find mentioned above (but perhaps point it at /proc)

农村范ル 2024-12-15 21:40:05

试试这个:

ps kstart_time -ef | grep myProc.py | awk '{print $5}'

这将显示进程的开始日期/时间myProc.py

[ 19:27 root@host ~ ]# ps kstart_time -ef | grep "httpd\|STIME" | awk '{print $5}'
STIME
19:25

另一个选项是etime

etime 是自进程启动以来经过的时间,格式为dd-hh:mm:ss。 dd 是天数; hh,小时数; mm,分钟数; ss,秒数。

[ 19:47 root@host ~ ]# ps -eo cmd,etime
CMD        ELAPSED
/bin/sh    2-16:04:45

还有另一种方法:

获取进程 pid 并读取 /proc 中相应子目录中的时间戳。

首先,使用 ps 命令(ps -efps aux)获取进程 pid

然后,使用 ls 命令显示创建时间戳目录的。

[ 19:57 root@host ~ ]# ls -ld /proc/1218
dr-xr-xr-x 5 jon jon 0 Sep 20 16:14 /proc/1218

从时间戳可以看出进程 1218 是在 9 月 20 日 16:14 开始执行的。

Try this out:

ps kstart_time -ef | grep myProc.py | awk '{print $5}'

This will show the start date/time of the proccess myProc.py:

[ 19:27 root@host ~ ]# ps kstart_time -ef | grep "httpd\|STIME" | awk '{print $5}'
STIME
19:25

Another option is etime.

etime is the elapsed time since the process was started, in the form dd-hh:mm:ss. dd is the number of days; hh, the number of hours; mm, the number of minutes; ss, the number of seconds.

[ 19:47 root@host ~ ]# ps -eo cmd,etime
CMD        ELAPSED
/bin/sh    2-16:04:45

And yet another way to do this:

Get the process pid and read off the timestamp in the corresponding subdirectory in /proc.

First, get the process pid using the ps command (ps -ef or ps aux)

Then, use the ls command to display the creation timestamp of the directory.

[ 19:57 root@host ~ ]# ls -ld /proc/1218
dr-xr-xr-x 5 jon jon 0 Sep 20 16:14 /proc/1218

You can tell from the timestamp that the process 1218 began executing on Sept 20, 16:14.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文