“Using Unix Cron to Run Automated Tests” by Reynold Miguel

Reynold Miguel is a QA engineering leader with a strong focus on bringing automation to organizations. His deep technical knowledge and understanding of technology enables him to discuss more complex concepts, like using cron to setup automation, while maintaining a focus on the value and broader organizational importance of the technique. 

Using Unix Cron to Run Automated Tests, by Reynold Miguel 

The software utility cron is a time-based job scheduler in Unix-like operating systems. People use a cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals.  Using cron jobs are a great way to run scheduled tests. Running tests in Jenkins provides more functionality, but crons are a quick shortcut in case you want to run your initial set of tests right away on a schedule. The benefit of this approach is the ability to schedule tests in a matter of minutes, without need for a whole lot of setup. You can also utilize crons for any external processes that you need for your tests, e.g. data loading, sending emails, etc. At our company we initially used crons for running tests, but since transitioning to Jenkins we use crons primarily for running various processes like data loading and sending test emails in our QA environment.

Needed Tools:

  • Unix/Linux/Mac
  • Automated Tests that are runnable from command line, or any runnable process
  • Basic knowledge of Unix and vi editor

Step 1: Have a Job You’d Like to Schedule

Here’s our job that we use for running our tests, we use a customized framework built on top of Selenium Web driver and Ruby.  This test can be run from the Unix command line. $ bin/autobot –connector=firefox –env=qa 2>&1 | tee data.log What do these commands mean?

  • This line is how we run our customized set of selenium tests, on firefox and in our qa environment: bin/autobot –connector=firefox –env=qa
  • And these are Unix commands that allow us to append the test results to a log file so we can look at it later: 2>&1 | tee data.log

Step 2: Setting up a Cron Job

Type the following in the terminal: crontab -e
If you do not have a crontab file, one will be created for you and opened in the vi editor. Setting up a cron job consists of creating, or adding to, your own crontab file.  Each job needs a line containing six entries, each of which is separated by spaces or tabs. The first five entries are integer patterns specifying the following:

  • Minute (0-59),
  • Hour (0-23),
  • Day of the month (1-31),
  • Month of the year (1-12),
  • Day of the week (0-6 with 0=Sunday).

Each of these patterns may be either an asterisk (meaning all legal values) or a list of elements separated by commas. An element is either a number or two numbers separated by a minus sign to indicate an inclusive range.

A minute specification of 0,30 would indicate the job is to be run on the hour and half hour. Likewise, a day of the month entry of 1,15 would initiate execution on the first and fifteenth of the month. The sixth field of a line in a crontab file is a string that is executed by the shell at the specified times. A percent character in this field (unless escaped by) is translated to a new-line character. Cron jobs are being executed from a system directory. Make sure you include and explicit path to your script. In our case, let’s say we want to run the tests every 30 min. You can also use a “#” to comment your scheduled jobs.

#This will run our tests every 30 min
0,30 * * * * bin/autobot –connector=firefox –env=qa 2>&1 | tee data.log

Step 3: Save the Cron and Let it Run

And once you save it you’re done!  You’ll receive a message that should say something like: crontab: installing new crontab

Crontab Commands:

  • crontab -e   The -e option spawns an editor containing the user’s current crontab
  • crontab -r   The -r option removes a user’s crontab from the crontab directory
  • crontab -l   To see the list of jobs use the -l option

Remember, when you save the file, it automatically updates the crontab when exiting the editor.

Pin It on Pinterest