Hints and Tips Topic
   >  Changing the Hostname
   >  Finding your IP & Mac Address
   >  Creating Users and Groups
   >  Running Scripts at Startup
   >  Network Interface Configuration
   >  Changing the Grub Default Option

 

Running Scripts on Linux Startup

Running Scripts on Linux Startup

Sometimes you have a script that you want to run at system startup time - for example, a script to tweak some of the system settings the way you want them. However, the method given in the Desktop Guide may not be appropriate as:

  • The script may need to run as root and not the logon user
  • The PC is used by multiple users and we want the script to be run when the PC is first powered up

In these cases, the easiest thing to do is to add the command(s) to run your script into the /etc/rc.local file

In these cases, the easiest thing to do is to add the command(s) to run your script into the /etc/rc.local file. This is a file that Linux supplies as part of the kernel that is run at startup time and which (by default) does nothing. Here is what it looks like by default (-note that any line beginning with a "#" is a comment and is ignored):

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

exit 0

This sounds a little daft, until you understand that it is a empty container that allows you to add your own startup commands into the startup routine. For example, you might want to run a script to set up your firewall rules; in this case all you need to do is to edit /etc/rc.local and add the highlighted block below:

>#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Set the firewall rules (Fred Bloggs, 7th January 2011)
/myDir/setupFirewall.sh

exit 0

Note: it is always important to comment (#) and date any lines added to system configuration files so you can identify the change when you come back to it in the future


HomeSite IndexDesktop GuideServer GuideHints and TipsHardware CornerVideo SectionContact Us

 sitelock verified Firefox Download Button