Code Comments

Code Comments

Tips and short tutorials on various programming technologies

Code Comments RSS Feed
 
 
 
 

How to Run a Simple Command at Startup in Linux

In Linux, you sometimes want to run a command when the server first starts up. I was doing this recently where I would have to manually go in and run a command each time the server got rebooted. This was a pain, so with a little help from my friend Martin, I learned how to have that script run automatically each time. There is a more advanced way to handle startup scripts, which is to use the /etc/init.d/ directory. I won’t go into that. You can Google it if you want more detail. To have a simple command run at startup, go to the /etc/rc.local file. And past the command at the end.

Below is what my file looks like (or close to it). This runs a script called MyScript. Make sure the script has execute permissions and that you have the full path to the 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
 
/path_to_script/MyScript &

Leave a Reply