Linux Execute Cron Job After System Reboot

by on February 17, 2009 · 9 comments· LAST UPDATED February 17, 2009

in , ,

I am on Red Hat Enterprise Linux 5.x server. 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
OR
# 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 read out tutorial on cron jobs.

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 9 comments… read them below or add one }

1 Linuxrebel February 17, 2009 at 9:26 pm

OK, But some caveats here.

1. Only works after a reboot. If you do a shutdown, or if the box crashes (as in plug vs vacuum) …. and then start, it doesn’t get run.

2. The @reboot command is not universal, especially missing on systems running micro or embedded Linux.

3. There is an alternate. the file /etc/rc.local is a place were you can place a command string to be run on boot, regardless of the means by which the system went down. Yes I know many Linux purists in favor of SysV style init scripts. However for a quick on liner it’s perfect. Here also gets run if you do a warm restart (as in go to init level 1 and then back to 3 or 5). Best this works across a very wide if not all spectrum of Linux systems. Anything in this file is always the last thing to run prior to login prompt appearing.

4. Write an init script, and put it in /etc/init.d with the appropriate run levels and timing. Harder to do and get right, somewhat, but it is the correct way to create an init entry. It also would allow for you to fine tune the timing. (need to run this job before something else gets started? Use this method)

Reply

2 Ashwani February 21, 2009 at 10:36 am

Nice..
linuxrebel is just looks ..right…but will an normal .sh script run on boot up if we put it in specified run level script like rc3.d ?…or what we need to do to follow what Linuxrebel said above??

Thanks Vk & Lr

Reply

3 pasourus February 15, 2012 at 7:22 am

The @reboot command not working. Finally got this and using for my Wordpress Site,
# touch /etc/init.d/memcached
# echo ‘#!/bin/sh -e’ >> /etc/init.d/memcached
# echo ‘/usr/local/bin/memcached -d -m 128 -p 11211 -u nobody -l localhost’ >> /etc/init.d/memcached
# chmod u+x /etc/init.d/memcached
# echo ‘/etc/init.d/memcached’ >> /etc/rc.local

Reply

4 Moray February 23, 2012 at 10:59 am

On CentOS 5.6, an /etc/cron.d/rebootlog crontab containing

@reboot root date >> /var/tmp/reboot.log

Is run when the system restarts after the commands “reboot”, “shutdown -r”, “shutdown -h”, “halt” and after a power failure. I did not find a shutdown method where the job was not run when the system restarted.

Reply

5 vedagiri April 9, 2012 at 7:34 am

how i check whether the cron job is running or not?

Reply

6 Nkaronji September 25, 2012 at 8:22 pm

If Cron is running a custom script then in your script you can write to a log file. Something like echo “Starting cronjob + timestamp” and echo “Successfully finished cron job + timestamp”

Reply

7 Philarete December 8, 2012 at 5:42 pm

With grep, it’s a little bit different. For example, to move every files including a given type of datas:
grep ‘<div id=' ./* | cut -f 1 -d":"|xargs -I {} mv {} ../YOUREP/{}

Reply

8 camypaj September 16, 2013 at 6:37 am

great tip, tnx! Made my day & solved a problem I had.. :)
Btw, I’ve just noticed a typo: command on redhat is “chkconfig” instead of “chekconfg”.

Reply

9 vamsi June 30, 2014 at 7:38 am

I used “chkconfig crond on”
Its working fine, thanks…

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , ,

Previous Faq:

Next Faq: