Get process elapsed time in seconds2019 Community Moderator ElectionHow to check how long a process has been running?How do I tell if a folder is actually a symlink and how do I fix it if it's broken?Read data from a pipe for a certain amount of time (in seconds)How can I conditionally pass a subshell through 'time'?Create tar files from shell script with current time stampHow to pass arguments to a Java process from within a shell scriptGet cpu usage average for the last 5 secondsPerform command every X secondsRun command after a certain length of time has elapsed?Printing epoch time with the date utilityAdding datetime column with time in seconds column of a csv file using shell

What Happens when Passenger Refuses to Fly Boeing 737 Max?

What's the "normal" opposite of flautando?

Should QA ask requirements to developers?

Do any of the books contain (magic) items for animal companions?

Is "history" a male-biased word ("his+story")?

How to pass a string to a command that expects a file?

Bash script should only kill those instances of another script's that it has launched

Why doesn't this Google Translate ad use the word "Translation" instead of "Translate"?

Is there an equal sign with wider gap?

Does splitting a potentially monolithic application into several smaller ones help prevent bugs?

Meaning of ちはース

Word for a person who has no opinion about whether god exists

Grey hair or white hair

Aliens englobed the Solar System: will we notice?

Force user to remove USB token

Can Mathematica be used to create an Artistic 3D extrusion from a 2D image and wrap a line pattern around it?

Unreachable code, but reachable with exception

GPLv2 - licensing for commercial use

What to do when during a meeting client people start to fight (even physically) with each others?

Virginia employer terminated employee and wants signing bonus returned

Making a sword in the stone, in a medieval world without magic

How can The Temple of Elementary Evil reliably protect itself against kinetic bombardment?

Leftbar without indentation

Are terms "stab" and "staccato" synonyms?



Get process elapsed time in seconds



2019 Community Moderator ElectionHow to check how long a process has been running?How do I tell if a folder is actually a symlink and how do I fix it if it's broken?Read data from a pipe for a certain amount of time (in seconds)How can I conditionally pass a subshell through 'time'?Create tar files from shell script with current time stampHow to pass arguments to a Java process from within a shell scriptGet cpu usage average for the last 5 secondsPerform command every X secondsRun command after a certain length of time has elapsed?Printing epoch time with the date utilityAdding datetime column with time in seconds column of a csv file using shell










2















Similar to How to check how long a process has been running?, I am trying to get the elapsed time for a process in seconds in an embedded Linux system using BusyBox and sh (not bash). The difference is that I would like it in pure seconds, not mm:ss format.



I can parse it out of the result of ps but am unable to get expr to convert from mm:ss to seconds. Using set -vx to help with debugging I can see the valid expr command but it fails with a syntax error. Copying and pasting it works fine.



After running this for added diagnostics:



root@embedded:~# set -vx


I enter:



root@embedded:~# $(/opt/bin/busybox ps -o pid,etime,time | grep 1156 | sed "s/ *[0-9]+ +([0-9]+):([0-9]+) .*//opt/bin/busybox expr 1 \* 60 + 2/")


And get this response:



$(/opt/bin/busybox ps -o pid,etime,time | grep 1156 | sed "s/ *[0-9]+ +([0-9]+):([0-9]+) .*//opt/bin/busybox expr 1 \* 60 + 2/")
+ /opt/bin/busybox ps -o pid,etime,time
+ grep 1156
+ sed s/ *[0-9]+ +([0-9]+):([0-9]+) .*//opt/bin/busybox expr 1 \* 60 + 2/
+ /opt/bin/busybox expr 2 * 60 + 03
expr: syntax error
root@embedded:~#


I have additionally tried multiple layers of escapes and also ''.



What do I need to do to get the escaping of the * to pass through to the expr command line?










share|improve this question
























  • it looks to me like you have an extra "busybox" call in there; try removing the busybox part before the expr call.

    – Jeff Schaller
    Jun 8 '15 at 13:26











  • This is being developed in test code where there are two copies of BusyBox. I am explicitly using the testing version for ps because the system version didn't include support for -o. In the second case I was just trying it out to see if there was a difference between the two binaries for expr as well.

    – altendky
    Jun 8 '15 at 16:22











  • Please post just your code, I'm not sure if I understand what bits are your code and what bits are traces and prompts and so on.

    – Gilles
    Jun 8 '15 at 22:42











  • @Gilles, I agree. I'll neaten it up when I get back on that system.

    – altendky
    Jun 9 '15 at 11:13















2















Similar to How to check how long a process has been running?, I am trying to get the elapsed time for a process in seconds in an embedded Linux system using BusyBox and sh (not bash). The difference is that I would like it in pure seconds, not mm:ss format.



I can parse it out of the result of ps but am unable to get expr to convert from mm:ss to seconds. Using set -vx to help with debugging I can see the valid expr command but it fails with a syntax error. Copying and pasting it works fine.



After running this for added diagnostics:



root@embedded:~# set -vx


I enter:



root@embedded:~# $(/opt/bin/busybox ps -o pid,etime,time | grep 1156 | sed "s/ *[0-9]+ +([0-9]+):([0-9]+) .*//opt/bin/busybox expr 1 \* 60 + 2/")


And get this response:



$(/opt/bin/busybox ps -o pid,etime,time | grep 1156 | sed "s/ *[0-9]+ +([0-9]+):([0-9]+) .*//opt/bin/busybox expr 1 \* 60 + 2/")
+ /opt/bin/busybox ps -o pid,etime,time
+ grep 1156
+ sed s/ *[0-9]+ +([0-9]+):([0-9]+) .*//opt/bin/busybox expr 1 \* 60 + 2/
+ /opt/bin/busybox expr 2 * 60 + 03
expr: syntax error
root@embedded:~#


I have additionally tried multiple layers of escapes and also ''.



What do I need to do to get the escaping of the * to pass through to the expr command line?










