How to start nohup as another user without creating two processes?

I try to run the nohup command and run it as a different user, but every time I do this, two processes are generated.

For instance:

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

This definitely starts my_command like no one else, but there is an additional process that I don't want to show:

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

And if I kill the root process, no one will continue the process ... but is there a way to not start the root process at all? Since getting the identifier my_command and killing is a bit more complicated.

+6
source share
4 answers

This can be achieved as:

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

and write pid "my_command" in the pidFile:

 pidFile=/var/run/myAppName.pid touch $pidFile chown nobody:nobody $pidFile su nobody -c "nohup my_command >/dev/null 2>&1 & echo \$! > '$pidFile'" 
+8
source

It is best to create a small script, for example. /usr/local/bin/start_my_command as follows:

 #!/bin/bash nohup my_command > outfile.txt & 

Use chown and chmod to install it as executable and owned by nobody , then just run su nobody -c /usr/local/bin/start_my_command .

+2
source
 nohup runuser nobody -c "my_command my_command_args....." < /dev/null >> /tmp/mylogfile 2>&1 & 
+2
source

If the user with the nologin shell works as follows:

 su - nobody -s /bin/sh -c "nohup your_command parameter >/dev/null 2>&1 &" 

Or:

 runuser - nobody -s /bin/sh -c "nohup your_command parameter >/dev/null 2>&1 &" 

Or:

 sudo su - nobody -s /bin/sh -c "nohup your_command parameter >/dev/null 2>&1 &" sudo runuser -u nobody -s /bin/sh -c "nohup your_command parameter >/dev/null 2>&1 &" 
0
source

Source: https://habr.com/ru/post/921710/


All Articles