I've created a script in /etc/init.d/ which has to run several other scripts from other (non-root privileged) users from their home directories, as if they started them.
I launch these scripts with:
sudo -b -u <username> <script_of_a_particular_user>
And it works. But for every user script that continues running(for example some watchdog) I see a corresponding parent sudo process, still alive and running as root. This creates a mess in the active processes list.
So my question is: How can I launch(fork) another script from existing bash script as another user and leave it as an orphaned(stand alone) process?
More detailed explanation:
I'm basically trying to provide to other users on the machine a mean to run stuff upon system start or system shutdown by running executable files found in respective subdirectories found in their home directory, named .startUp and .shutDown. Since I did not find any other means to do that I wrote my bash script that does exactly that and I've configured it as a service script (by following the skeleton example) in /etc/init.d/ so when it is run with start argument it launches everything from .startUp directories and when it is run with stop argument it launches everything from .shutDown directories of all users as them.
Alternatively I'm also interested if I could have used some existing solution to solve this problem.
I've looked around a bit and I found this question: http://unix.stackexchange.com/questions/22478/detach-a-daemon-using-sudo
Accepted answer there, to use:
sudo -u user sh -c "daemon & disown %1", works for me to. But I also tried without disown %1 and it is the same. So this is what works for me as I expected:
sudo -u <username> bash -c "<script_of_a_particular_user> &"
My additional question now is, why is it working without disown? should I still leave the disown call, regardless, for some potential special case?
Apparently this works too:
su <username> -c "<script_of_a_particular_user> &"
Is there any difference between this call and the sudo call? I know this is potentially an entire different question. But since I'm finding the answers here myself maybe for the sake of this topic someone could clarify this here.
Both of these methods with either su or sudo now produce a new startpar process (single process that runs as root) after I boot the machine. Visible in process list as:
startpar -f -- <name_of_my_init.d_script>
Why is this process spawned? Obviously I'm doing something wrong since no other init.d script has this process running.
The issue with startpar is resolved. I've started another question for that:
startpar process left hanging when starting processes from rc.local or init.d
And another question to further discuss launching mechanisms for non privileged users:
Providing normal users(non-root) with initialization and shutdown auto-run capabilities