如何获取通过ssh远程创建的进程的时间?
我目前正在编写一个脚本,其目的是杀死运行时间超过阈值的进程。 “杀手”在一堆主机上运行,作业由服务器发送到每个主机。我的想法是使用“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
或者,您可以查看 /var/run 中 pid 文件的创建情况,假设您的进程创建了一个 pid 文件,然后使用 find 命令查看它是否超过特定阈值:
如果文件名符合条件(最后修改时间),则将返回文件名至少 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:
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.
如果您想知道该进程已经存在多长时间,您可以尝试
..这将在纪元时间内将其返回给您。如果您想一次性杀死,我建议使用上面提到的查找(但也许将其指向
/proc
)If you want how long the process has been alive, you could try
..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
)试试这个:
这将显示进程的开始日期/时间
myProc.py
:另一个选项是
etime
。etime
是自进程启动以来经过的时间,格式为dd-hh:mm:ss
。 dd 是天数; hh,小时数; mm,分钟数; ss,秒数。还有另一种方法:
获取进程 pid 并读取
/proc
中相应子目录中的时间戳。首先,使用 ps 命令(
ps -ef
或ps aux
)获取进程 pid然后,使用
ls
命令显示创建时间戳目录的。从时间戳可以看出进程 1218 是在 9 月 20 日 16:14 开始执行的。
Try this out:
This will show the start date/time of the proccess
myProc.py
:Another option is
etime
.etime
is the elapsed time since the process was started, in the formdd-hh:mm:ss
. dd is the number of days; hh, the number of hours; mm, the number of minutes; ss, the number of seconds.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
orps aux
)Then, use the
ls
command to display the creation timestamp of the directory.You can tell from the timestamp that the process 1218 began executing on Sept 20, 16:14.