Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 828
Apothem Avatar asked Jul 30 '12 18:07

Apothem


1 Answers

This could be achieved as:

su nobody -c "nohup my_command >/dev/null 2>&1 &"

and to write the pid of 'my_command' in a pidFile:

pidFile=/var/run/myAppName.pid
touch $pidFile
chown nobody:nobody $pidFile
su nobody -c "nohup my_command >/dev/null 2>&1 & echo \$! > '$pidFile'"
like image 193
javdev Avatar answered Oct 24 '22 06:10

javdev