1

While running the ps command, the output is as follows :

  PID TTY          TIME CMD
  494 pts/0    00:00:00 ps
29487 pts/0    00:00:00 bash
32617 pts/0    00:00:02 gedit

Why is the TIME 00:00:00 for bash? According to GeeksforGeeks, the explanation is as follows :

It is nothing but the total accumulated CPU utilization time for any process and 00:00:00 indicates no CPU time has been given by the kernel till now. In above example we found that, for bash no CPU time has been given.

Does someone has a better explanation ?

4
  • 1
    Because the CPU time spent by the bash process is less that a second. (Or less than half a second, in case that ps rounds to the nearest second.)
    – FedKad
    Sep 17, 2020 at 8:51
  • @FedonKadifeli Why is CPU time spent by the bash process is less that a second? Can you please expand? Sep 17, 2020 at 9:16
  • 1
    Because it does not do any number crunching stuff. bash simply reads commands from input and calls the relevant binary to do the job requested by the command. bash itself is not a CPU bound process. However you can make bash use some CPU by running the following bash command, waiting a few seconds and killing it using Ctrl+C: while [[ true ]] ; do : ; done
    – FedKad
    Sep 17, 2020 at 10:26
  • @FedonKadifeli Can you post this as an answer, so that I could close the question Sep 17, 2020 at 10:53

1 Answer 1

3

The ps command reports the CPU usage of a process in seconds. If you want to get this information in milliseconds, you can look at the end of this answer.

ps truncates the total CPU time used by the process to the nearest low second, so you can safely assume that the process you looked up (bash in your case) had used less than one CPU second so far.

This is normal, because bash does not do any number crunching stuff to spend too many CPU cycles. bash simply reads commands from input and calls the relevant executable to do the job requested by the command.

bash itself is not a "CPU bound" process. However, you can make bash use some CPU time by running the following bash command, waiting a few seconds and killing it using Ctrl+C:

while [[ true ]] ; do : ; done

After that, you can check once more the CPU time spent by bash by running ps again.


I have written a simple script to display the total CPU time used by a process in milliseconds. Actually, on most systems the clock ticks 100 times per second, so the accuracy of the output is in tens of milliseconds. You can call this script by passing a single parameter specifying the process ID of the process you are interested, e.g. it will be 494 in your question.

#!/bin/bash
usage="Usage: $(basename $0) pid"
case "$*" in
 ''|*[!0-9]*)
  echo "$usage" 1>&2
  exit 1
  ;;
 * )
  pid="$1"
  ;;
esac
read -r -a pinfo <<< $(cat "/proc/$pid/stat" 2>/dev/null)
if (( ${#pinfo[@]} == 0 )) ; then
  echo -e "No proccess with PID : $pid" >&2
  exit 2
fi
let clk_tck="$(getconf CLK_TCK)"
let utime="${pinfo[13]}"
let stime="${pinfo[14]}"
let cputime=(utime + stime)*1000/clk_tck
echo "PID : ${pinfo[0]}
Cmd : ${pinfo[1]}
CPU : $cputime msec"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .