Go Back   HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials > Linux Forums > Programming/Scripts

Do you like HowtoForge? Please consider supporting us by becoming a subscriber.
Reply
 
Thread Tools Display Modes
  #1  
Old 27th September 2009, 02:26
Leszek Leszek is offline
Senior Member
 
Join Date: Nov 2006
Location: Poland,Włocławek
Posts: 369
Thanks: 22
Thanked 42 Times in 35 Posts
Send a message via Skype™ to Leszek
Default 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!
Reply With Quote
Sponsored Links
  #2  
Old 27th September 2009, 04:15
lchvdlch lchvdlch is offline
Junior Member
 
Join Date: Sep 2009
Posts: 6
Thanks: 0
Thanked 1 Time in 1 Post
Default

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.
Reply With Quote
The Following User Says Thank You to lchvdlch For This Useful Post:
Leszek (27th September 2009)
  #3  
Old 27th September 2009, 15:33
Leszek Leszek is offline
Senior Member
 
Join Date: Nov 2006
Location: Poland,Włocławek
Posts: 369
Thanks: 22
Thanked 42 Times in 35 Posts
Send a message via Skype™ to Leszek
Default

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.
Reply With Quote
  #4  
Old 27th September 2009, 18:34
Leszek Leszek is offline
Senior Member
 
Join Date: Nov 2006
Location: Poland,Włocławek
Posts: 369
Thanks: 22
Thanked 42 Times in 35 Posts
Send a message via Skype™ to Leszek
Default

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
Reply With Quote
  #5  
Old 28th September 2009, 15:09
lchvdlch lchvdlch is offline
Junior Member
 
Join Date: Sep 2009
Posts: 6
Thanks: 0
Thanked 1 Time in 1 Post
Default 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.
Reply With Quote
  #6  
Old 28th September 2009, 15:19
Leszek Leszek is offline
Senior Member
 
Join Date: Nov 2006
Location: Poland,Włocławek
Posts: 369
Thanks: 22
Thanked 42 Times in 35 Posts
Send a message via Skype™ to Leszek
Default

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.
Reply With Quote
  #7  
Old 28th September 2009, 15:32
lchvdlch lchvdlch is offline
Junior Member
 
Join Date: Sep 2009
Posts: 6
Thanks: 0
Thanked 1 Time in 1 Post
Default

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.
Reply With Quote
  #8  
Old 28th September 2009, 15:41
Leszek Leszek is offline
Senior Member
 
Join Date: Nov 2006
Location: Poland,Włocławek
Posts: 369
Thanks: 22
Thanked 42 Times in 35 Posts
Send a message via Skype™ to Leszek
 
Default

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

Bookmarks

Tags
bash, cron, shell

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Install failure -- Apache 1.3 missing? Allen15 Installation/Configuration 11 24th February 2009 09:53
can't help ispconfig to install please help steve51184 Installation/Configuration 17 20th February 2009 10:37
ISPConfig install issues... flyingaggie Installation/Configuration 2 18th July 2008 10:46
add web site serr57 Installation/Configuration 18 13th April 2008 11:40
Systemimager (rsync) doesn't copy all comedit HOWTO-Related Questions 11 19th January 2007 17:17


All times are GMT +2. The time now is 00:05.


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