Friday, June 18, 2010

Schedule commands with the Cisco IOS

Have you ever wanted to schedule a command to run on a regular basis on a router? Then the Cisco IOS built in command scheduler called "kron" is for you. This command was introduced in Cisco IOS 12.3(1) and has been updated in 12.4.

The command scheduler will allow you to run commands, or a sequence of commands once or on a recurring basis. This is very similar to the Windows "at" command.

A common request that people have asked me for is to copy a router's running-config to startup-config or to a TFTP server on a certain day of the week every week. This will ensure that no changes are lost if someone forgets to save the config or the router dies.

Here is an example: To get a router to copy the running-config to startup-config every Sunday at 23:00, do the following:

Step 1) Kron policy list

Create a kron policy list - this is your script which lists what commands the router should run at the scheduled time

Router(config)# kron policy-list SaveConfig
Router(config-kron-policy)# cli write
Router(config-kron-policy)# exit

Note:
cli - Specifies EXEC CLI commands within a Command Scheduler policy list.
policy-list - Specifies the policy list associated with a Command Scheduler occurrence.

IMPORTANT: The reason why "write" was used rather than "copy running-config startup-config" is because kron does not support interactive prompts and "copy running-config startup-config" requires interaction. It is important to remember this when creating commands. Also note that kron does not support configuration commands.

Step 2) Create a kron occurrence

Create a kron occurrence, in which you tell the router when and how often you want to run the policy.

Router(config)# kron occurrence SaveConfigSchedule at 23:00 Sun recurring
Router(config-kron-occurrence)# policy-list SaveConfig

Note:
SaveConfigSchedule - Name of occurrence. Length of occurrence-name is from 1 to 31 characters. If the occurrence-name is new, an occurrence structure will be created. If the occurrence-name is not new, the existing occurrence will be edited.
at - Identifies that the occurrence is to run at a specified calendar date and time.
recurring - Identifies that the occurrence is to run on a recurring basis.

3) Verify

Verify that you've entered everything correctly by using the show command.

r1#sh kron schedule
Kron Occurrence Schedule
SaveConfigSchedule inactive, will run again in 1 days 12:37:47 at 23:00 on Sun

You can see that the schedule is ready to go and will run at the date above.

Note:
inactive - means that kron is not running the command(s) at present. Active means that kron is running the current command(s)


Verify that kron works via debugs:

Router#debug kron exec-cli

Dec 17 22:59:59.999: Call parse_cmd 'write'
Dec 17 23:00:01.587: Kron CLI return 0
'
**CLI 'write':
Building configuration...[OK]'
Dec 17 23:00:59.999: Call parse_cmd 'write'
Dec 17 23:01:01.559: Kron CLI return 0
'
**CLI 'write':
Building configuration...[OK]'

4) View the output of your configuration:

Router# show running-configuration

kron occurrence SaveConfigSchedule at 23:00 Sun recurring
policy-list SaveConfig

kron policy-list SaveConfig
cli write