Tuesday, June 23, 2009

Execute Cron Job After System Reboot on Linux

Is there is an easy way to run script or command at boot time after fresh reboot command?

crontab is the program used to install, deinstall or list the tables used to drive the cron daemon in Vixie Cron. Each user can have their own crontab, and though these are files in /var/spool/cron/crontabs, they are not intended to be edited directly. You or user can use crontab program to edit cron jobs.

Running job at statup (boot)

You need to use special string called @reboot. It will run once, at startup after reboot command.
@reboot  /path/to/job
@reboot  /path/to/shell.script
@reboot  /path/to/command

This is an easy way to give your users the ability to run a shell script or command at boot time without root access. First, run crontab command:
$ crontab -e

# crontab -e -u UserName
# crontab -e -u vivek

Run a script called /home/vivek/bin/installnetkit.sh
@reboot /home/vivek/bin/installnetkit.sh

You also need to enable crond service via sys v / BSD init style system. Under RHEL / CentOS / Fedora, you need to use chkconfig (ntsysv) command to enable crond on boot:
# chekconfg crond on

Under Debian / Ubuntu Linux use update-rc.d as follows to turn on service on boot:
# update-rc.d cron defaults

Save and close the file.

For further information see this tutorial on cron jobs.

No comments:

Post a Comment