我有一个以 root 身份运行的程序。这个应用程序调用另一个程序(processA)来运行。当 processA 运行时,它归 root 所有,但我希望它的所有者是当前登录的用户。怎么做?
问问题
3818 次
1 回答
4
好吧,这有点棘手...取决于它是守护程序(服务)还是您运行此命令/应用程序。
对于第二种情况,您可以使用“su”命令。这是一个简短的例子。
1.我创建了一个简单的脚本,内容如下(它将在后台休眠100秒,并输出与该脚本对应的进程列表):
#!/bin/bash
sleep 100 &
ps faux | grep test.sh
2.我像这样运行“su”命令(我目前以“root”身份登录,我想以“sandbox”用户身份运行此脚本):
su - sandbox -c ./test.sh
sandbox = 将运行此命令的用户名。-c ./test.sh = 表示它会执行这个命令
3. 输出(第一列 = 拥有此进程的用户):
root@i6:/web-storage/sandbox# su - sandbox -c ./test.sh
sandbox 18149 0.0 0.0 31284 1196 pts/0 S+ 20:13 0:00 \_ su - sandbox -c ./test.sh
sandbox 18150 0.0 0.0 8944 1160 pts/0 S+ 20:13 0:00 \_ /bin/bash ./test.sh
sandbox 18155 0.0 0.0 3956 644 pts/0 S+ 20:13 0:00 \_ grep test.sh
root@i6:/web-storage/sandbox#
我希望它会有所帮助,斯特凡
于 2011-11-15T18:15:55.850 回答