Hi, I need to write a shell script that would: -check if a specified directory exists and contains any files -chmod and chown all files inside the directory -execute all shell scripts in that directory (*.sh) That script would be run by Cron using root account. So far I came up with something like this: Code: #!/bin/bash KATALOG=/var/scripts if [ -d $katalog ]; then chmod -R 700 /var/scripts for SCRIPT in $katalog do if [ -f $SCRIPT -a -x $SCRIPT ] then $SCRIPT fi done else echo "ERROR! The directory doesn't exist." exit 1 fi Please help me in making it do what it's supposed to do. Thanks in advance!
I suggest you to use something like: Code: #!/bin/bash KATALOG=/var/scripts FILESPEC=*.sh DIRPERM=700 FILEPERM=750 OWNER=user:group CH_FSPEC=0 if [ -d $KATALOG ] then find $KATALOG -type d | xargs chmod $DIRPERM if [ $CH_FSPEC -gt 0 ] then find $KATALOG -type f | xargs chmod $FILEPERM else find $KATALOG -type f | grep -v "$FILESPEC" | xargs chmod $FILEPERM fi chown -R $OWNER $KATALOG for SCRIPT in $KATALOG/$FILESPEC do if [ -x $SCRIPT ] then echo "Executing : $SCRIPT" . $SCRIPT fi done else echo "ERROR! The directory doesn't exist." exit 1 fi exit 0 I wouldn't like a cron script to change permissions. Giving execution permissions on the fly, seems pretty dangerous to me.
Your suggestion is good but it's working when I run Your script manually (sh /root/script.sh). When I try to make Cron do it nothing happens. Code: crontab -e Code: * * * * * sh /root/script.sh > /dev/null I have also tried using full path: Code: * * * * * /bin/sh /root/script.sh > /dev/null Here's a script from /var/scripts which Your script should execute: Code: #!/bin/bash groupadd someuser useradd -d /home/someuser -s /bin/bash -m -g someuser someuser echo "someuser:somepassword" | chpasswd It's also working when I run it manually. All that might seem a little strange because I haven't fully described what I'd like to do. The test server runs Apache (default installation) with php. A php script generates shell scripts in /var/scripts/ with information provided in the form. You know the rest. I'm close but I'm missing something here because Cron doesn't execute them.
Ok, I think I've got it! It's an environmental problem. Cron doesn't know where to find the commands so I've used full paths and it seems to work. Code: #!/bin/bash /usr/sbin/groupadd someuser /usr/sbin/useradd -d /home/someuser -s /bin/bash -m -g someuser someuser echo "someuser:somepassword" | /usr/sbin/chpasswd
Environment under Cron As a general rule, in linux, you can "see" the environment of a running process by knowing it's PID: Code: strings /proc/PID/env On the other hand, when you issue no PATH, all programs (CRON included), run by default with a simple path: Code: PATH=/bin:/usr/bin So that's why you couldn't run programs under /usr/sbin. You can modify the PATH in your cron, so it suits your script needs.
That's right but I'll leave everything the way it is. To bad I can't see the output of the running scripts but will try to do something about it today after work.
If you want to see the output of the scripts, just look at the mail of the user which executes them in its cron. If you want a verbose detail, just add set -x to the top of your script and let it run. If you want to receive those notifications by mail, just add your email as an alias of the user in /etc/aliases.