ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

FreeBSD Own bootup script

Updated on May 9, 2009

Quick background

When you start working with FreeBSD you find yourself one day getting a brainwave to write a script that you need to load during boot. This is the method I have found most usefull in those times.

Comments in files

Yes, terminology may not alway be correct, you say 'parameter' when technically it is a 'value', so what... when learning its about understanding what you do. Later on you will come across it again, see the mistake and fix it. The main thing is that you understand what you need to do, and why you did it. You will find a way you find most helpful

/usr/local/et/rc.d/ = (dos - autoexec.bat | windows - start up)

Ok, REALLY crossing dangerous lines here with that statement. In simple terms for you to make a comparison. As DOS would use autoexec.bat and Windows would use the 'Start -> Programs -> Start Up' you need to understand that this is how FreeBSD reads and executes that it starts during boot. This goes deeper... much much deeper, for that read the manuals, now just understand the concept.

Quick and Dirty

Make yourself a template

When you look at all the scripts in /etc/rc.d/ and /usr/local/etc/rc.d/ you will see they have a commom structure. Create a template and comment it in your own words.Calling this one rc.d.template

# PROVIDE: my_script
# KEYWORD: shutdown
# Add the following lines to /etc/rc.conf.local or /etc/rc.conf
# to enable this service:

# my_script_enable (bool):   Set to NO by default.
#               Set it to YES to enable.
# This calls the rc.d framework to make all the functions work 
# in this script
. /etc/rc.subr

# in the /etc/rc.conf, application_enable="YES".. 
# this is where it gets its name from

# to start the command (start_cmd), 
# run the function start_my_script() [named whatever you want]

load_rc_config $name

# default value for the rc.conf
: ${my_script_enable="NO"}

# this is the 'start_my_script' in the line above 
# "start_cmd=start_my_script"
# start_my_script() is a standard sh function
  # path to interpreter (sh) then path to script then( 
  # and then any values you need to pass to the script to make it 
  # run (up)
  # MAKE SURE script is executeable (chmod a+x
  /bin/sh /path/to/ up

  /bin/sh /path/to/ down
  /bin/sh /path/to/ down
  /bin/sh /path/to/ up

run_rc_command "$1"

All most done.

Creating the rc script

Make sure your script is execuatable

chmod a+x /path/to/

Copy your template to the /usr/local/etc/rd.d directory

cp /path/to/rc.d.template /usr/local/etc/rd.d/my_script

Edit the new my_script and replace / remove / add values and functions as you please with your text editor of choice

See how the rc script is recognised.

/usr/local/etc/rc.d/my_script rcvar

You will get a response (depending on your default value)

# my_script

Nice, now add the script to your rc.conf and test

/usr/local/etc/rc.d/my_script start

All done! Now your script will execute during boot.

Trouble shooting

This can get trick specially if you are not too familiar with bash, make sure you have no syntax errors, this may take a short time to get use to as sh can be a bit fusy on where and how its functions and statements are called


Hope this helped someone out there, even if it was just to understand how the puzzle pieces fit together. Comment and suggestions more than welcome.

Until we meet again =]


    0 of 8192 characters used
    Post Comment

    No comments yet.