如何在不产生两个进程的情况下以其他用户身份运行nohup? [英] How do I run nohup as a different user without spawning two processes?
问题描述
我正在尝试nohup命令并以其他用户身份运行它,但是每次执行此操作时,都会产生两个进程.
I'm trying to nohup a command and run it as a different user, but every time I do this two processes are spawned.
例如:
$ nohup su -s /bin/bash nobody -c "my_command" > outfile.txt &
这肯定以没有人的身份运行my_command,但是我不想显示一个额外的过程:
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
如果我杀死了根进程,没人进程仍然存在...但是有没有办法根本不运行根进程?由于获取my_command的ID并将其杀死会更加复杂.
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.
推荐答案
这可以通过以下方式实现:
This could be achieved as:
su nobody -c "nohup my_command >/dev/null 2>&1 &"
,然后在pidFile中写入"my_command"的pid:
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'"
这篇关于如何在不产生两个进程的情况下以其他用户身份运行nohup?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!