Run a script at startup on Linux

If you need to run few commands or a script at boot, just for one time to set some settings or to mount few folders or to set iptables rules or set processor power saving and settings ..etc, you can do so using crontab.

Simply, place all the commands you need to run in a file, lets name it “/etc/startup-cmds”, and then add a reference to it in the crontab file in /etc/crontab with this line

First, use this command to make this file executable

chmod +x /etc/startup-cmds

Then, edit the /etc/crontab file

nano /etc/crontab

and append this line

@reboot  root  /etc/startup-cmds

This way, those commands will run with permission level as root, if you do not need to run them as root, replace ‘root’ with the username to run as it.

Note: This is not the best way to start an application or daemon if it has its own init.d script in /etc/init.d/ or in /etc/init/

Note2: This is not best way to run scripts that modify critical security settings, like settings iptables security rules, because it might not execute right after the network interface is brought up.

Leave a Reply

Your email address will not be published. Required fields are marked *

(Your message will only be visible after moderation)