share|improve this question
























  • it looks to me like you have an extra "busybox" call in there; try removing the busybox part before the expr call.

    – Jeff Schaller
    Jun 8 '15 at 13:26











  • This is being developed in test code where there are two copies of BusyBox. I am explicitly using the testing version for ps because the system version didn't include support for -o. In the second case I was just trying it out to see if there was a difference between the two binaries for expr as well.

    – altendky
    Jun 8 '15 at 16:22











  • Please post just your code, I'm not sure if I understand what bits are your code and what bits are traces and prompts and so on.

    – Gilles
    Jun 8 '15 at 22:42











  • @Gilles, I agree. I'll neaten it up when I get back on that system.

    – altendky
    Jun 9 '15 at 11:13













2












2








2


1






Similar to How to check how long a process has been running?, I am trying to get the elapsed time for a process in seconds in an embedded Linux system using BusyBox and sh (not bash). The difference is that I would like it in pure seconds, not mm:ss format.



I can parse it out of the result of ps but am unable to get expr to convert from mm:ss to seconds. Using set -vx to help with debugging I can see the valid expr command but it fails with a syntax error. Copying and pasting it works fine.



After running this for added diagnostics:



root@embedded:~# set -vx


I enter:



root@embedded:~# $(/opt/bin/busybox ps -o pid,etime,time | grep 1156 | sed "s/ *[0-9]+ +([0-9]+):([0-9]+) .*//opt/bin/busybox expr 1 \* 60 + 2/")


And get this response:



$(/opt/bin/busybox ps -o pid,etime,time | grep 1156 | sed "s/ *[0-9]+ +([0-9]+):([0-9]+) .*//opt/bin/busybox expr 1 \* 60 + 2/")
+ /opt/bin/busybox ps -o pid,etime,time
+ grep 1156
+ sed s/ *[0-9]+ +([0-9]+):([0-9]+) .*//opt/bin/busybox expr 1 \* 60 + 2/
+ /opt/bin/busybox expr 2 * 60 + 03
expr: syntax error
root@embedded:~#


I have additionally tried multiple layers of escapes and also ''.



What do I need to do to get the escaping of the * to pass through to the expr command line?










share|improve this question
















Similar to How to check how long a process has been running?, I am trying to get the elapsed time for a process in seconds in an embedded Linux system using BusyBox and sh (not bash). The difference is that I would like it in pure seconds, not mm:ss format.



I can parse it out of the result of ps but am unable to get expr to convert from mm:ss to seconds. Using set -vx to help with debugging I can see the valid expr command but it fails with a syntax error. Copying and pasting it works fine.



After running this for added diagnostics:



root@embedded:~# set -vx


I enter:



root@embedded:~# $(/opt/bin/busybox ps -o pid,etime,time | grep 1156 | sed "s/ *[0-9]+ +([0-9]+):([0-9]+) .*//opt/bin/busybox expr 1 \* 60 + 2/")


And get this response:



$(/opt/bin/busybox ps -o pid,etime,time | grep 1156 | sed "s/ *[0-9]+ +([0-9]+):([0-9]+) .*//opt/bin/busybox expr 1 \* 60 + 2/")
+ /opt/bin/busybox ps -o pid,etime,time
+ grep 1156
+ sed s/ *[0-9]+ +([0-9]+):([0-9]+) .*//opt/bin/busybox expr 1 \* 60 + 2/
+ /opt/bin/busybox expr 2 * 60 + 03
expr: syntax error
root@embedded:~#


I have additionally tried multiple layers of escapes and also ''.



What do I need to do to get the escaping of the * to pass through to the expr command line?







shell quoting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 13 '17 at 12:36









Community

1




1










asked Jun 8 '15 at 12:10









altendkyaltendky

12115




12115












  • it looks to me like you have an extra "busybox" call in there; try removing the busybox part before the expr call.

    – Jeff Schaller
    Jun 8 '15 at 13:26











  • This is being developed in test code where there are two copies of BusyBox. I am explicitly using the testing version for ps because the system version didn't include support for -o. In the second case I was just trying it out to see if there was a difference between the two binaries for expr as well.

    – altendky
    Jun 8 '15 at 16:22











  • Please post just your code, I'm not sure if I understand what bits are your code and what bits are traces and prompts and so on.

    – Gilles
    Jun 8 '15 at 22:42











  • @Gilles, I agree. I'll neaten it up when I get back on that system.

    – altendky
    Jun 9 '15 at 11:13

















  • it looks to me like you have an extra "busybox" call in there; try removing the busybox part before the expr call.

    – Jeff Schaller
    Jun 8 '15 at 13:26











  • This is being developed in test code where there are two copies of BusyBox. I am explicitly using the testing version for ps because the system version didn't include support for -o. In the second case I was just trying it out to see if there was a difference between the two binaries for expr as well.

    – altendky
    Jun 8 '15 at 16:22











  • Please post just your code, I'm not sure if I understand what bits are your code and what bits are traces and prompts and so on.

    – Gilles
    Jun 8 '15 at 22:42











  • @Gilles, I agree. I'll neaten it up when I get back on that system.

    – altendky
    Jun 9 '15 at 11:13
















it looks to me like you have an extra "busybox" call in there; try removing the busybox part before the expr call.

– Jeff Schaller
Jun 8 '15 at 13:26





it looks to me like you have an extra "busybox" call in there; try removing the busybox part before the expr call.

– Jeff Schaller
Jun 8 '15 at 13:26













This is being developed in test code where there are two copies of BusyBox. I am explicitly using the testing version for ps because the system version didn't include support for -o. In the second case I was just trying it out to see if there was a difference between the two binaries for expr as well.

– altendky
Jun 8 '15 at 16:22





This is being developed in test code where there are two copies of BusyBox. I am explicitly using the testing version for ps because the system version didn't include support for -o. In the second case I was just trying it out to see if there was a difference between the two binaries for expr as well.

– altendky
Jun 8 '15 at 16:22













Please post just your code, I'm not sure if I understand what bits are your code and what bits are traces and prompts and so on.

– Gilles
Jun 8 '15 at 22:42





Please post just your code, I'm not sure if I understand what bits are your code and what bits are traces and prompts and so on.

– Gilles
Jun 8 '15 at 22:42













@Gilles, I agree. I'll neaten it up when I get back on that system.

– altendky
Jun 9 '15 at 11:13





@Gilles, I agree. I'll neaten it up when I get back on that system.

– altendky
Jun 9 '15 at 11:13










5 Answers
5






active

oldest

votes


















5














eval might help this case...



~ $ $(echo expr 1 \* 2)
+ echo expr 1 * 2
+ expr 1 * 2
expr: syntax error
~ $ eval $(echo expr 1 \* 2)
+ echo expr 1 * 2
+ eval expr 1 * 2
+ expr 1 * 2
2


But it might be better to look up /proc/$pid/stat on Linux.



pid=1155
hz=$(getconf CLK_TCK)
uptime=$(awk 'print $1' < /proc/uptime)
starttime=$(awk 'print $22' < /proc/$pid/stat)
echo $(( $uptime%.* - $starttime / $hz ))


If /usr/bin/getconf is unavailable, you need to find your system's CLK_TCK (or USER_HZ) value. I think you can assume it as 100 in most cases.






