问题描述
我有一个以root身份运行的程序.这个应用程式会呼叫另一个程式(processA)来执行.当processA运行时,它由root拥有,但是我希望它的所有者成为登录的当前用户.怎么做?
I have a program that is running as root. This app calls another program (processA) to run. When processA is running, it is owned by root but I want owner of it to be the current user logged on. How to do it?
推荐答案
有点棘手...取决于它是守护程序(服务)还是您运行此命令/应用程序.
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
这篇关于更改Mac/Linux上进程的用户所有者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!