[SOLVED] crontab which is written once and runs for everyone
Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Distribution: On my PC I use RHEL, at office AIX, Solaris, HP-UX, RHEL.
Posts: 254
Original Poster
Rep:
Well, thanks for the info! I was trying that method when I was posting the question. I was looking for some other better way.... running a script which can list all the users and then substitute them one by one for the mail program to use in the "to or cc" field.
Well, one more thing. The method which you have stated is fine if we want to email the output of a single command such as given in this fragment:
Code:
/bin/ps | mail -s "Process Info" userX userY userZ userN
but we have to use the same long line of the users for every command. So, is there any workaround for it?
One more thing yet: the commnad as given below:
Code:
crontab -u userName -e
can configure crontab for that particular user only. How do we do it if we want to configure the same crontab for all the users in our network?
Last edited by Hi_This_is_Dev; 09-10-2009 at 03:31 PM.
AIUI, free http://linux.die.net/man/1/free only applies to the whole system, not each user, so there's no point sending it to everyone, and especially not every minute(!)
You could post it to an Intranet website.
Alternatively you can mail it to a mailing list address, which contains the address of all users.
Distribution: On my PC I use RHEL, at office AIX, Solaris, HP-UX, RHEL.
Posts: 254
Original Poster
Rep:
Well, that was just an example. I would not want to do that every minute!
However, your idea: "Alternatively you can mail it to a mailing list address, which contains the address of all users." sounds good and I will try it and post my findings here.
Quote:
Originally Posted by chrism01
AIUI, free http://linux.die.net/man/1/free only applies to the whole system, not each user, so there's no point sending it to everyone, and especially not every minute(!)
You could post it to an Intranet website.
Alternatively you can mail it to a mailing list address, which contains the address of all users.
How do we do it if we want to configure the same crontab for all the users in our network?
Write the content of the crontab in a file and pass the file name as argument to the crontab command (beware it will overwrite any existing user's crontab):
Code:
for user in $users
do
crontab -u $user file
done
If users are spread over diffrent machines in the local network and you have to use ssh to accomplish the task, you can use process substitution in place of the file, with the same result:
Code:
for user in $users
do
ssh -n root@${host} "crontab -u ${user} <(echo '0 9 * * * /bin/date')"
done
Anyway, I second the suggestion by chrism01: run the commands and send the output to your local mailing list. Far more easy to mantain. Furthermore, users can always delete or edit their own crontab and your effort is lost.
I just added surrounding slashes to the pattern, but it should not make the difference.
Edit: I forgot to mention that if the array approach does not work, you can always pass the output directly to a for or while loop. For example:
Code:
for user in $(awk -F: '/\/home\//{print $1}' /etc/passwd)
do
echo $user
done
while read user
do
echo $user
done < <(awk -F: '/\/home\//{print $1}' /etc/passwd)
which equals the original /bin/sh syntax which uses back-ticks, but with some additional feature, like the ability to use nested commands. A complete explanation in the Advanced Bash Scripting Guide, here.
Distribution: On my PC I use RHEL, at office AIX, Solaris, HP-UX, RHEL.
Posts: 254
Original Poster
Rep:
Thanks for explaining me the following command:
$() as in:
Code:
echo $(date)
By the way, I am at work in the company I work for. What do you do? My present work involves monitoring of networks, backups, domino servers, etc. as I am in Data Center Operations.
is made of two parts. The first < sign is the usual redirection of standard input: instead of accepting input from the keyboard, the loop (and its read statement in this specific case) accepts input from what follows. The rest of the line uses process substitution, with the syntax:
Code:
<(command)
again the Advanced Bash Scripting Guide comes in help, here. A loop of this kind has the great advantage it can easily manage input containing blank spaces, provided you enclose the value of the loop variable in double quotes, when using it inside the loop itself. For example suppose you have two files (one of them with spaces in its name):
Code:
$ ls -l *.txt
-rw-r--r-- 1 alex users 0 2009-09-13 23:11 file.txt
-rw-r--r-- 1 alex users 0 2009-09-13 23:11 file with spaces.txt
$ for file in $(ls *.txt)
> do
> ls -l "$file"
> done
-rw-r--r-- 1 alex users 0 2009-09-13 23:11 file.txt
ls: cannot access file: No such file or directory
ls: cannot access with: No such file or directory
ls: cannot access spaces.txt: No such file or directory
$ while read file
> do
> ls -l "$file"
> done < <(ls *.txt)
-rw-r--r-- 1 alex users 0 2009-09-13 23:11 file.txt
-rw-r--r-- 1 alex users 0 2009-09-13 23:11 file with spaces.txt
As you can see the for loop fails, whereas the while loop with process substitution do its job without a glitch.
By the way, I am at work in the company I work for. What do you do? My present work involves monitoring of networks, backups, domino servers, etc. as I am in Data Center Operations.
I'm not at work, now (here in Italy is almost midnight). By the way, I'm a physical and biological oceanographer.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.