ps aux
includes the full command line (path and parameters), while pgrep only looks at the first 15 characters of the executable's names
ps aux
returns the full command line of each process, while pgrep
only looks at the names of the executables.
That means that grepping ps aux
output will match anything that occurs in the path or the parameters of a process' binary: e.g. `
ps aux | grep php5
will match /usr/share/php5/i-am-a-perl-script.pl
- but
pgrep php5
won't
Take an example from my system -- only we'll use python instead of php5
:
ps aux | grep python
gives us:
izx 2348 0.0 0.7 514928 15644 ? Sl Jun24 0:00 /usr/bin/python /usr/lib/unity-lens-video/unity-lens-video
izx 2444 0.0 0.9 547392 18864 ? Sl Jun24 0:01 /usr/bin/python /usr/lib/unity-scope-video-remote/unity-scope-video-remote
root 2805 0.0 0.5 95436 12204 ? S Jun24 0:00 /usr/bin/python /usr/lib/system-service/system-service-d
izx 6272 0.0 2.9 664400 60320 ? SNl Jun24 1:16 /usr/bin/python /usr/bin/update-manager --no-focus-on-map
root 11729 0.0 0.9 180508 19516 ? S Jun25 0:00 python /usr/lib/software-properties/software-properties-dbus
- But
pgrep python
returns only 11729
, which you'll see from the above list is:
root 11729 0.0 0.9 180508 19516 ? S Jun25 0:00 python /usr/lib/software-properties/software-properties-dbus
The problem is that mka
is a bash function exported by your script and not an executable (as opposed to make
), so /usr/bin/time
doesn't find it, as it looks for an executable.
There are two possible solutions.
First, note that there is a difference between the bash built-in function time
, and the executable /usr/bin/time
. These are different commands, which take different parameters and generate different output:
$ time sleep 1
real 0m1.001s
user 0m0.000s
sys 0m0.001s
$ /usr/bin/time sleep 1
0.00user 0.00system 0:01.00elapsed 0%CPU (0avgtext+0avgdata 664maxresident)k
0inputs+0outputs (0major+215minor)pagefaults 0swaps
Type help time
to get help for the bash built-in time
. Type man time
to get help for the executable /usr/bin/time
.
The first solution uses time
, whereas the second solution uses /usr/bin/time
.
First solution: using the bash built-in function time
The bash built-in function time
is aware of declared bash functions, and you can immediately use it to measure the time of such functions.
$ foo() {
sleep 1;
echo "Hello World"
}
$ time foo
Hello World
real 0m1.002s
user 0m0.000s
sys 0m0.001s
In your case, the following command should work:
$ time mka -j8 bacon
This would be my preferred way. However, it may not generate exactly the output you want. In particular, it does not measure or print CPU usage.
Second solution: using the executable /usr/bin/time
You cannot directly measure the time of a built-in bash function using /usr/bin/time
(as far as I know), but what you can do is measure the time of the /bin/bash
executable executing that bash function. This is a little hacky, and it generates a little overhead as you are launching an extra instance of bash
. But this overhead may be negligible when faced with a function that takes minutes to compute, so it may still suit your needs better.
To be able to launch the /bin/bash
executable on a bash function, we have to export the function first.
$ foo() {
sleep 1;
echo "Hello World"
}
$ export -f foo
$ echo foo | /usr/bin/time /bin/bash
Hello World
0.00user 0.00system 0:01.00elapsed 0%CPU (0avgtext+0avgdata 1476maxresident)k
0inputs+0outputs (0major+707minor)pagefaults 0swaps
Hence, in your case, to be able to use /usr/bin/time
and generate the output format you want, you could proceed as follows:
$ export -f mka
$ echo 'mka -j8 bacon' | /usr/bin/time -f "User\t%U\nSys\t%S\nReal\t%E\nCPU\t%P" /bin/bash
Best Answer
Because
time
and/usr/bin/time
run different code. In order to determine it, do the following:It will show you that:
Thus, it doesn't run
/usr/bin/time
if you simply calltime
.They are different things...