Run a script or command on Linux CentOS Startup
Written by vaheeD on December 30, 2012
Looking to run a set of commands or a shell script on CentOS / RHEL / Fedora startup?
/etc/rc.local is what you’re looking for. /etc/rc.local (a symbolic link to /etc/rc.d/rc.local) is a script executed after the initial startup services have been executed.
Here’s an example of the /etc/rc.local script:
#!/bin/sh # # This script will be executed *after* all the other init scripts. # You can put your own initialization stuff in here if you don't # want to do the full Sys V style init stuff. touch /var/lock/subsys/local
How to execute scripts or commands on Linux startup
Simply append the command(s) or scripts you want to run on startup to this script.
For example, we will run a script in the /opt directory called kittens.sh that prints the last system logins when Linux boots.
Create the kittens.sh Linux startup script
touch /opt/kittens.sh && echo last >> /opt/kittens.sh && chmod +x /opt/kittens.sh
Add the script to Linux startup by adding it to /etc/rc.local
echo sh /opt/kittens.sh >> /etc/rc.local
You’re done! Next time you reboot your Linux solution, the kittens.sh script will run and you will see the last user logins.