Thursday, December 30, 2010

System Configuration and Management -- Use shell scripting to automate system maintenance tasks

This objective is quite a bit more ethereal than the others - with no clear end game, this could mean almost anything. With that in mind, here are a few of the very basic scripts that I have found to assist in automating management. A good place to find help is the man bash page

  • Doing something to each file in a directory
    •  for i in [`ls`]; do echo $i; done
  • Doing something for each line in a file
    •  while read i; do echo $i; done < anaconda-ks.cfg
  • Repeating a task every 10 seconds
    • while true; do echo Hello World; sleep 10; done
  • Create a task that occurs the same time every day
    • crontab -e
    • Enter 1 22 * * * echo Hello World
  • Create a task that occurs once at a specific time/day
    • at 10pm Dec 31 [return]
    • echo Hello World [return]
    • [CTRL]+z
  • Creating an executable script
    • Identify a working set of bash commands and save them to a file
    • Add a #!/bin/bash as the first line (not required, but good form)
    • Execute chmod +x foo.sh to make it executable
http://www.linuxconfig.org/Bash_scripting_Tutorial is a great basic overview of bash scripting. Note that all these commands may work differently in a different shell.

No comments: