I'm a newbie server administrator having recently purchased an account from Slicehost.com and using it to host a few personal sites, so I can learn more (I'm primarily a web developer).

Years ago I remember playing with batch scripts on Windows that would allow me to automate certain basic things. Does anything like this exist for Unix?

I would like to automate setting up of new domains in Apache2, so basically creating directories, setting permissions, creating the configuration file in sites-available then running the a2ensite command and finally restarting Apache.

Is this achievable from a basic script I can run from the command line? Any basic pointers and guidance you can give would be much appreciated.

Thanks!

link|improve this question
feedback

3 Answers

up vote 5 down vote accepted

I'd suggest you read up on bash scripting.

Some good links:

Bash is a lot more powerful than a batch file :)

link|improve this answer
More powerful than batch programming? I don't know. Better documented? Absolutely. – J. Polfer Jul 6 '09 at 17:09
I have found that *nix shell scripting languages are much more powerful than default Windows batch scripting. This is due to partially to more powerful script language features but mostly due to *nix philosophy for making very powerful command line utilities that accept piped data. – Chris Nava Jul 6 '09 at 18:26
Perfect! Thanks, that's really handy - I'll check out those resources. – Hellweaver666 Jul 7 '09 at 10:32
feedback

It is definitely doable. Anything that can be accomplished by a series of commands on the command line can be put into a bash script and run from there.

The script starts with #!/bin/bash or #!/bin/sh (for sh instead of bash) After that # starts a comment line $VARIABLE_NAME can be used to refer to a variable set by VARIABLE_NAME=foo

Command line parameters can be grabbed starting with $1, ($0 is the name of the program that was called).

so if you want to evoke a script as script.sh new_site

you could have script.sh looks something like this

#!/bin/sh

mkdir /var/www/htdocs/new_site chmod 755 /var/www/htdocs/new_site ... apachectl restart

then you simply have to run the script with sufficient permissions actually perform all the commands run.

Use sh -x to debug (it will step through the script with lots of output so you can tell what is actually happening).

link|improve this answer
feedback

You might also look into puppet. It's a bit of overkill for a single server, but is definitely designed to handle this kind of thing.

link|improve this answer
Is it this Puppet: en.wikipedia.org/wiki/Puppet_(tool) ? "Puppet is an open source configuration management tool. It is written in Ruby." – Peter Mortensen Jul 14 '09 at 23:44
Yes, that one. reductivelabs.com/trac/puppet – freiheit Jul 15 '09 at 0:56
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.