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.
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