0

if i run this command

ps aux | grep -ie ff1 | awk '{print $2 , $9 , $(NF-0)}'

i get this result:

7019 12:33 /var/www/html/tv1/video1.m3u8
13374 17:13 /var/www/html/tv1/asdas.m3u8
15001 05:58 /var/www/html/tv1/dfwef.m3u8
15021 05:58 /var/www/html/tv1/werwe.m3u8
15200 11:45 /var/www/html/tv1/2fsdfsf.m3u8

so second word in each line is time when process started so is there a way i can calculate that time with

date +%H:%M   - awk '{print $9}' 

so i can know how much time each proccess it was running


i want a result like this:

02:20 7019 12:33 /var/www/html/tv1/video1.m3u8

(it means proccess 7019 that starts on 12:33 has been running for 2hours and 20 minutes)

arpak
  • 133
  • 1
  • 10
  • Personally, I wouldn't use `ps` for this at all, but would extract the epoch time direct from procfs; subtract from the current epoch time and you have number of seconds of runtime. – Charles Duffy Jul 18 '17 at 17:12
  • 1
    ...epoch time is also rather considerably less bug prone when you get to corner cases like situations when your script is run just after midnight with processes started prior to that boundary. – Charles Duffy Jul 18 '17 at 17:15
  • maybe need something like this: ps -o etime= -p "PID NUMBER example awk '{print $2 " – arpak Jul 18 '17 at 17:18
  • 2
    @arpak: don't use awk to parse the output. Use the `-o` flag of `ps` to request exactly the fields you want in the correct order. – rici Jul 18 '17 at 17:27

2 Answers2

1

You can use the following commands. first one gives for the specific PID, 2nd for all processes.

ps -o etime= -p <PID>

ps -eo pid,etime
Kaushik Nayak
  • 30,772
  • 5
  • 32
  • 45
  • how to implement this command in mine ps aux | grep -ie ff1 | awk '{print $2 , $9 , $(NF-0)}' – arpak Jul 18 '17 at 17:24
  • Combine with https://stackoverflow.com/questions/14652445/parse-ps-etime-output-and-convert-it-into-seconds to convert to seconds perhaps :-) – cmbuckley Jul 18 '17 at 17:24
  • ps aux | grep -ie ff1 | awk '{print $2 , $9 , $(NF-0)}' | ps -o etime= -p something like this is it possible? – arpak Jul 18 '17 at 17:27
0

This should give what you want - elapsed time, PID, and start time:

$ ps -o etime,pid,bsdstart,cmd
    ELAPSED   PID  START CMD
      00:05 25980  18:33 bash
      00:00 26019  18:33 ps -o etime,pid,bsdstart,cmd

(and I second @CharlesDuffy's sentiment that you definitely should not try to parse this output.)

l0b0
  • 55,365
  • 30
  • 138
  • 223
  • how to implement in mine command ps aux | grep -ie ff1 | awk '{print $2 , $9 , $(NF-0)}' – arpak Jul 18 '17 at 17:24
  • What do you mean, @arpak? – l0b0 Jul 18 '17 at 17:27
  • i need to get result like this: 02:20 7019 12:33 /var/www/html/tv1/video1.m3u8 (it means proccess 7019 that starts on 12:33 has been running for 2hours and 20 minutes) – arpak Jul 18 '17 at 17:30
  • im waiting for it – arpak Jul 18 '17 at 17:41
  • look u dont understeand me . how i can add /var/www/html/tv1/video1.m3u8 there,???? u must add something to my command so it will show all results that show now. plus time difference – arpak Jul 18 '17 at 17:44
  • Nope, I don't understand you. I don't have /var/www/html/tv1/video1.m3u8, so how am I supposed to know what you even mean? Voting to close. – l0b0 Jul 18 '17 at 19:37