I've had some questions about shell scripts here of late and I thought I would take some time to share my thoughts on the notion of shell scripts. Based on the questions I have received I will based this article around shell scripts and cron jobs; how I set them up and when I tend to use them.
From my point of view, a shell script is simply a file consisting of system commands. I find these useful when I want to encapsulate a series of repetitive commands. Lets a say I want to update my website's virtualhost file. This usually requires executing a series of 5 commands in a specific order. Every time I try to update my virtualhosts file I can try to remember to update the virtualhosts file disable the site, reload Apache, enable the site and reload Apache. Not only do I need to remember the sequence of commands, but also the file path and the commands themselves. Alternatively, I can name the file something meaningful update_website.sh place it somewhere I can remember, lets say ~/shells. My file (~/shells/update_website.sh) might look like the following. Notice, these are the exact same commands I would enter directly into the console.
#!/bin/bash ## Udpate your vhosts file sudo vim /etc/apache2/sites-available/default ## Disable the existing hosts configuration sudo a2dissite default sudo /etc/init.d/apache2 reload ## Enable the existing hosts configuration sudo a2ensite default sudo /etc/init.d/apache2 reload
Assuming I have added the above to ~/shells/update_virtualhost.sh, I will need to make executable before I can run it.
## Add +x to make a file executable sudo chmod +x ~/shells/update_virtualhost.sh ## Prefix the file name with ./ t execute the shell cd ~/shells && ./update_virtualhost.sh
While this makes it easy to update the default virtualhosts file, what if I have several virtualhosts files due to running several websites on one server. I have two choices, duplicate the file and change "default" to something else or I can pass parameters. When creating a shell script that accepts parameters the parameters are passed by number with $0 being the command itself.
$0 is the name of the command
$1 first parameter
$2 second parameter etc.
#!/bin/bash ## Udpate your vhosts file sudo vim /etc/apache2/sites-available/$1 ## Disable the existing hosts configuration sudo a2dissite $1 sudo /etc/init.d/apache2 reload ## Enable the existing hosts configuration sudo a2ensite $1 sudo /etc/init.d/apache2 reload ## Add some feed back so we know everything is done echo "$1 has been updated and reloaded"
Another common use is backing up your database. The following will execute the mysqldump command and pipe the results into a gzipped file. This sort of script goes well with a cron job.
#!/bin/bash ## Set the current time stamp to build the file name suffix=$(date +%Y%m%d) ## Create a new full backup mysqldump -h localhost -u username --password='password' database_name | gzip > /home/jasonsnider/db_backups/backup$suffix.sql.gz
That's it, encapsulate a few simple commands, add some parameters and presto, a working shell script.
A cron job is a way of scheduling a command. Just about anything that can be executed from a command line, can be executed using a cron job. Cron jobs are executed by the user who created it. If your crobtab requires root access, make sure your logged in as root when you create the cron job. Cron jobs are created by editing the crontab file.
When setting up a cron job there are six arguments that must be defined in order (*Note: ).
3 Day of Month
5 Day of Week
#m h dom mon dow command ## Database backup 01 05 * * * /home/jasonsnider/shells/database_backup.sh
The above command will execute a backup of the database on the first minute of the fifth hour of each day of the month, each month of the year and each day of the week.
When it comes to shell scripts and cron jobs, this is just the tip of the iceberg, but hopefully it's enough to you going.
Checkout more shell scripts on GitHub.