Shell script gets the running time of the process _linux shell

Source: Internet
Author: User
Tags current time

In our system, I wrote a script to update some repository on a regular basis, but occasionally I encountered problems, such as when git pull might be stuck there (possibly due to a network problem at some point), which would hinder the next update.
So I thought, when I started this script, I would check it and kill it and its subprocess if the last script I ran was not finished and a certain time threshold was passed. Then I need to write a script that can query how much time (in second) a process has been running.
Note: This does not refer to the CPU time consumed by the process, which is the current time minus the time at which the process started, and this is the time period.

Originally, PS itself also provides the option to query, but more intuitive and image (such as 10:32), not seconds as a unit, is not convenient to use in the script directly. The commands for process time in PS are as follows:

Copy Code code as follows:

[Root@jay-linux jay]# ps-p 4260-o Pid,start_time,etime,comm
PID START ELAPSED COMMAND
4260 Apr18 16-08:57:25 gnome-session

The third column of 16-08:57:25 is the time the process runs: 16 days, 8 hours, 57 minutes and 25 seconds.

I myself based on some information in the/proc file system, the query process run time script is shared as follows:

Copy Code code as follows:

#!/bin/bash
function Show_elapsed_time ()
{
user_hz=$ (getconf clk_tck) #mostly it ' s on x86/x86_64
Pid=$1
jiffies=$ (cat/proc/$pid/stat | cut-d ""-f22)
sys_uptime=$ (Cat/proc/uptime | cut-d ""-F1)
last_time=$ ((${sys_uptime%.*}-$jiffies/$user _hz))
echo "The process $pid lasts for $last _time seconds."
}

If [$#-ge 1];then
For PID in $@
Todo
Show_elapsed_time $pid
Done
Fi

While read PID
Todo
Show_elapsed_time $pid
Done

The implementation process and results are as follows:
Copy Code code as follows:

[Root@jay-linux jay]#./get_process_time.sh 4260
The process #4260 lasts for 1415417 seconds.

Follow-up I write two more simple talk about/proc/stat,/proc/$pid/stat,/proc/uptime Bar.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.