There is no greater hope in human life than survival. At the same time, it is important to live well. With that in mind, every content on the website is written so that a person can get all the information from here to start his life to make beautifully.
According to that, Technology is one of the topics. It is also a part of life. Read carefully Details of Technology related article
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? How can I execute cron job on Linux after system restart?
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 crontab. You or user can use the crontab command to edit cron jobs. This page shows how to run cron job after Linux system reboot.
|Est. reading time||2 minutes|
Linux execute cron job after system reboot
The crontab command maintains crontab files for individual users. One can install or remove cron jobs as per needs.
Running job at startup (boot time)
You need to use special string called @reboot. It will run once, at startup after Linux reboot command. The syntax is as follows:
@reboot /path/to/job @reboot /path/to/shell.script @reboot /path/to/command arg1 arg2
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
It i a super-simple way to run scripts on boot using cron jobs.
How to run a cron job automatically after server reboot
Say your script needs to be run only after 5 minutes. For example: reboot + 5mintues. The syntax is as follows:
@reboot sleep 300 && /home/wwwjobs/clean-static-cache.sh
The sleep command to add delay for a specified amount of time. In this example, 600 seconds and after that time clean-static-cache.sh gets executed at boot time.
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 for Linux Execute Cron Job ###
# 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 the following systemctl command
# systemctl enable crond.service
### this to start/stop/restart crond on systemd 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
For Debian/Ubuntu Linux with systemd, try:
$ sudo systemctl enable cron.service
$ sudo systemctl start cron.service
$ sudo systemctl stop cron.service
$ sudo systemctl restart cron.service
$ sudo systemctl status cron.service
Save and close the file. For further information read out tutorial on cron jobs.
Getting push notification when the Linux system comes online
Specific jobs at AWS need working ssh to replicate cluster config and TLS file replication done using hooks. To keep it simple, Nginx would use ssh to send TLS files to all cluster nodes. However, this fails if the server rebooted for a kernel update. Hence, the developer and sysadmin need to log in manually into the box and add ssh keys using a keychain script. My cron job:
Sample /efs/scripts/push-reboot-notification that push/send message to iOS and Android from Linux CLI script:
#!/usr/bin/env bash source /efs/scripts/cli_app_pushover.sh push_to_mobile "$0" "The $HOSTNAME (AWS Debian) server was rebooted. Please login over ssh and add a new ssh key. Otherwise, a backup job and TLS certificate replication by nginx-sync will fail for sure. Boot time: $(last -x reboot | head -1)" >/dev/null
Did you like this article?
Share it on any of the following social media channels below to give us your vote. Your feedback helps us improve.
Other related Technologies ideas you might enjoy