share|improve this answer




















  • 1





    +1 for the /proc solution. But don't use the expr method. That code is already too complex and fragile.

    – Mikel
    Jun 8 '15 at 13:25











  • Given that the only comments I've seen about ticks/jiffies are to avoid dealing with them unless you want to carefully maintain your code, I'm going to stay away from that approach (and no, I don't presently have getconf). Your eval did work but I now have it working without as well. :[ I swear I tried anywhere from 0-6 `s before the *` and it didn't work, but it now does with \*. Apparently I was debugging too many different avenues at once and ended up not having tested what I thought I had. Bugger and sorry.

    – altendky
    Jun 8 '15 at 17:02


















1














Embarrassingly, I seem to have failed to test as completely as I had thought. It turns out that modifying what I posted in my question to contain only two escapes (\*) makes it work fine.



root@embedded:~# $(/opt/bin/busybox ps -o pid,etime | sed -n "s/ *1155 +([0-9]+):([0-9]+).*/expr 1 \* 60 + 2/p")
$(/opt/bin/busybox ps -o pid,etime | sed -n "s/ *1155 +([0-9]+):([0-9]+).*/expr 1 \* 60 + 2/p")
+ /opt/bin/busybox ps -o pid,etime
+ sed -n s/ *1155 +([0-9]+):([0-9]+).*/expr 1 * 60 + 2/p
+ expr 293 * 60 + 54
17634


Note that I also integrated the grep into sed with the -n 's///p' approach (global no-print and then print on match).



Thanks to @yaegashi for getting me to try \ again...






share|improve this answer

























  • Nope, this fails. The difference was the existence of files in the directory or not. If there are no files, then * remains unsubstituted. Otherwise, it expands and messes up the expr.

    – altendky
    Jun 8 '15 at 17:49


















1














assumping process is 16752 (which you seems to be able to figure out)



expr $(date +%s) - $(stat -c %Y /proc/16752/environ )


where




  • date +%s is current date in second since the epoch


  • stat -c %Y /proc/16752/environ is "creation date" of /proc/16752/environ, that is the moment where proc #16752 was launched

edit:



  • maybe /proc/$PID/environ is the wrong pseudo file, /proc/$PID/exe should be used instead.


  • if time changed on host, both result from ps -p $PID -h -o etime and timestamp from /proc/$PID/exe are likely to be wrong. (unsure how to check that).






share|improve this answer

























  • This system set's it's time on startup from the application. Since this occurs between the process start and the checking of the process elapsed time, it is pretty much guaranteed to be wrong. Such timestamps are generally hazardous even when not guaranteed to be a problem. Thanks for taking the time to respond.

    – altendky
    Jun 8 '15 at 16:29











  • I am glad you found a solution, are you sure there is a difference between ps'etime and my solution ?

    – Archemar
    Jun 9 '15 at 3:57











  • The user (or application in my case) can change the system time between the process start and checking of elapsed time. You can try elapsed() expr $(date +%s) - $(stat -c %Y /proc/$MYPID/environ ); ; sleep 10 & export MYPID=$!; elapsed; sleep 1; elapsed; sudo date +%F %T -s "$(date -d "$(date +%F %T) 1 hour ago")"; elapsed. My result is 0, then 1, then -3599. Don't forget to reset your time, optionally like sudo rdate 0.us.pool.ntp.org (it takes a minute or two for me). Wall clock time is a hazardous way to keep track of durations and should be avoided when uptime is usable.

    – altendky
    Jun 9 '15 at 11:07


















0














@yaegashi had the approach right but my sed call seems to introduce the need for a third .



root@embedded:/data# set -vx
root@embedded:/data# echo Process Uptime: $(eval $(/opt/bin/busybox ps -o pid,etime | sed -n "s/ *$APP_PID +([0-9]+):([0-9]+).*/expr 1 \* 60 + 2/p"))
echo Process Uptime: $(eval $(/opt/bin/busybox ps -o pid,etime | sed -n "s/ *$APP_PID +([0-9]+):([0-9]+).*/expr 1 \* 60 + 2/p"))
+ /opt/bin/busybox ps -o pid,etime
+ sed -n s/ *1156 +([0-9]+):([0-9]+).*/expr 1 \* 60 + 2/p
+ eval expr 2 * 60 + 46
+ expr 2 * 60 + 46
+ echo Process Uptime: 166
Process Uptime: 166


Fingers crossed that this not only works now, but also continues to do so next time I use it...






share|improve this answer
































    0














    This is an awk script which will convert the 0-00:00:00 output to seconds. It is used like this...



    ps -eo pid,time,user,comm | awk '"$2"' | awk -f _os_return_process_time_in_seconds.awk


    Here is the script. Just wrote if for another purpose. May have issues still.



    BEGIN "


    "total_seconds"





    share|improve this answer








    New contributor




    Ethan Post is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.



















      Your Answer








      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "106"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f208242%2fget-process-elapsed-time-in-seconds%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      5 Answers
      5






      active

      oldest

      votes








      5 Answers
      5






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      5














      eval might help this case...



      ~ $ $(echo expr 1 \* 2)
      + echo expr 1 * 2
      + expr 1 * 2
      expr: syntax error
      ~ $ eval $(echo expr 1 \* 2)
      + echo expr 1 * 2
      + eval expr 1 * 2
      + expr 1 * 2
      2


      But it might be better to look up /proc/$pid/stat on Linux.



      pid=1155
      hz=$(getconf CLK_TCK)
      uptime=$(awk 'print $1' < /proc/uptime)
      starttime=$(awk 'print $22' < /proc/$pid/stat)
      echo $(( $uptime%.* - $starttime / $hz ))


      If /usr/bin/getconf is unavailable, you need to find your system's CLK_TCK (or USER_HZ) value. I think you can assume it as 100 in most cases.






      share|improve this answer




















      • 1





        +1 for the /proc solution. But don't use the expr method. That code is already too complex and fragile.

        – Mikel
        Jun 8 '15 at 13:25











      • Given that the only comments I've seen about ticks/jiffies are to avoid dealing with them unless you want to carefully maintain your code, I'm going to stay away from that approach (and no, I don't presently have getconf). Your eval did work but I now have it working without as well. :[ I swear I tried anywhere from 0-6 `s before the *` and it didn't work, but it now does with \*. Apparently I was debugging too many different avenues at once and ended up not having tested what I thought I had. Bugger and sorry.

        – altendky
        Jun 8 '15 at 17:02















      5














      eval might help this case...



      ~ $ $(echo expr 1 \* 2)
      + echo expr 1 * 2
      + expr 1 * 2
      expr: syntax error
      ~ $ eval $(echo expr 1 \* 2)
      + echo expr 1 * 2
      + eval expr 1 * 2
      + expr 1 * 2
      2


      But it might be better to look up /proc/$pid/stat on Linux.



      pid=1155
      hz=$(getconf CLK_TCK)
      uptime=$(awk 'print $1' < /proc/uptime)
      starttime=$(awk 'print $22' < /proc/$pid/stat)
      echo $(( $uptime%.* - $starttime / $hz ))


      If /usr/bin/getconf is unavailable, you need to find your system's CLK_TCK (or USER_HZ) value. I think you can assume it as 100 in most cases.






      share|improve this answer




















      • 1





        +1 for the /proc solution. But don't use the expr method. That code is already too complex and fragile.

        – Mikel
        Jun 8 '15 at 13:25











      • Given that the only comments I've seen about ticks/jiffies are to avoid dealing with them unless you want to carefully maintain your code, I'm going to stay away from that approach (and no, I don't presently have getconf). Your eval did work but I now have it working without as well. :[ I swear I tried anywhere from 0-6 `s before the *` and it didn't work, but it now does with \*. Apparently I was debugging too many different avenues at once and ended up not having tested what I thought I had. Bugger and sorry.

        – altendky
        Jun 8 '15 at 17:02













      5












      5








      5







      eval might help this case...



      ~ $ $(echo expr 1 \* 2)
      + echo expr 1 * 2
      + expr 1 * 2
      expr: syntax error
      ~ $ eval $(echo expr 1 \* 2)
      + echo expr 1 * 2
      + eval expr 1 * 2
      + expr 1 * 2
      2


      But it might be better to look up /proc/$pid/stat on Linux.



      pid=1155
      hz=$(getconf CLK_TCK)
      uptime=$(awk 'print $1' < /proc/uptime)
      starttime=$(awk 'print $22' < /proc/$pid/stat)
      echo $(( $uptime%.* - $starttime / $hz ))


      If /usr/bin/getconf is unavailable, you need to find your system's CLK_TCK (or USER_HZ) value. I think you can assume it as 100 in most cases.






      share|improve this answer















      eval might help this case...



      ~ $ $(echo expr 1 \* 2)
      + echo expr 1 * 2
      + expr 1 * 2
      expr: syntax error
      ~ $ eval $(echo expr 1 \* 2)
      + echo expr 1 * 2
      + eval expr 1 * 2
      + expr 1 * 2
      2


      But it might be better to look up /proc/$pid/stat on Linux.



      pid=1155
      hz=$(getconf CLK_TCK)
      uptime=$(awk 'print $1' < /proc/uptime)
      starttime=$(awk 'print $22' < /proc/$pid/stat)
      echo $(( $uptime%.* - $starttime / $hz ))


      If /usr/bin/getconf is unavailable, you need to find your system's CLK_TCK (or USER_HZ) value. I think you can assume it as 100 in most cases.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Jun 8 '15 at 13:28

























      answered Jun 8 '15 at 13:22









      yaegashiyaegashi

      8,45611734




      8,45611734







      • 1





        +1 for the /proc solution. But don't use the expr method. That code is already too complex and fragile.

        – Mikel
        Jun 8 '15 at 13:25











      • Given that the only comments I've seen about ticks/jiffies are to avoid dealing with them unless you want to carefully maintain your code, I'm going to stay away from that approach (and no, I don't presently have getconf). Your eval did work but I now have it working without as well. :[ I swear I tried anywhere from 0-6 `s before the *` and it didn't work, but it now does with \*. Apparently I was debugging too many different avenues at once and ended up not having tested what I thought I had. Bugger and sorry.

        – altendky
        Jun 8 '15 at 17:02












      • 1





        +1 for the /proc solution. But don't use the expr method. That code is already too complex and fragile.

        – Mikel
        Jun 8 '15 at 13:25











      • Given that the only comments I've seen about ticks/jiffies are to avoid dealing with them unless you want to carefully maintain your code, I'm going to stay away from that approach (and no, I don't presently have getconf). Your eval did work but I now have it working without as well. :[ I swear I tried anywhere from 0-6 `s before the *` and it didn't work, but it now does with \*. Apparently I was debugging too many different avenues at once and ended up not having tested what I thought I had. Bugger and sorry.

        – altendky
        Jun 8 '15 at 17:02







      1




      1





      +1 for the /proc solution. But don't use the expr method. That code is already too complex and fragile.

      – Mikel
      Jun 8 '15 at 13:25





      +1 for the /proc solution. But don't use the expr method. That code is already too complex and fragile.

      – Mikel
      Jun 8 '15 at 13:25













      Given that the only comments I've seen about ticks/jiffies are to avoid dealing with them unless you want to carefully maintain your code, I'm going to stay away from that approach (and no, I don't presently have getconf). Your eval did work but I now have it working without as well. :[ I swear I tried anywhere from 0-6 `s before the *` and it didn't work, but it now does with \*. Apparently I was debugging too many different avenues at once and ended up not having tested what I thought I had. Bugger and sorry.

      – altendky
      Jun 8 '15 at 17:02





      Given that the only comments I've seen about ticks/jiffies are to avoid dealing with them unless you want to carefully maintain your code, I'm going to stay away from that approach (and no, I don't presently have getconf). Your eval did work but I now have it working without as well. :[ I swear I tried anywhere from 0-6 `s before the *` and it didn't work, but it now does with \*. Apparently I was debugging too many different avenues at once and ended up not having tested what I thought I had. Bugger and sorry.

      – altendky
      Jun 8 '15 at 17:02













      1














      Embarrassingly, I seem to have failed to test as completely as I had thought. It turns out that modifying what I posted in my question to contain only two escapes (\*) makes it work fine.



      root@embedded:~# $(/opt/bin/busybox ps -o pid,etime | sed -n "s/ *1155 +([0-9]+):([0-9]+).*/expr 1 \* 60 + 2/p")
      $(/opt/bin/busybox ps -o pid,etime | sed -n "s/ *1155 +([0-9]+):([0-9]+).*/expr 1 \* 60 + 2/p")
      + /opt/bin/busybox ps -o pid,etime
      + sed -n s/ *1155 +([0-9]+):([0-9]+).*/expr 1 * 60 + 2/p
      + expr 293 * 60 + 54
      17634


      Note that I also integrated the grep into sed with the -n 's///p' approach (global no-print and then print on match).



      Thanks to @yaegashi for getting me to try \ again...






      share|improve this answer

























      • Nope, this fails. The difference was the existence of files in the directory or not. If there are no files, then * remains unsubstituted. Otherwise, it expands and messes up the expr.

        – altendky
        Jun 8 '15 at 17:49















      1














      Embarrassingly, I seem to have failed to test as completely as I had thought. It turns out that modifying what I posted in my question to contain only two escapes (\*) makes it work fine.



      root@embedded:~# $(/opt/bin/busybox ps -o pid,etime | sed -n "s/ *1155 +([0-9]+):([0-9]+).*/expr 1 \* 60 + 2/p")
      $(/opt/bin/busybox ps -o pid,etime | sed -n "s/ *1155 +([0-9]+):([0-9]+).*/expr 1 \* 60 + 2/p")
      + /opt/bin/busybox ps -o pid,etime
      + sed -n s/ *1155 +([0-9]+):([0-9]+).*/expr 1 * 60 + 2/p
      + expr 293 * 60 + 54
      17634


      Note that I also integrated the grep into sed with the -n 's///p' approach (global no-print and then print on match).



      Thanks to @yaegashi for getting me to try \ again...






      share|improve this answer

























      • Nope, this fails. The difference was the existence of files in the directory or not. If there are no files, then * remains unsubstituted. Otherwise, it expands and messes up the expr.

        – altendky
        Jun 8 '15 at 17:49













      1












      1








      1







      Embarrassingly, I seem to have failed to test as completely as I had thought. It turns out that modifying what I posted in my question to contain only two escapes (\*) makes it work fine.



      root@embedded:~# $(/opt/bin/busybox ps -o pid,etime | sed -n "s/ *1155 +([0-9]+):([0-9]+).*/expr 1 \* 60 + 2/p")
      $(/opt/bin/busybox ps -o pid,etime | sed -n "s/ *1155 +([0-9]+):([0-9]+).*/expr 1 \* 60 + 2/p")
      + /opt/bin/busybox ps -o pid,etime
      + sed -n s/ *1155 +([0-9]+):([0-9]+).*/expr 1 * 60 + 2/p
      + expr 293 * 60 + 54
      17634


      Note that I also integrated the grep into sed with the -n 's///p' approach (global no-print and then print on match).



      Thanks to @yaegashi for getting me to try \ again...






      share|improve this answer















      Embarrassingly, I seem to have failed to test as completely as I had thought. It turns out that modifying what I posted in my question to contain only two escapes (\*) makes it work fine.



      root@embedded:~# $(/opt/bin/busybox ps -o pid,etime | sed -n "s/ *1155 +([0-9]+):([0-9]+).*/expr 1 \* 60 + 2/p")
      $(/opt/bin/busybox ps -o pid,etime | sed -n "s/ *1155 +([0-9]+):([0-9]+).*/expr 1 \* 60 + 2/p")
      + /opt/bin/busybox ps -o pid,etime
      + sed -n s/ *1155 +([0-9]+):([0-9]+).*/expr 1 * 60 + 2/p
      + expr 293 * 60 + 54
      17634


      Note that I also integrated the grep into sed with the -n 's///p' approach (global no-print and then print on match).



      Thanks to @yaegashi for getting me to try \ again...







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Jun 8 '15 at 17:32

























      answered Jun 8 '15 at 17:11









      altendkyaltendky

      12115




      12115












      • Nope, this fails. The difference was the existence of files in the directory or not. If there are no files, then * remains unsubstituted. Otherwise, it expands and messes up the expr.

        – altendky
        Jun 8 '15 at 17:49

















      • Nope, this fails. The difference was the existence of files in the directory or not. If there are no files, then * remains unsubstituted. Otherwise, it expands and messes up the expr.

        – altendky
        Jun 8 '15 at 17:49
















      Nope, this fails. The difference was the existence of files in the directory or not. If there are no files, then * remains unsubstituted. Otherwise, it expands and messes up the expr.

      – altendky
      Jun 8 '15 at 17:49





      Nope, this fails. The difference was the existence of files in the directory or not. If there are no files, then * remains unsubstituted. Otherwise, it expands and messes up the expr.

      – altendky
      Jun 8 '15 at 17:49











      1














      assumping process is 16752 (which you seems to be able to figure out)



      expr $(date +%s) - $(stat -c %Y /proc/16752/environ )


      where




      • date +%s is current date in second since the epoch


      • stat -c %Y /proc/16752/environ is "creation date" of /proc/16752/environ, that is the moment where proc #16752 was launched

      edit:



      • maybe /proc/$PID/environ is the wrong pseudo file, /proc/$PID/exe should be used instead.


      • if time changed on host, both result from ps -p $PID -h -o etime and timestamp from /proc/$PID/exe are likely to be wrong. (unsure how to check that).






      share|improve this answer

























      • This system set's it's time on startup from the application. Since this occurs between the process start and the checking of the process elapsed time, it is pretty much guaranteed to be wrong. Such timestamps are generally hazardous even when not guaranteed to be a problem. Thanks for taking the time to respond.

        – altendky
        Jun 8 '15 at 16:29











      • I am glad you found a solution, are you sure there is a difference between ps'etime and my solution ?

        – Archemar
        Jun 9 '15 at 3:57











      • The user (or application in my case) can change the system time between the process start and checking of elapsed time. You can try elapsed() expr $(date +%s) - $(stat -c %Y /proc/$MYPID/environ ); ; sleep 10 & export MYPID=$!; elapsed; sleep 1; elapsed; sudo date +%F %T -s "$(date -d "$(date +%F %T) 1 hour ago")"; elapsed. My result is 0, then 1, then -3599. Don't forget to reset your time, optionally like sudo rdate 0.us.pool.ntp.org (it takes a minute or two for me). Wall clock time is a hazardous way to keep track of durations and should be avoided when uptime is usable.

        – altendky
        Jun 9 '15 at 11:07















      1














      assumping process is 16752 (which you seems to be able to figure out)



      expr $(date +%s) - $(stat -c %Y /proc/16752/environ )


      where




      • date +%s is current date in second since the epoch


      • stat -c %Y /proc/16752/environ is "creation date" of /proc/16752/environ, that is the moment where proc #16752 was launched

      edit:



      • maybe /proc/$PID/environ is the wrong pseudo file, /proc/$PID/exe should be used instead.


      • if time changed on host, both result from ps -p $PID -h -o etime and timestamp from /proc/$PID/exe are likely to be wrong. (unsure how to check that).






      share|improve this answer

























      • This system set's it's time on startup from the application. Since this occurs between the process start and the checking of the process elapsed time, it is pretty much guaranteed to be wrong. Such timestamps are generally hazardous even when not guaranteed to be a problem. Thanks for taking the time to respond.

        – altendky
        Jun 8 '15 at 16:29











      • I am glad you found a solution, are you sure there is a difference between ps'etime and my solution ?

        – Archemar
        Jun 9 '15 at 3:57











      • The user (or application in my case) can change the system time between the process start and checking of elapsed time. You can try elapsed() expr $(date +%s) - $(stat -c %Y /proc/$MYPID/environ ); ; sleep 10 & export MYPID=$!; elapsed; sleep 1; elapsed; sudo date +%F %T -s "$(date -d "$(date +%F %T) 1 hour ago")"; elapsed. My result is 0, then 1, then -3599. Don't forget to reset your time, optionally like sudo rdate 0.us.pool.ntp.org (it takes a minute or two for me). Wall clock time is a hazardous way to keep track of durations and should be avoided when uptime is usable.

        – altendky
        Jun 9 '15 at 11:07













      1












      1








      1







      assumping process is 16752 (which you seems to be able to figure out)



      expr $(date +%s) - $(stat -c %Y /proc/16752/environ )


      where




      • date +%s is current date in second since the epoch


      • stat -c %Y /proc/16752/environ is "creation date" of /proc/16752/environ, that is the moment where proc #16752 was launched

      edit:



      • maybe /proc/$PID/environ is the wrong pseudo file, /proc/$PID/exe should be used instead.


      • if time changed on host, both result from ps -p $PID -h -o etime and timestamp from /proc/$PID/exe are likely to be wrong. (unsure how to check that).






      share|improve this answer















      assumping process is 16752 (which you seems to be able to figure out)



      expr $(date +%s) - $(stat -c %Y /proc/16752/environ )


      where




      • date +%s is current date in second since the epoch


      • stat -c %Y /proc/16752/environ is "creation date" of /proc/16752/environ, that is the moment where proc #16752 was launched

      edit:



      • maybe /proc/$PID/environ is the wrong pseudo file, /proc/$PID/exe should be used instead.


      • if time changed on host, both result from ps -p $PID -h -o etime and timestamp from /proc/$PID/exe are likely to be wrong. (unsure how to check that).







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Jun 9 '15 at 11:31

























      answered Jun 8 '15 at 14:11









      ArchemarArchemar

      20.2k93773




      20.2k93773












      • This system set's it's time on startup from the application. Since this occurs between the process start and the checking of the process elapsed time, it is pretty much guaranteed to be wrong. Such timestamps are generally hazardous even when not guaranteed to be a problem. Thanks for taking the time to respond.

        – altendky
        Jun 8 '15 at 16:29











      • I am glad you found a solution, are you sure there is a difference between ps'etime and my solution ?

        – Archemar
        Jun 9 '15 at 3:57











      • The user (or application in my case) can change the system time between the process start and checking of elapsed time. You can try elapsed() expr $(date +%s) - $(stat -c %Y /proc/$MYPID/environ ); ; sleep 10 & export MYPID=$!; elapsed; sleep 1; elapsed; sudo date +%F %T -s "$(date -d "$(date +%F %T) 1 hour ago")"; elapsed. My result is 0, then 1, then -3599. Don't forget to reset your time, optionally like sudo rdate 0.us.pool.ntp.org (it takes a minute or two for me). Wall clock time is a hazardous way to keep track of durations and should be avoided when uptime is usable.

        – altendky
        Jun 9 '15 at 11:07

















      • This system set's it's time on startup from the application. Since this occurs between the process start and the checking of the process elapsed time, it is pretty much guaranteed to be wrong. Such timestamps are generally hazardous even when not guaranteed to be a problem. Thanks for taking the time to respond.

        – altendky
        Jun 8 '15 at 16:29











      • I am glad you found a solution, are you sure there is a difference between ps'etime and my solution ?

        – Archemar
        Jun 9 '15 at 3:57











      • The user (or application in my case) can change the system time between the process start and checking of elapsed time. You can try elapsed() expr $(date +%s) - $(stat -c %Y /proc/$MYPID/environ ); ; sleep 10 & export MYPID=$!; elapsed; sleep 1; elapsed; sudo date +%F %T -s "$(date -d "$(date +%F %T) 1 hour ago")"; elapsed. My result is 0, then 1, then -3599. Don't forget to reset your time, optionally like sudo rdate 0.us.pool.ntp.org (it takes a minute or two for me). Wall clock time is a hazardous way to keep track of durations and should be avoided when uptime is usable.

        – altendky
        Jun 9 '15 at 11:07
















      This system set's it's time on startup from the application. Since this occurs between the process start and the checking of the process elapsed time, it is pretty much guaranteed to be wrong. Such timestamps are generally hazardous even when not guaranteed to be a problem. Thanks for taking the time to respond.

      – altendky
      Jun 8 '15 at 16:29





      This system set's it's time on startup from the application. Since this occurs between the process start and the checking of the process elapsed time, it is pretty much guaranteed to be wrong. Such timestamps are generally hazardous even when not guaranteed to be a problem. Thanks for taking the time to respond.

      – altendky
      Jun 8 '15 at 16:29













      I am glad you found a solution, are you sure there is a difference between ps'etime and my solution ?

      – Archemar
      Jun 9 '15 at 3:57





      I am glad you found a solution, are you sure there is a difference between ps'etime and my solution ?

      – Archemar
      Jun 9 '15 at 3:57













      The user (or application in my case) can change the system time between the process start and checking of elapsed time. You can try elapsed() expr $(date +%s) - $(stat -c %Y /proc/$MYPID/environ ); ; sleep 10 & export MYPID=$!; elapsed; sleep 1; elapsed; sudo date +%F %T -s "$(date -d "$(date +%F %T) 1 hour ago")"; elapsed. My result is 0, then 1, then -3599. Don't forget to reset your time, optionally like sudo rdate 0.us.pool.ntp.org (it takes a minute or two for me). Wall clock time is a hazardous way to keep track of durations and should be avoided when uptime is usable.

      – altendky
      Jun 9 '15 at 11:07





      The user (or application in my case) can change the system time between the process start and checking of elapsed time. You can try elapsed() expr $(date +%s) - $(stat -c %Y /proc/$MYPID/environ ); ; sleep 10 & export MYPID=$!; elapsed; sleep 1; elapsed; sudo date +%F %T -s "$(date -d "$(date +%F %T) 1 hour ago")"; elapsed. My result is 0, then 1, then -3599. Don't forget to reset your time, optionally like sudo rdate 0.us.pool.ntp.org (it takes a minute or two for me). Wall clock time is a hazardous way to keep track of durations and should be avoided when uptime is usable.

      – altendky
      Jun 9 '15 at 11:07











      0














      @yaegashi had the approach right but my sed call seems to introduce the need for a third .



      root@embedded:/data# set -vx
      root@embedded:/data# echo Process Uptime: $(eval $(/opt/bin/busybox ps -o pid,etime | sed -n "s/ *$APP_PID +([0-9]+):([0-9]+).*/expr 1 \* 60 + 2/p"))
      echo Process Uptime: $(eval $(/opt/bin/busybox ps -o pid,etime | sed -n "s/ *$APP_PID +([0-9]+):([0-9]+).*/expr 1 \* 60 + 2/p"))
      + /opt/bin/busybox ps -o pid,etime
      + sed -n s/ *1156 +([0-9]+):([0-9]+).*/expr 1 \* 60 + 2/p
      + eval expr 2 * 60 + 46
      + expr 2 * 60 + 46
      + echo Process Uptime: 166
      Process Uptime: 166


      Fingers crossed that this not only works now, but also continues to do so next time I use it...






      share|improve this answer





























        0














        @yaegashi had the approach right but my sed call seems to introduce the need for a third .



        root@embedded:/data# set -vx
        root@embedded:/data# echo Process Uptime: $(eval $(/opt/bin/busybox ps -o pid,etime | sed -n "s/ *$APP_PID +([0-9]+):([0-9]+).*/expr 1 \* 60 + 2/p"))
        echo Process Uptime: $(eval $(/opt/bin/busybox ps -o pid,etime | sed -n "s/ *$APP_PID +([0-9]+):([0-9]+).*/expr 1 \* 60 + 2/p"))
        + /opt/bin/busybox ps -o pid,etime
        + sed -n s/ *1156 +([0-9]+):([0-9]+).*/expr 1 \* 60 + 2/p
        + eval expr 2 * 60 + 46
        + expr 2 * 60 + 46
        + echo Process Uptime: 166
        Process Uptime: 166


        Fingers crossed that this not only works now, but also continues to do so next time I use it...






        share|improve this answer



























          0












          0








          0







          @yaegashi had the approach right but my sed call seems to introduce the need for a third .



          root@embedded:/data# set -vx
          root@embedded:/data# echo Process Uptime: $(eval $(/opt/bin/busybox ps -o pid,etime | sed -n "s/ *$APP_PID +([0-9]+):([0-9]+).*/expr 1 \* 60 + 2/p"))
          echo Process Uptime: $(eval $(/opt/bin/busybox ps -o pid,etime | sed -n "s/ *$APP_PID +([0-9]+):([0-9]+).*/expr 1 \* 60 + 2/p"))
          + /opt/bin/busybox ps -o pid,etime
          + sed -n s/ *1156 +([0-9]+):([0-9]+).*/expr 1 \* 60 + 2/p
          + eval expr 2 * 60 + 46
          + expr 2 * 60 + 46
          + echo Process Uptime: 166
          Process Uptime: 166


          Fingers crossed that this not only works now, but also continues to do so next time I use it...






          share|improve this answer















          @yaegashi had the approach right but my sed call seems to introduce the need for a third .



          root@embedded:/data# set -vx
          root@embedded:/data# echo Process Uptime: $(eval $(/opt/bin/busybox ps -o pid,etime | sed -n "s/ *$APP_PID +([0-9]+):([0-9]+).*/expr 1 \* 60 + 2/p"))
          echo Process Uptime: $(eval $(/opt/bin/busybox ps -o pid,etime | sed -n "s/ *$APP_PID +([0-9]+):([0-9]+).*/expr 1 \* 60 + 2/p"))
          + /opt/bin/busybox ps -o pid,etime
          + sed -n s/ *1156 +([0-9]+):([0-9]+).*/expr 1 \* 60 + 2/p
          + eval expr 2 * 60 + 46
          + expr 2 * 60 + 46
          + echo Process Uptime: 166
          Process Uptime: 166


          Fingers crossed that this not only works now, but also continues to do so next time I use it...







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jun 9 '15 at 11:57

























          answered Jun 8 '15 at 18:17









          altendkyaltendky

          12115




          12115





















              0














              This is an awk script which will convert the 0-00:00:00 output to seconds. It is used like this...



              ps -eo pid,time,user,comm | awk '"$2"' | awk -f _os_return_process_time_in_seconds.awk


              Here is the script. Just wrote if for another purpose. May have issues still.



              BEGIN "


              "total_seconds"





              share|improve this answer








              New contributor




              Ethan Post is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.
























                0














                This is an awk script which will convert the 0-00:00:00 output to seconds. It is used like this...



                ps -eo pid,time,user,comm | awk '"$2"' | awk -f _os_return_process_time_in_seconds.awk


                Here is the script. Just wrote if for another purpose. May have issues still.



                BEGIN "


                "total_seconds"





                share|improve this answer








                New contributor




                Ethan Post is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.






















                  0












                  0








                  0







                  This is an awk script which will convert the 0-00:00:00 output to seconds. It is used like this...



                  ps -eo pid,time,user,comm | awk '"$2"' | awk -f _os_return_process_time_in_seconds.awk


                  Here is the script. Just wrote if for another purpose. May have issues still.



                  BEGIN "


                  "total_seconds"





                  share|improve this answer








                  New contributor




                  Ethan Post is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.










                  This is an awk script which will convert the 0-00:00:00 output to seconds. It is used like this...



                  ps -eo pid,time,user,comm | awk '"$2"' | awk -f _os_return_process_time_in_seconds.awk


                  Here is the script. Just wrote if for another purpose. May have issues still.



                  BEGIN "


                  "total_seconds"






                  share|improve this answer








                  New contributor




                  Ethan Post is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  share|improve this answer



                  share|improve this answer






                  New contributor




                  Ethan Post is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  answered 2 hours ago









                  Ethan PostEthan Post

                  1011




                  1011




                  New contributor




                  Ethan Post is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.





                  New contributor





                  Ethan Post is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






                  Ethan Post is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Unix & Linux Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f208242%2fget-process-elapsed-time-in-seconds%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      -quoting, shell

                      Popular posts from this blog

                      Frič See also Navigation menuinternal link

                      Identify plant with long narrow paired leaves and reddish stems Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?What is this plant with long sharp leaves? Is it a weed?What is this 3ft high, stalky plant, with mid sized narrow leaves?What is this young shrub with opposite ovate, crenate leaves and reddish stems?What is this plant with large broad serrated leaves?Identify this upright branching weed with long leaves and reddish stemsPlease help me identify this bulbous plant with long, broad leaves and white flowersWhat is this small annual with narrow gray/green leaves and rust colored daisy-type flowers?What is this chilli plant?Does anyone know what type of chilli plant this is?Help identify this plant

                      fontconfig warning: “/etc/fonts/fonts.conf”, line 100: unknown “element blank” The 2019 Stack Overflow Developer Survey Results Are In“tar: unrecognized option --warning” during 'apt-get install'How to fix Fontconfig errorHow do I figure out which font file is chosen for a system generic font alias?Why are some apt-get-installed fonts being ignored by fc-list, xfontsel, etc?Reload settings in /etc/fonts/conf.dTaking 30 seconds longer to boot after upgrade from jessie to stretchHow to match multiple font names with a single <match> element?Adding a custom font to fontconfigRemoving fonts from fontconfig <match> resultsBroken fonts after upgrading Firefox ESR to latest Firefox