如何在不产生两个进程的情况下以不同的用户身份运行nohup?
问题描述:
我正在尝试禁用一个命令并将其作为一个不同的用户运行,但每次我这样做时都会产生两个进程。如何在不产生两个进程的情况下以不同的用户身份运行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并杀死它会复杂一点。
答
您可能会尽最大努力在例如/usr/local/bin/start_my_command
这样的:
#!/bin/bash
nohup my_command > outfile.txt &
使用chown
和chmod
将其设置为可执行,并通过nobody
拥有,那么只需运行su nobody -c /usr/local/bin/start_my_command
。
答
nohup runuser nobody -c "my_command my_command_args....." </dev/null>> /tmp/mylogfile 2>&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'"
答
如果与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 &"
有什么'runuser'位? – 2014-04-15 23:10:06
做一个'man runuser'。它不是一个nohup的说法,而是它自己的单独命令很像su – Rooster 2014-06-03 16:00:23