Process Management – Why Does Running sudo cp Show Two Processes

processsudo

I am trying to copy some files and folders on a CentOS machine.

I do it like so: sudo cp source destination & to run it in the background.

I see there are three cp processes now when I run ps aux | grep cp: One of them is the grep command itself, but the other two are copies of the above cp command: one with the entire command listed above and the other without the sudo in front. The PIDs of the two processes also differs by one.
The owner of both is listed as root.

Why are there two processes?

Best Answer

The two processes are sudo on the one hand, and cp on the other. When you run

sudo cp source destination &

the shell starts sudo with the full command line; then sudo (which runs as root because it is setuid root) checks that you're allowed to run cp like that, and forks and starts cp. So while cp is running you see both sudo and cp processes.

Related Question