Automatically Start a Script at Linux Bootup
Say you want to automatically start programs when starting up Linux. A quick and easy way to make a script or program start when Linux does is through the init scripts located in the /etc/rc2.d/ directory. These scripts are called in order of priority when Linux enters runlevel 2. Your default runlevel may be different than 2, so be sure to check with the ‘runlevel’ command or in the /etc/inittab. In order for the scripts here to start, they must follow a three-part naming schema. For example: /etc/rc2.d/S10thisscript. This script will Start with priority of 10 when the system enters runlevel 2 and is described as thisscript. The description doesn’t matter, but it is useful to give it a meaningful name. For more information, please see this page.
Here’s a very quick and dirty walkthrough that will change the IP address whenever the system is booted:
echo "ifconfig eth0 192.168.1.102" > /etc/init.d/changeip chmod +x /etc/init.d/changeip ln -s /etc/init.d/changeip /etc/rc2.d/S10changeip
So, you can see that we create a script called changeip in the /etc/init.d/ directory, gave it executable permissions, and then created a symbolic link to /etc/rc2.d/S10changeip so that it will run when the system is started. As a general note, the script can be located anywhere as long as the symbolic link is targeted at /etc/rc2.d/S10changeip.
If you are going to be calling a binary that needs to run in the background, you’ll want to use the nohup and sleep commands like below to avoid having your application be terminated with the init process (the sleep command will prevent the nohup command from being killed before it’s initialized — ironic):
nohup myprog & sleep 1
Along the same lines as the above trick, a script can be made to run whenever a user logs in by placing the name and location of your script into the ~/.profile directory. For example:
# ~/.profile: executed by Bourne-compatible login shells. if [ "$BASH" ]; then if [ -f ~/.bashrc ]; then . ~/.bashrc fi fi mesg n echo "Now running user script..." /home/username/changeip
Happy scripting!
Note: I’ve just learned that on Debian systems, all you may need to do is create the script, make it executable, and then call the “update-rc.d” command like such:
chmod +x the_script_name update-rc.d the_script_name defaults
Source: http://embraceubuntu.com/2005/09/07/adding-a-startup-script-to-be-run-at-bootup/
Feel free to donate if this post prevented any headaches! Another way to show your appreciation is to take a gander at these relative ads that you may be interested in:
Here are some similar posts that you may be interested in:
There's 9 Comments So Far
November 30th, 2011 at 11:15 am
Hi DEREK, thanks for posting this and I think that is is really what I needed. But I must be doing something wrong or missing something.
I basically created new a script and I called it lrgenerator under the /etc/init.d/ directory, and the script content looks just like this:
export PRODUCT_DIR=/opt/HP/HP_LoadGenerator
export M_LROOT=$PRODUCT_DIR
export LD_LIBRARY_PATH=${M_LROOT}/bin
export PATH=${M_LROOT}/bin:$PATH
/opt/HP/HP_LoadGenerator/bin/m_daemon_setup -install
so I did the following:
chmod +x lrgenerator
ln -s /etc/init.d/lrgenerator /etc/rc2.d/S10lrgenerator
I tried to reboot the server, and tried to echo the value of M_LROOT
echo $M_LROOT
and it just comes as blank. So my take is that it didn’t really run the S10lrgenerator symbolic link upon restarting the server. Can you please advise regarding this issue?
Thanks,
Manny
November 30th, 2011 at 11:07 pm
What’s your default runlevel? Run ‘runlevel’ at the command line and see what number is returned. It’s possible that “2” isn’t the default runlevel on your system.
December 2nd, 2011 at 11:35 am
Hi Derek,
As per your guide, having the symbolic link on /etc/rc3.d/S10lrgenerator
worked perfectly, Thanks a lot!
February 7th, 2012 at 8:01 am
But if your system for any reason get rebooted the system will change IP not sure if its a good solution how do you stop that once it runs
February 7th, 2012 at 8:08 am
The IP address example was contrived. It’s not meant to be useful in any way, but rather explain how to create the startup script.
January 8th, 2013 at 8:53 pm
my php session path is in /dev/shm/session
but every reboot, folder session gone.
how to auto create /dev/sh/session and chmod nginx:nginx to the folder
November 24th, 2014 at 2:21 pm
Thanks!
Who Linked To This Post?
Share your thoughts, leave a comment!