Friday 24 April 2015

TIPS & TRICKS: To run a script on system reboot

In this post, I will explain a few tips to run a script soon after reboot. This feature worth useful in many cases such as, to setup an mail alert system regarding the system reboot, so that, I will always be informed via mail whether my system is rebooted or not.

TIP 1:  BY USING CRON
------
Suppose, I have a script named  "/home/manoj/files/run.sh" and it needs to be run on every boot. Open the cron file in editable mode using  "crontab -e" and add the following:

--------
@reboot /home/manoj/files/run.sh
--------

Now, every time my system is rebooted the script "run.sh" is executed.


TIP 2:  BY USING /etc/rc.local
--------
The /etc/rc.local file is executed by the init process at the end of the systems boot process.

Inorder to execute the script  "/home/manoj/files/run.sh" on reboot, you can open "/etc/rc.local" and append the line as below:
-------
sh /home/manoj/files/run.sh
-------

NOTE:
------
1) Make sure crond service is enabled in chkconfig
2)  755 permission is available for "run.sh"

IMPORTANT: NEVER USE /etc/rc.local for startup of services
----------
Never, use "/etc/rc.local" file to start services on reboot. For example, if you are adding apache service to "/etc/rc.local", everytime the system reboots, apache will not shut down gracefully. This will create more issues. Always use chkconfig for service startup on reboot.
---------

Kool :)

No comments:

Post a Comment

Note: only a member of this blog may post a comment.