Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 711867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:47:03+00:00 2026-05-14T04:47:03+00:00

How to retrieve the process start time (or uptime) in python in Linux? I

  • 0

How to retrieve the process start time (or uptime) in python in Linux?

I only know, I can call “ps -p my_process_id -f” and then parse the output. But it is not cool.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:47:03+00:00Added an answer on May 14, 2026 at 4:47 am

    If you are doing it from within the python program you’re trying to measure, you could do something like this:

    import time
    # at the beginning of the script
    startTime = time.time()
    # ...
    def getUptime():
        """
        Returns the number of seconds since the program started.
        """
        # do return startTime if you just want the process start time
        return time.time() - startTime
    

    Otherwise, you have no choice but to parse ps or go into /proc/pid. A nice bashy way of getting the elapsed time is:

    ps -eo pid,etime | grep $YOUR_PID | awk '{print $2}'
    

    This will only print the elapsed time in the following format, so it should be quite easy to parse:

    days-HH:MM:SS
    

    (if it’s been running for less than a day, it’s just HH:MM:SS)

    The start time is available like this:

    ps -eo pid,stime | grep $YOUR_PID | awk '{print $2}'
    

    Unfortunately, if your process didn’t start today, this will only give you the date that it started, rather than the time.

    The best way of doing this is to get the elapsed time and the current time and just do a bit of math. The following is a python script that takes a PID as an argument and does the above for you, printing out the start date and time of the process:

    import sys
    import datetime
    import time
    import subprocess
    
    # call like this: python startTime.py $PID
    
    pid = sys.argv[1]
    proc = subprocess.Popen(['ps','-eo','pid,etime'], stdout=subprocess.PIPE)
    # get data from stdout
    proc.wait()
    results = proc.stdout.readlines()
    # parse data (should only be one)
    for result in results:
        try:
            result.strip()
            if result.split()[0] == pid:
                pidInfo = result.split()[1]
                # stop after the first one we find
                break
        except IndexError:
            pass # ignore it
    else:
        # didn't find one
        print "Process PID", pid, "doesn't seem to exist!"
        sys.exit(0)
    pidInfo = [result.split()[1] for result in results
               if result.split()[0] == pid][0]
    pidInfo = pidInfo.partition("-")
    if pidInfo[1] == '-':
        # there is a day
        days = int(pidInfo[0])
        rest = pidInfo[2].split(":")
        hours = int(rest[0])
        minutes = int(rest[1])
        seconds = int(rest[2])
    else:
        days = 0
        rest = pidInfo[0].split(":")
        if len(rest) == 3:
            hours = int(rest[0])
            minutes = int(rest[1])
            seconds = int(rest[2])
        elif len(rest) == 2:
            hours = 0
            minutes = int(rest[0])
            seconds = int(rest[1])
        else:
            hours = 0
            minutes = 0
            seconds = int(rest[0])
    
    # get the start time
    secondsSinceStart = days*24*3600 + hours*3600 + minutes*60 + seconds
    # unix time (in seconds) of start
    startTime = time.time() - secondsSinceStart
    # final result
    print "Process started on",
    print datetime.datetime.fromtimestamp(startTime).strftime("%a %b %d at %I:%M:%S %p")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the Win Api function GetModuleBaseName to retrieve the process name from
I am using LinkedIn API. I have done all process to retrieve access token.
I want to retrieve the CPU usage and memory usage of each process running
I retrieve mail using net/pop , but I also need to parse through the
I need to execute a program and retrieve its stdout output in c++. I'd
I use this function below to process the array data I retrieve from my
How can I start a thread which will run in the background but the
I call CURL in the C# to retrieve data. The following is my code:
Retrieve code from json. C#code:- var collection = getsortcat.Select(x => new { idterm =
I retrieve XML data in Flex via HttpService. I have the resultFormat property on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.