且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

更改Mac/Linux上进程的用户所有者?

更新时间:2023-02-21 08:03:13

有点棘手...取决于它是守护程序(服务)还是您运行此命令/应用程序.

Well it's a little bit tricky... Depends if it's a daemon (service) or you run this command/app.

对于第二种情况,您可以使用"su" 命令. 这是一个简短的例子.

For the 2nd case you can use "su" command. Here's a short example.

1.我用以下内容创建了一个简单的脚本(它将在后台休眠100秒,并将输出与此脚本相对应的进程列表):

1. I create o simple script with following content (it will sleep in background for 100 seconds and will output the process list coresponding to this script):

#!/bin/bash
sleep 100 &
ps faux | grep test.sh

2.我这样运行"su"命令(我目前以"root"身份登录,并且希望以"sandbox"用户身份运行此脚本):

2. I run the "su" command like this (I'm currently logged in as "root" and I want to run this script as "sandbox" user):

su - sandbox -c ./test.sh

sandbox =将运行此命令的用户名. -c ./test.sh =表示它将执行此命令

sandbox = the username that will run this command. -c ./test.sh = means it will execute this command

3.输出(第一列=拥有此过程的用户):

3. Output (first column = the user that owns this process):

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#

我希望它会有所帮助, 斯蒂芬

I hope it will help, Stefan