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

左心房为你撑大大i 提交于 2019-12-04 03:57:16

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'"
user2196349
nohup runuser nobody -c "my_command my_command_args....." < /dev/null >> /tmp/mylogfile 2>&1 &

If the user with nologin shell, run 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 &"

You might do best to create a small script in e.g. /usr/local/bin/start_my_command like this:

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

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!