LL11 – Shell Scripting & Cron

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

We’re going to bring many things we have learned to date together.  If students struggle with this lab they have just received notice that they have either not learned to read with the exacting attention detail as is required in this discipline or you have not done the necessary reading and research up to this point.  Again recall my many communications that it is possible to follow my directions to a certain point but without the continual rereading of past labs and required readings, students will hit a wall due to a lack of understanding.  If  students struggle with this lab please use it as a notice that they have significant rereading to perform to complete this LL11, the Final Project and Linux Quiz. 

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)

LL11 is performed on HVCC AcadNX server (Again, LL11 performed on AcadNX not your recently installed VM)

Intro ScreenShot

Open or launch your SSH application. 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”.

11.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.  With this 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.

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

Put another way, building on the previous link’s resource,  permission is indicated by a 1 and not permitted by a 0 so as another example,  permission to read and write but not execute would be 110 and this convention is extended and used for the 3 distinct user groups (i.e. user, group and all).  The rest is just binary interpretation (i.e. convert 0b110 to decimal and please see my previous numbering systems docs/tutorials if you have forgotten -> LM2).

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

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

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

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

11.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 (Midnight is 00, Noon is 12). 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 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 and it is this log file that will show you your cronFile/scriptDemo worked correctly.  Again, adjust this for your own directory and make sure you check your log file to determine if you were successful.

11.8 – Now we have to add our cronFile to the cron table using the Linux crontab command 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 and at the log file.  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 11.8 – Re-edit the cronFile to run a few minutes in the future and re-register it in the Cron Table.  After cron has run your script, from your home directory, in order, display date and the contents of the cronFile, cronScripts with permissions, scriptDemo, and cronScript.log file on your screen with the Linux prompt. Please make sure to show the command used to display these items as well.

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

11.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!

11.10 – Please submit your assignment.

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

Getting Help From Your Faculty

First, please consider what we did:

        1. We created the scriptDemo, made it executable and tested its execution
        2. We created the cronFile with: 
          1. Time to run
          2. What to run
          3. Redirect output into the log file
        3. We enter the cronFile into the cron table using the cronTab command
        4. Cron runs cronFile as listed from its cronTable which ran your scriptDemo and directed the output to the log file
        5. If everything is correct the log file will have the redirected output from scriptDemo

If your log file doesn’t show up or is empty or has errors, you have done something wrong so please carefully recheck every step and please recognize the opening paragraph of this lab copied in bold italics below.

Please note I/we cannot assist with incomplete information so if you need help, as with previous labs, please specify the exact step you are on and please send screenshots of all relevant directory long listings (i.e. home and cronScripts) and contents of relevant files (e.g.  scriptDemo and cronFile), the cron table listing and the date command in this lab.

If you struggle with this lab you have just received notice that you have either not learned to read with exacting attention detail as is required in this discipline or you have not done the necessary reading and research up to this point and you have significant work (e.g. rereading past labs and required readings) to complete this LL11, the Final Project and Linux Quiz.