HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials

HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials (http://www.howtoforge.com/forums/index.php)
-   Programming/Scripts (http://www.howtoforge.com/forums/forumdisplay.php?f=7)
-   -   Executing all shell scripts in specified directory (http://www.howtoforge.com/forums/showthread.php?t=39660)

Leszek 27th September 2009 02:26

Executing all shell scripts in specified directory
 
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!

lchvdlch 27th September 2009 04:15

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.

Leszek 27th September 2009 15:33

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.

Leszek 27th September 2009 18:34

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


lchvdlch 28th September 2009 15:09

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.

Leszek 28th September 2009 15:19

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.

lchvdlch 28th September 2009 15:32

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.

Leszek 28th September 2009 15:41

Maybe I'll log everything to a file readible by Apache and view it using a link.


All times are GMT +2. The time now is 17:33.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.