LL12 – Shell Scripting & Cron

First, special thanks to SUNY Chancellor’s Award recipient Mat Cantore for developing this content.

Ok, we’re going to bring many things we have learned to date together.  If you struggle with this lab you have just received notice that you have not done the necessary reading and research up to this point and you have significant work to do for the Linux Quiz (located in BB Course Conclusion LM).

 

Required Reading

The Linux Command Line by Shotts – 4.24

Wikipedia’s entries on: cron and shell scripting 

Also read about cron here:

Now as in the past, please read through this lab in its entirety before beginning (including the referenced links)

 

LL12 performed on HVCC AcadNX (Again, LL12 performed on AcadNX not your recently installed VM)

Intro ScreenShot

Open or launch your SSH application (e.g. Mac Terminal or Win PuTTy). Please resize your Terminal to make it larger to capture as much as possible but note you may not capture everything and this will be fine. If possible just take a screenshot/picture of your open terminal as if you include other items the text in the Terminal can be very small making it harder for me to see your commands. Paste this screenshot into your correctly named pdf document and label or title this screenshot “Intro Screenshot”.

 

12.2 – Previously we created a file using touch that had nothing in it and we created a file with vim to demonstrate cat/more/grep/etc. functionality but its time we create a file that actually DOES something (i.e. executes).  Something useful.  Something powerful.  Something amazing.  Ok, maybe not powerful and amazing, but it’ll do something, and with the concepts we learn, we’ll become more aware of system administration and be able to do a lot more in the future.

But first… background.  Yep.  Sorry, we need theory here.

Linux has the ability to perform a lot of different commands, as we’re learning.  However, one of the best features about command line Linux is our ability to create sets of commands that can then be executed by calling a single file.  We call this shell scripting.  It’s one of the most powerful things in Linux and we finally see the CLI is actually a programming environment.

So, we’re going to learn how to take a bunch of commands in a sequence, put them into a file, and then run them => a shell script.

 

12.3 – First, we need to edit (create) a text file to hold the commands. We know about vim but for diversity let’s use the nano editor to create a file titled “scriptDemo”. A nice intro to the nano editor is here.

To edit a new file, in the Terminal type nano scriptDemo and press enter.  You’ll be brought to a basic editor.  You can just start typing into the screen and have the text show up in the file.  Type ls -l on the first line, and then press CTRL + O.  (This means hold down the CTRL key and press the ‘o’ key… also look at the bottom of the nano screen for all CTRL + key combination commands).  Using the CTRL + O, you will be prompted to save the file (look near the bottom of the screen) and press enter (in previous versions you had to press Y, then press enter to confirm the file name so be aware of this in other version of nano).

You’ve now created a file, added some text, and saved it!  In Nano we use the arrow keys to move around so please use the arrow keys to move the cursor in the file in front of the command you just typed in, and type echo ‘Listing:’ and press the enter key to move the “ls -l” to the next new line.  The result should look like:

echo ‘Listing:’
ls -l

This time, type CTRL + X.  The file will save itself or you might be prompted to save the file again like above, only this time, you’ll be put back to the command line prompt. Now we’ve learned that every time we create or remove a file or directory we should perform a ls to verify we entered the command correctly and in this case we need to perform a ls -l to also look at the permissions.  Also, please verify the contents of the file by displaying it on the screen using one of the commands we previously covered.

Now we can’t run the file yet, because Linux doesn’t know it can run the file.  This is the mysterious execute privilege we didn’t cover as much earlier on (review rwx permissions in previous labs and readings as necessary).

We know how to add the execute privilege for the user using chmod and the following syntax chmod [ugo] +|-[rwx] but let’s become better SysAdmins and learn how to use binary numbers in conjunction with the chmod command so please see here: Using chmod with binary numbers

Now, using chmod and binary numbers, please add the execute privilege for user/group/world to scriptDemo (retaining their existing read and write privileges for user/group/world) and when finished, your permissions should look like the following:

-rwxr-xr-x  1  j.looby  j.looby  22  March 23 07:27 scriptDemo

You may also observe the scriptDemo filename changed color as it is now executable.

Exercise 12.3: Please take a screenshot of your Terminal showing your chmod command and a listing of your directory and paste this into your pdf document.

As long as the users’s permissions are set to rwx (or 7) you’ll be able to run the file since you are ‘user/owner’.

Please clear your screen after taking your screenshot and before continuing.

 

12.4 – To execute a file in linux, type ./<filename> and press enter.  So, in this case please enter:

./scriptDemo

Now you just executed your first script.

Exercise 12.4: Immediately below the script output, display the contents of scriptDemo in the terminal, take a screenshot of these items and paste into your pdf document.

