How can I periodically run a script for each user, as that user?

I worked at making it as simple as possible, but it is now a little too simple.

As root, I want to be able to run a script for each user, as that user. For instance, you can be logged in as root and type su <user name> and you are that user, including his environment. Then you can type exit, and you will be back at root. I want to be able to cycle through all of the users, and run a script as that user. What I don't want to do is set up a cron at the root level, that runs a script for every user, as the user. I can run the su <user name> from within the script, and it will switch to that user, but the moment you do, I am no longer in the script, and the only way back is to manually type exit. When you do type exit, the script continues where you left off before switching users. I'm guessing DA has processes that do something like this periodically.
 
Last edited:
If I'm not mistaken this won't work unless the user also has SSH privileges.
When I do a su - username in console, I'm still root (even if I do a "whoami"), because only users with ssh privileges will appear in sshd_config and can be su'ed to.
Or does that not apply when you use the -c parameter?

However, you can also run crontabs as a user at root level so you don't have to place it in the users home directory.
I have been using /etc/crontab in the past for such thing:
Code:
0 */5 * * * user /home/user/script.sh
but that may also not be what you're looking for.
 
I'm making quite a mess of this thread. What I'm trying to do is:

As root, I want to be able to cron a script, that discovers all of the user names, then steps through each of the user names, and runs another script as that user name. Actually, I'm open to any method that enables me to automatically discover the user names and cycle through the user names and run the script as each user.
 
Last edited:
Here's an idea
Code:
#!/bin/sh

cd /usr/local/directadmin/data/users/

for d in * ; do
    echo "$d"
done

This will simply echo each directadmin user. You could probably modify it to execute a script using su.
 
The issue you have with the 'su -c' command is that you start a subshell as the non-priviliged user. 'su' means 'switch user', but you want to use 'sudo' like:

sudo -u youruser "/path/script.sh"
 
The issue you have with the 'su -c' command is that you start a subshell as the non-priviliged user. 'su' means 'switch user', but you want to use 'sudo' like:
sudo -u youruser "/path/script.sh"
sudo is an app that you install. I want to use su. I have it working, and am testing scripts before I post.
 
sudo is an app that you install.

So is bash, but that's not relevant.

su requires the non-priv user to have a shell assigned in his passwd file with sudo allows the user to keep /bin/false as his shell, preventing the user to get a shell.
 
True, if your distro supports the -s, use it, but not all do. And since it's run by root it doesn't really matter which way you go.
 
Thanks tons to Dutch Daemon, SeLLeRoNe, and Arieh for your input early on. I've been working on the project and thanks to Dutch Daemon and SeLLeRoNe for their thoughts on SU, and Arieh on clean way to get a list of DA users, the project is functional. However, I thought it best instead of posting the code here, to make a HOW-TO for it at HOW-TO enable users to easily train Bayesian data

Thanks again to everyone that contributed!
 
Back
Top