V Lib
V Lib

Reputation: 11

How to get the time of a process created remotely via ssh?

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.

Upvotes: 1

Views: 593

Answers (3)

chown
chown

Reputation: 52778

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.

Upvotes: 1

phs
phs

Reputation: 11061

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)

Upvotes: 1

Manny D
Manny D

Reputation: 20744

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.

Upvotes: 2

Related Questions