change user owner of process on Mac/Linux?

感情迁移 提交于 2020-01-13 18:01:50

问题


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?


回答1:


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

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

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. 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 = the username that will run this command. -c ./test.sh = means it will execute this command

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



来源:https://stackoverflow.com/questions/8140884/change-user-owner-of-process-on-mac-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!