How to get the start time of a long-running Linux process?
You can specify a formatter and use lstart
, like this command:
ps -eo pid,lstart,cmd
The above command will output all processes, with formatters to get PID, command run, and date+time started.
Example (from Debian/Jessie command line)
$ ps -eo pid,lstart,cmd
PID CMD STARTED
1 Tue Jun 7 01:29:38 2016 /sbin/init
2 Tue Jun 7 01:29:38 2016 [kthreadd]
3 Tue Jun 7 01:29:38 2016 [ksoftirqd/0]
5 Tue Jun 7 01:29:38 2016 [kworker/0:0H]
7 Tue Jun 7 01:29:38 2016 [rcu_sched]
8 Tue Jun 7 01:29:38 2016 [rcu_bh]
9 Tue Jun 7 01:29:38 2016 [migration/0]
10 Tue Jun 7 01:29:38 2016 [kdevtmpfs]
11 Tue Jun 7 01:29:38 2016 [netns]
277 Tue Jun 7 01:29:38 2016 [writeback]
279 Tue Jun 7 01:29:38 2016 [crypto]
...
You can read ps
's manpage or check Opengroup's page for the other formatters.
How to find out how long a screen command has been running on Linux?
- Hi cannot comment (yet) so ill try to answer here.
- You can find the process id of the screen command that running by using
pgrep screen
. (process grep screen).- if you can't find the screen process by this command, then you can use
top
command to see all the process that running and look for screen process with his own PID (process id).
- if you can't find the screen process by this command, then you can use
- The output should be a number within few digits.
- Then you can run the command
ps -o etime= -p "PID"
to see how much time the process is running.- change PID with the process id you have got from
pgrep screen
command. - The output should be the elapsed time in the format
[[dd-]hh:]mm:ss
.
- change PID with the process id you have got from
- If you want to terminate the process you can execute the command
kill PID
.- change the PID with the process id that you want to terminate.
- Another method that i prefer, because she is more readable is:
ps -o stime,time PID
- the output should be something like that:
- STIME TIME
- 15:52 00:21:45
How to get year also in the startup process when doing ps -ef
Use the formatted output (-o
option) of ps command and choose the format. For example:
$ ps -eo pid,ppid,lstart,cmd
PID PPID STARTED COMMAND
1 0 Mon Jan 11 12:26:03 2021 systemd
2 0 Mon Jan 11 12:26:03 2021 kthreadd
3 2 Mon Jan 11 12:26:03 2021 rcu_gp
4 2 Mon Jan 11 12:26:03 2021 rcu_par_gp
5 2 Mon Jan 11 12:26:03 2021 kworker/0:0-events
6 2 Mon Jan 11 12:26:03 2021 kworker/0:0H-kblockd
9 2 Mon Jan 11 12:26:03 2021 mm_percpu_wq
[...]
Available formats are described in the "STANDARD FORMAT SPECIFIERS" section of the manual.
fetching individual running process Start-Time
loggin of individual process time can be done by this way:
public static long getStartTime(final int pid) throws
IOException {
final long SYSTEM_CLK_TCK= 100; //needed as value in /proc/[PID]/stat file driectory is in clock ticks,100 is used to convert clock ticks to secs
final int fieldStartTime = 20; //column 20 of the /proc/[PID]/stat file driectory
try {
System.out.println("******String path******"+ path);
stat = reader.readLine();
System.out.println("******String stat******"+ stat);
} finally {
reader.close();
}
try {
final String[] fields = stat.substring(stat.lastIndexOf(field2End)).split(fieldSep);
final long startTime = Long.parseLong(fields[fieldStartTime]);
System.out.println("******fieldstarttime based on clock ticks******"+ startTime);
return startTime * msInSec / SYSTEM_CLK_TCK;
}
How to determine the date-and-time that a Linux process was started?
I have a project on github that does this in perl. You can find it here:
https://github.com/cormander/psj
The code you're wanting is in lib/Proc/PID.pm
, and here is the snippit (with comments removed):
use POSIX qw(ceil sysconf _SC_CLK_TCK);
sub _start_time {
my $pid = shift->pid;
my $tickspersec = sysconf(_SC_CLK_TCK);
my ($secs_since_boot) = split /\./, file_read("/proc/uptime");
$secs_since_boot *= $tickspersec;
my $start_time = (split / /, file_read("/proc/$pid/stat"))[21];
return ceil(time() - (($secs_since_boot - $start_time) / $tickspersec));
}
Beware the non-standard code function file_read
in here, but that should be pretty straight forward.
Find Elapsed Time or Duration of Long Running jobs without using sleep command
Code
#!/bin/bash
array=()
function dump () {
timestamp=$(date +%s)
object=${1}
action=${2}
if [ "${action}" == "start" ]; then
array[object]=${timestamp}
elif [ "${action}" == "end" ]; then
spent=$(( ${timestamp} - ${array[${object}]} ))
echo "Time report for ${object}"
echo "Started: $(date -d @${array[${object}]})"
echo "Ended: $(date -d @${timestamp})"
printf "Spent: %dh:%dm:%ds\n" $((${spent}/3600)) $((${spent}%3600/60)) $((${spent}%60))
fi
}
# MAIN
dump "iter1" "start"
echo "iter1 started"
sleep 2
dump "iter1" "end"
echo "iter2 started"
dump "iter2" "start"
echo "iter3 started"
dump "iter3" "start"
sleep 2
dump "iter2" "end"
sleep 2
dump "iter3" "end"
Output
iter1 started
Time report for iter1
Started: Wed Mar 20 11:27:17 UTC 2019
Ended: Wed Mar 20 11:27:19 UTC 2019
Spent: 0h:0m:2s
iter2 started
iter3 started
Time report for iter2
Started: Wed Mar 20 11:27:19 UTC 2019
Ended: Wed Mar 20 11:27:21 UTC 2019
Spent: 0h:0m:2s
Time report for iter3
Started: Wed Mar 20 11:27:19 UTC 2019
Ended: Wed Mar 20 11:27:23 UTC 2019
Spent: 0h:0m:4s
Related Topics
Number of Executed Instructions Different for Hello World Program Nasm Assembly and C
Running Shell Script in Parallel
Sending Udp Packets from the Linux Kernel
What Is Path //, How Is It Different from /
Create Zip File and Ignore Directory Structure
What Are the Rules for Valid Identifiers (E.G. Functions, Vars, etc) in Bash
How to Recall the Argument of the Previous Bash Command
Bash - How to Pipe Result from the Which Command to Cd
How to Configure Linux Capabilities Per User
Parallel Download Using Curl Command Line Utility
What Does "-Sh: Executable_Path:Not Found" Mean
Negate If Condition in Bash Script
Joining Multiple Fields in Text Files on Unix
Should I Use Libc++ or Libstdc++
Docker: Are Docker Links Deprecated
How to Exclude Directories from Grep -R
Finding Docker Container Processes? (From Host Point of View)