Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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!

share|improve this question

3 Answers 3

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 :)

share|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

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).

share|improve this answer

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.

share|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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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