EPM Linux Lab – Cron

April 5, 2020

 

When using a planning and budgeting system, scheduling jobs to load data, update hierarchy, etc. is necessary. The task scheduler is commonly used for Windows systems. What if a file transfer and automation server is using a Linux system? Most likely, we would come across using Cron.

 

Cron is a service that allows us to run scripts at specific dates and times. We can use Cron to create and manage common tasks.  This time, let’s spend a bit of time and take a look at some basic ideas of using Cron.

 

­Cron is a server and the commands to be executed are stored in a table, which is called crontab. I will walk you through how to access the crontab and how to run scripts at defined times.

 

The crontab files are located in the /var/spool/cron directory.

 

To enter crontab, use

crontab –e

command to open the crontab file.

 

To edit the crontab file, use vi editor. Vi editor opens by default when calling crontab –e. If you prefer using Nano editor. Use

EDITOR=nano crontab –e

command.

 

Vi has 2 modes, insert mode and command mode. Use I key to open the insert mode. Under insert mode, we will be able to enter characters in the text. To switch to command mode press the ESC key.  Please note that vi cannot stop in insert mode. We have to switch to command mode to exit vi.

 

As we mentioned, all commands to be executed by cron are stored in a table, which is called crontab. It consists of the following columns:

*  Minutes (0-59)

*  Hours (0-23, 0=midnight)

*  Days (1-31)

*  Months (1-12)

*  Weekday (0-6, 0=Sunday)

xxx.sh   Command to be executed.

 

The first five columns contain time information. These entries cannot have spaces. The last column contains the command or directory and name of the script file. When entering a command, make sure we always enter the complete path.

For example,

This backup job will be running at 1:01 am daily.

 

The backup will be running every 10 minutes.

 

The backup will be running at 1am, 2am, and 3am.

 

The backup will be running from January to June at 1:01am, 1:31am, 2:01am, 2:31am, daily between the 1st and the 15th of the month.

 

To check the existing cron jobs, use,

crontab –l

 

Some basic and import vi commands.

  • Close the editor without saving changes       :q!
  • Save the text and close the editor                 :wq
  • Insert a new line under the current line          o
  • Undo the last change                                     u
  • Delete the current line                                   dd

All of the cron actions will be entered in the $CRON_LOG_FILE log file, which is located in the /var/spool/cron directory.

 

Once the jobs are scheduled, another import thing is setting email alert to admins. In the even a task runs into errors, cron should be able to send an email to admins. We can use the MAILTO variable.

 

To change the MAILTO variable, add

MAILTO=admin1@domain.com

Into the crontab file.

 

To add multiple receivers, simply add spaces between the emails.

 

I will spend more time on sharing Linux ideas in the future. Hopefully, you enjoy this post. Until next time.

 

 

Leave a Reply