如何做到不产卵两个过程我运行的nohup为不同的用户?(How do I run nohup as

2019-07-30 16:26发布

我试图NOHUP命令并运行它以不同的用户,但每次我这样做两个进程催生。

例如:

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

这绝对my_command以nobody运行,但有一个额外的过程,我不所示起来想:

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

如果我杀了根的过程中,没有人过程仍然生活...但有没有办法不运行在所有的根进程? 由于越来越my_command的ID和杀死它是一个比较复杂一点。

Answer 1:

这可以实现为:

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

并写在PIDFILE“my_command”的PID:

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


Answer 2:

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


Answer 3:

如果与NOLOGIN壳的用户,运行如下:

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

要么:

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

要么:

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 &"


Answer 4:

你可能会做最好建立在例如小脚本/usr/local/bin/start_my_command是这样的:

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

使用chownchmod将其设置为可执行,并通过拥有的nobody ,然后只需运行su nobody -c /usr/local/bin/start_my_command



文章来源: How do I run nohup as a different user without spawning two processes?