Please clear your screen after taking your screenshot and before continuing.

 

12.5 – We can edit an existing file by typing nano <filename>.  So, type nano scriptDemo and press enter.  You’ll be back in the editor with the file’s contents.

Make your file look like the following with one command per line:

echo “Sleeping:”

ps -a

sleep 60

echo “Listing:”

ls -l

Then,  press CTRL + X and follow the steps to save it (look at the bottom border and follow its guidance)

Exercise 12.5: Run scriptDemo in the background, execute the command to show all processes and take a screenshot while the script is sleeping and then take another screenshot when the script completes and paste both screenshots into your pdf file under this heading (if you don’t have enough time, put the script to sleep for a longer period).  Have a good look at the first screenshot and the status of the script commands.

Please clear your screen after taking your screenshot and before continuing.

 

12.6 –  cron is a Linux command that allows us to schedule the execution of pre-designed scripts at specific days and times.

Now let’s prepare our file system for the rest of this lab.

Make a new directory called cronScripts directly under your home directory.

Move the scriptDemo file into the cronScripts directory.

Modify scriptDemo so instead of sleep 60 it says sleep 5.

Always make your script executable by user, group, and others (i.e. use a long listing and/or test your script by executing it as above)

Exercise 12.6 – perform a long listing of your cronScripts directory and display the contents of scriptDemo on the screen.  Take a screenshot and paste this into your pdf document.

Please clear your screen after taking your screenshot and before continuing.

 

12.7 – Now we need to create a cron file as this tells cron what to do when.

In your home directory, we will edit (create) a file called cronFile.  This is the accepted convention for the file name, but really, you could name it anything you want. 

Now, cron has a very specific format that tells it when to run things, and where to find the things to run.  So, we need to know the current system date and time so we can practice this.

Type date and press enter.   Make careful note of the current time, especially.  Not so much the date.  Mine looked like => Sun Aug 18 11:51:39 EDT 2013

I’m keying in on 11:51:39.  I am going to set my cron job to run in the future at 11:55, so I can see if it worked.  Based on that, this is what I enter in my cronFile:

55 11 * * * /home/j.looby/cronScripts/scriptDemo >> /home/j.looby/cronScripts/cronScript.log

(Note: The above should all be on a single line in cronFile).

There are a lot of pieces to this.  First, note that MINUTES GO FIRST, then the hour so this is to be run at 11:55.  Cron uses military time, so if I wanted 1 pm, that would be 13 in the second spot rather than 11. The other three spots, noted as an *, let you specify a day of the month, specific month, and/or day of the week to run something on (they are command arguments so they are of course separated by spaces).  As an example, I could set something to run the first sunday of every month, or the 3rd day of every month, or even the 1st day of every other month.  An * is a wildcard, so it will run anytime that is matched.  If I said 53 * * * * that would equate to 53 minutes of every hour, every day, of every month.  Ok, enough with this.  Let’s get on with it.

Following the third * above is the actual script to run and again it must be executable.  Note the absolute path.  Change the path above to fit your script (i.e. your username and cronScripts directory).  Recall the >> is of course append redirection so if the file does not exist, it will be created, if it exists, it will be appended.  So, cronScript will create a log file.  Again, adjust this for your own directory.

 

12.8 – Now we have to add our cronFile to the cron table to register it for cron so in the Terminal enter, crontab cronFile and press enter.

If you want to verify this is scheduled in the cron table type crontab -l and press enter and you should see your entry (of course you can use the manual pages to read more about cron and crontab).

Now, wait until the necessary time passes and monitor whether your script has run by looking for the log file in your cronScripts directory.  It should magically appear! Now if you took too much time to get this setup (i.e. timeframe passed before cron could run the script), perform another date command, edit your cronScript file setting for another future time, register your cronScript with cron in the cron table and verify this is working.

Exercise 12.8 – Re-edit the cronScript file to run a few minutes in the future and re-register cronScript in the Cron Table.  After cron has run your script, from your home directory, in order, display the contents of the cronScript, scriptDemo and cronScript.log files on your screen.

Please clear your screen after taking your screenshot and before continuing.

 

12.9 – We don’t REALLY want this simple script to run every day so

Go back to your main directory, edit cronFile, and place a # in front of the one line. You know this turns it into a comment, meaning cron won’t execute it.  Don’t forget to re-load cron: crontab cronFile does the trick!

 

12.10 – Please submit your assignment in BlackBoard.

Now if you really wish to understand and explore Linux Shell Scripting here are two excellent resources: 1. tutorial and resource 2: tutorial and resource

Leave a Reply