[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Changing Users in a script



I have a system with several different users and would like to use cron to run this script as root:

#!/bin/bash

for user in `ls /home/`; do
#	echo "Path: $user"
	if [ "${user:0:1}" != "0" ]; then
		path="/home/$user/Backup"
		if [ -e $path ]; then
			echo "Calling backup for user: $user"
			sudo -u $user /usr/local/bin/user-backup
		fi
	fi
done

The idea is that instead of adding a backup script every time I add a user, this script will go through the /home directories and skip any that start with a 0 (a program I'm using creates some directories there, but starts their names with a 0) and automatically call the generic backup script for that user.

The problem is sudo can't be run without a tty, so I can run it myself, but it won't run from a script.

I want the backup script to run under each user's name to match the user on the backup system.

Are there other ways to do this with an "all-in-one" approach?  Either for a script run as root to run scripts with the id of the users or some generic way to tell cron to run a script once for each user that meets certain conditions?

I prefer the all-in-one solutions, since when I add a user, I'm adding it to their system, to this backup NAS, and to an offsite backup NAS, and even though I use notes, it's easy to forget having to do extra things when adding a user.  So I'd really prefer a solution that handles all of them at once.

Any other way I can do this?



Thanks!



Hal

Reply to: