I need to run command at startup on crontab. How can we use crontab to schedule jobs to be run at system reboot? Say I want to run command called /root/scripts/reboot-job
. It needs to be run when we reboot the Linux box for kernel update or hardware update. Is it possible?
Run once, at startup using the syntax
@reboot command
1. Run it as root
crontab -e
2. Ask cron to run rask on boot / startup time on Linux
@reboot /root/scripts/reboot-job
Save and close the file.
How to add a delay to task on boot on Linux
The @reboot
run job when crond start i.e. is the time when the cron(8) daemon startup. In particular, it may be before some system daemons, or other facilities, were startup. This is due to the boot order sequence of the machine. We can run a job with a delay after the system reboots, by using the sleep command when adding the @reboot
:
# run /root/scripts/reboot-job after 10 minutes (600 seconds) #
@reboot sleep 600 && /root/scripts/reboot-job
How to remove @reboot
entry
If you no longer wish to run a job after Linux system starts, remove it from the task list. All you have to do is run the following as root:
crontab -e
Look for the jobs you added and delete it. Make sure you save and exit the text editor.
I have page that provide detailed information how to execute a job automatically at Linux server boot time:
https://www.cyberciti.biz/faq/linux-execute-cron-job-after-system-reboot/
Just a word of caution: I had a LOOOOT of trouble with this the last two or so years. I had @ reboot running successfully in my scripts for years, that is until the SystemD thing started creeping in. I could never find SystemD 's replacement of SysVinit execute @reboot reliably. Somehow SystemD messed with cron entries.
It was only after I completely uninstalled SystemD that my @reboot calls in cron worked again.
It was no easy task to remove systemD in Debian, but I now have a system that runs like greased lightning and way better after I rid myself of it.
It is also sometimes useful on some distros to install anacron to get @reboot to work reliably.
@reboot issues were just one of the myrrad of problems I traced back to SystemD.
So if @reboot doesnt work reliably for you. Take a look at getting rid of SystemD or use a SystemD free Distro such as in this list.
https://en.wikipedia.org/wiki/Category:Linux_distributions_without_systemd
-
- You can also try to install anacron and see if that helps @reboot to work, or if you have it installed, uninstall it and use or install cron and see if @reboot works.
-
- Also remember that there is not an exact fixed time that @reboot will execute. It will loosely execute before or sometimes after xserver is up dependent on distro and runlevels.
-
- If the program or script you try to start at startup needs X and a user to be logged in, you will have to write a script that is triggered by @reboot, and then the script will wait for the user to log in before it executes the X application or X-dependent daemons.
-
- If you just want to start system daemons just the program & path name is sufficient.
-
- Best way to test it first is to just do something like @reboot echo “@reboot working” >/root/reboot.dat and you can check the file creation date time. That way you know that @reboot was actually executed.
-
- Most of the times when @reboot is suspected not to work, it is due to the fact that the particular program or script is not at the correct runlevel or the user has X-depndent programs or scripts.
-
- I yet have to get @reboot to run from a user crontab. I have to do all from my root account where it works.
-
- So dont be surprised if @reboot doesnt work in normal user accounts.
These should be your first steps.
- So dont be surprised if @reboot doesnt work in normal user accounts.
Once you confirmed that @reboot actually executes e.g. by the file created in my example above, then you can run your kernel update or hardware update scripts.
I don’t have time to remove systemd. if i left job others will find such system difficult to use. hence, i will use @reboot
but you have got good point. thank you.
Removing SystemD is surely not a prerequisite. It is just my expereince that @reboot generally chokes when it is present. I am sure other users might get it to work reliably with SystemD. I could not, but it could be due to my level of understanding.
Try any case along my rough suggestions to find why it doesnt work.
My suggestions are arguably trivial, but a reasonable approach to troubleshoot based on my experience.