Linux Execute Cron Job After System Reboot

I am on Red Hat Enterprise Linux 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. [donotprint][/donotprint]You or user can use crontab program to edit cron jobs.

Running job at startup (boot time)

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

@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

Start crond automatically at boot time

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:
# chkconfig crond on
## Commands to start/stop/restart crond ###
# service crond restart
# service crond start
# service crond stop

Under Debian / Ubuntu Linux use update-rc.d as follows to turn on service on boot:
# update-rc.d cron defaults
If you are using modern distro with systemd, try
# systemctl enable crond.service
### this to start/stop/restart crond on systmd enabled distro such centos 7.x/debian/ubutnu/arch ###
# systemctl start crond.service
# systemctl stop crond.service
# systemctl restart crond.service
# systemctl status crond.service

Save and close the file. For further information read out tutorial on cron jobs.

This entry is 10 of 15 in the Linux Cron Jobs Howto & Tutorial series. Keep reading the rest of the series:

  1. What is cron on a Linux or Unix-like systems?
  2. HowTo: Add Jobs To cron Under Linux or UNIX?
  3. Linux Verify crond Daemon And Cronjobs Are Running
  4. Linux Start Restart and Stop The Cron or Crond Service
  5. Linux: List / Display All Cron Jobs
  6. Linux / UNIX Crontab File Location
  7. Linux / UNIX: Change Crontab Email Settings ( MAILTO )
  8. Disable The Mail Alert By Crontab Command On a Linux or Unix-like Systems
  9. Linux: At What Time Cron Entries In cron.daily, cron.weekly, cron.monthly Run?
  10. Linux Execute Cron Job After System Reboot
  11. Linux / UNIX Setup and Run PHP Script As A Cron Job
  12. How to run crontab job every minute on a Linux or Unix-like system
  13. Run crontab (cron jobs) Every 10 Minutes
  14. Cron Job Script Execution on the Last Day of a Month
  15. Run crontab (cron jobs) every 1 minute

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.