Thursday, August 9, 2012

How to execute a program at regular intervals in Ubuntu

Automatically executing a program at regular intervals in Ubuntu is undertaken using the Linux's/Unix's cron command. Here are the steps:

Step 1: Create the crontab file using an editor. For example:

# joe mycrontabfile.txt

Step 2: Enter the instructions for cron to follow. For example:

0 14 15,28 * * root /etc/firewall/firewall.sh

The instruction format is:

minute hour day month weekday user command

The values for minute is 0-59, hour is 0-23, day is 0-31, month is 0-12, weekday is 0-11. The asterisk character (*) is a wildcard and a comma (,) is a separator.

The above example means execute the program firewall.sh in the directory /etc/firewall with user root at 2pm on the 15th and 28th day of the month.


Step 3: Load the crontab file using the crontab command:

# crontab mycrontabfile.txt

Autoexecute a program in Ubuntu at Boot

Assuming you have a script file named "firewall.sh", to automatically execute the file at boot time in Ubuntu, issue the following commands:

# mv firewall.sh /etc/init.d/
# chmod +x /etc/init.d/firewall.sh
# update-rc.d /etc/init.d/firewall.sh defaults


To remove the autoexecute command, type:

# update-rc.d -f /etc/init.d/firewall.sh remove