bash – 如何在不产生两个进程的情况下以不同的用户身份运行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

如果我杀死root进程,那么nobody进程仍然存在……但是有没有办法根本不运行root进程?因为获取my_command的id并将其删除有点复杂.

这可以通过以下方式实现:
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'"

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...