Yuki
Yuki

Reputation: 4173

bash exec with different user without extra process

Currently I am running the command in my bash script like

exec sudo -u mongodb mongod $@

The problem is that i would have two processes here. One for sudo and one forked from sudo mongod. Is there a way to avoid sudo process?

Upvotes: 0

Views: 66

Answers (1)

Ondrej K.
Ondrej K.

Reputation: 9664

You cannot achieve specifically that with sudo, because of how sudo is implemented, from its man page:

There are two distinct ways sudo can run a command.

If an I/O logging plugin is configured or if the security policy explicitly requests it, a new pseudo-terminal (“pty”) is allocated and fork(2) is used to create a second sudo process, referred to as the monitor...

If no pty is used, sudo calls fork(2), sets up the execution environment as described above, and uses the execve(2) system call to run the command in the child process...

In other words, sudo uses two processes.

So if you are trying to avoid fork() (or clone() these days), you need to look for a different way of changing your user, perhaps su; if euid is acceptable for your purpose, suid bit with corresponding ownership.

Upvotes: 2

Related Questions