FreeBSD Own bootup script

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

#!/bin/sh
# PROVIDE: my_script
# REQUIRE: LOGIN
# 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
name="my_script"
rcvar=${name}_enable

sig_stop=-KILL 
# to start the command (start_cmd), 
# run the function start_my_script() [named whatever you want]
start_cmd=start_my_script
stop_cmd=stop_my_script
restart_cmd=restart_my_script

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
start_my_script()
{
  # path to interpreter (sh) then path to script then(my_script.sh) 
  # and then any values you need to pass to the script to make it 
  # run (up)
  # MAKE SURE script is executeable (chmod a+x my_script.sh)
  /bin/sh /path/to/my_script.sh up
}

stop_my_script()                                                                  
{                                                                               
  /bin/sh /path/to/my_script.sh down
}
 
restart_my_script()
{
  /bin/sh /path/to/my_script.sh down
  /bin/sh /path/to/my_script.sh up
}  

run_rc_command "$1"

All most done.

Creating the rc script

Make sure your script is execuatable

chmod a+x /path/to/my_script.sh

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
my_script_enable=NO

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

End

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 =]

Comments

No comments yet.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working