Search code examples
clinuxbashredhatsu

why drastic performance degradation when running a command as different user


i have a hello world C example ./a.out

Now i measured execution time using time for below commands

time  ./a.out
Hello World
real    0m0.001s
user    0m0.000s
sys     0m0.002s

time runuser -l root -c './a.out'
real    0m0.017s
user    0m0.004s
sys     0m0.011s

time su -s /bin/bash -c "./a.out" root 
Hello World
real    0m0.080s ---> 80 times slower
user    0m0.005s
sys     0m0.071s

Why is the third command 80 times slower than the first command?

Environment -- Redhat 7


Solution

  • With the second and third command, the time command also times the launch of runuser, su and bash, which takes some time as well.

    It shouldn't make so many difference if you do:

    $ runuser -l root -c 'time ./a.out'
    

    and:

    $ su -s /bin/bash -c "time ./a.out" root