How do I run nohup as a different user without spawning two processes?

Viewed 14535

I'm trying to nohup a command and run it as a different user, but every time I do this two processes are spawned.

For example:

$ nohup su -s /bin/bash nobody -c "my_command" > outfile.txt &

This definitely runs my_command as nobody, but there's an extra process that I don't want to shown up:

$ ps -Af
.
.
.
root ... su -s /bin/bash nobody my_command
nobody ... my_command

And if I kill the root process, the nobody process still lives... but is there a way to not run the root process at all? Since getting the id of my_command and killing it is a bit more complicated.

5 Answers

A note on running this on a session, is that if you run in background, there is a job associated with the session, and background jobs may be killed (the su -c gets around this).

To disassociate the process from the shell (so you can exit the shell but keep the process running), use disown.

Related