LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-13-2007, 06:46 PM   #1
Sanborn
Member
 
Registered: Jun 2007
Distribution: Suse 10.1
Posts: 40

Rep: Reputation: 15
Can someone explain how this script works?


I am by no means a programmer. Using SUSE I found a script that creates the equivalent to having a rc.local file, found here: http://linux.derkeiler.com/Mailing-L...5-07/1773.html. I was wondering if anyone could explain how this script works too me.

This is what the mail-list says to do:

Steps to simulate rc.local in Suse:

1) Create a "/etc/rc.d/rclocal" script, with this content:

----------------------------
#! /bin/sh

## This script simulates redhat's rc.local (Add commands at the end)

### BEGIN INIT INFO
# Provides: rclocal
# Required-Start: $local_fs $remote_fs $network
# X-UnitedLinux-Should-Start: $ALL
# Required-Stop:
# X-UnitedLinux-Should-Stop:
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Short-Description: Simulates rc.local
# Description: Simulates redhat's rc.local: contains
# commands to execute after system has booted (all services are already
# available)
### END INIT INFO

## Execute ony when service is started
case "$1" in
start)
## commands will be executed
;;
*)
exit 0
;;
esac

# vvvvv Add your commands bellow this line vvvvv
----------------------------

2) Add executable permision: chmod +x rclocal

3) Create symlink to make it easy to find: ln -s rclocal rc.local

4) Activate the service by using yast2:
yast2 > System > Runlevel editor > rclocal > Enable

You can add/remove commands to /etc/rc.d/rc.local anytime you wish.

So what I did and what my problem is:

I opened up Kate editor and followed step 1 exactly character for character. Where it says "add your commands bellow this line vvvv" I typed ' route add 192.168.0.1 gw 30.38.72.50 ' (without ''). This part confused me, because doesn't the command need to be where it says ## commands will be executed?

Step 2 and 3 worked no problem

on step 4, when I tried to activate the script it gave me the error message:

/etc/init.d/relocal start returned 7 (program is not running)

Did I implement that line of code incorrectly?
 
Old 06-13-2007, 07:42 PM   #2
dxqcanada
Member
 
Registered: Sep 2006
Location: Canada
Distribution: Gentoo
Posts: 702

Rep: Reputation: 43
case "$1" in

this means ... read the first parameter passed to this shell script and look for a match below.

start)

If $1 equals "start" then run all commands after this section up to the ";;" that terminates this section ... then goto esac.
In this script there are no commands to run.

*)

If $1 equals anything then run all commands after this section up to the ";;"
In this script it says to exit.

esac

ends the case

Commands after this point will only be started if the $1 parameter equals "start".
 
Old 06-13-2007, 07:52 PM   #3
Sanborn
Member
 
Registered: Jun 2007
Distribution: Suse 10.1
Posts: 40

Original Poster
Rep: Reputation: 15
Thanks, this is going to sound stupid. But does that mean I should put the route command inside the first part and at the end ?

so like:

## Execute ony when service is started
case "$1" in
start)
route 192.168.0.1 gw 30.38.72.50
;;
*)
exit 0
;;
esac

route 192.168.0.1 gw 30.38.72.50

or just in one? Also, is that the right way to use route? If you do whereis route it says that route is /sbin/route. Does that mean I need to use it as /sbin/route add ..blah blah when inside the script ?

Thanks for the help, I'm a bit overwhelmed at even the more simple stuff.
 
Old 06-13-2007, 07:57 PM   #4
dxqcanada
Member
 
Registered: Sep 2006
Location: Canada
Distribution: Gentoo
Posts: 702

Rep: Reputation: 43
If the rclocal script was passed a parameter of "start" then it would pass through the case statement ... exit the case statement without doing anything ... and then attempt to execute your route command.

So, I think it should be OK in the "vvvvv" section.

Yes, I would specify the full path of the command.

Make sure the command runs properly on the command line ... cause I think you do not have the command correctly written.
 
Old 06-13-2007, 08:50 PM   #5
Sanborn
Member
 
Registered: Jun 2007
Distribution: Suse 10.1
Posts: 40

Original Poster
Rep: Reputation: 15
Oh yes, I forgot to add the "add" portion right after route. I have it done correctly on the code.

So thats odd, I have the script typed exactly as described, any other reason why when I try to enable the script Yast gives me that error?
 
Old 06-13-2007, 08:58 PM   #6
dxqcanada
Member
 
Registered: Sep 2006
Location: Canada
Distribution: Gentoo
Posts: 702

Rep: Reputation: 43
First ... if you do not add the route command to the rclocal script ... does it generate an error ?
 
Old 06-13-2007, 10:11 PM   #7
Sanborn
Member
 
Registered: Jun 2007
Distribution: Suse 10.1
Posts: 40

Original Poster
Rep: Reputation: 15
It's actually a work computer I've been doing some side stuff on. I'll check it tomorrow morning. If the script is supposed to load while the system is booting, where would I see if there was an error? Or do you mean actually running the script by itself after everything is loaded?

I believe that I ran it and nothing seemed to happen, but it was a quick try before I left for the day.

dxq, do you know how I can add some kind of echo command to write to a text file? Just to see if the script goes thru on boot?
 
Old 06-14-2007, 03:15 AM   #8
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
nothing to do with networking. please choose your forums more carefully in future. moved to Programming.
 
Old 06-14-2007, 06:24 AM   #9
Sanborn
Member
 
Registered: Jun 2007
Distribution: Suse 10.1
Posts: 40

Original Poster
Rep: Reputation: 15
Opps, I should sleep more.
 
Old 06-14-2007, 08:54 AM   #10
Sanborn
Member
 
Registered: Jun 2007
Distribution: Suse 10.1
Posts: 40

Original Poster
Rep: Reputation: 15
If I enable the script in Yast it will run the route command as it should (becuase I placed the route command in 3 spots). In the yast-runlevel service I told it to start/stop in level 2/3/5/S (what is S?) and yet nothing happens on boot.

Any ideas?

This is the script exactly as I have it:

"#! /bin/sh

## This script simulates redhat's rc.local (Add commands at the end)

### BEGIN INIT INFO
# Provides: rclocal
# Required-Start: $local_fs $remote_fs $network
# X-UnitedLinux-Should-Start: $ALL
# Required-Stop:
# X-UnitedLinux-Should-Stop:
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Short-Description: Simulates rc.local
# Description: Simulates redhat's rc.local: contains
# commands to execute after system has booted (all services are already
# available)
### END INIT INFO

## Execute ony when service is started
/sbin/route add 192.168.0.1 gw 30.38.72.50

case "$1" in
start)
## commands will be executed
/sbin/route add 192.168.0.1 gw 30.38.72.50
;;
*)
exit 0
;;
esac

# vvvvv Add your commands bellow this line vvvvv ----------------------------
/sbin/route add 192.168.0.1 gw 30.38.72.50


-I added it to multiple lines to see if anything would happen. Again, if I enable it from yast it will run thru the script and route add will work correctly. On boot, nothing.
 
Old 06-14-2007, 09:08 AM   #11
Sanborn
Member
 
Registered: Jun 2007
Distribution: Suse 10.1
Posts: 40

Original Poster
Rep: Reputation: 15
Also, can someone explain to me how anything after esac matters? Is that where the program goes to if $1 = Start and it hits ;; ?
 
Old 06-14-2007, 01:04 PM   #12
dxqcanada
Member
 
Registered: Sep 2006
Location: Canada
Distribution: Gentoo
Posts: 702

Rep: Reputation: 43
The script runs through the case routine ... then the sript runs through any other commands after esac (which ends the case routine).

The only time the script will exit is either there are no more commands to process or an exit command it encountered.

So in your case ... if the case routine matches "start" then it will process all commands up to the ";;" then the case routine jumps to esac ... then the script continues on to process any other commands that follow it.
If the case routine matches "*" (anything else) then it encounters the exit command that tells the script to end.

There should not be a " in the first line ... I am going to assume it is a typo.


You could add some debug type statements into the script to see what it is doing.
Example ... add
Code:
echo "script start" > /debug
in the begining of the script.

After boot see if the file /de bug was created.
If not then the script did not even run.
 
Old 06-14-2007, 01:45 PM   #13
Sanborn
Member
 
Registered: Jun 2007
Distribution: Suse 10.1
Posts: 40

Original Poster
Rep: Reputation: 15
Which line do you think is a typo??

the "$1" portion? That code is exactly how I have it (unless I am not seeing something from what i typed previously)

edit: Yes the "#!sbin part is a typo.

I assume the "$1" is correct tho?

Another question I have is, with SUSE, where should this script be running?

Just to cover bases I told it to start/stop on 2,3,5,S (what does S even do?) and I edited the rc5.d and rcS.d folder so that it was S99 position.

I feel I am close...but just missing one little detail.

Last edited by Sanborn; 06-14-2007 at 01:47 PM.
 
Old 06-14-2007, 02:07 PM   #14
Sanborn
Member
 
Registered: Jun 2007
Distribution: Suse 10.1
Posts: 40

Original Poster
Rep: Reputation: 15
Alright little update:

I put an echo command to write to a text file and ran the script from command line. It worked as intended, so I know that the code is ok. Now the real problem: Why is it not being ran on boot up? It is enabled on multiple levels and set to S99. I must be making some sort of novice mistake.
 
Old 06-14-2007, 04:38 PM   #15
Sanborn
Member
 
Registered: Jun 2007
Distribution: Suse 10.1
Posts: 40

Original Poster
Rep: Reputation: 15
Quite interesting, back in 05 someone was trying to do the exact same thing I am, so I must be close.

http://www.linuxforums.org/forum/sus...m-startup.html

RobeCote's last reply:

okay I got it to work using a work around with a rc.local file
i had to create a file that calls the scripts (rc.local) and i put it in /etc/init.d/
then you have to create a symlink in rcX.d that calls the rc.local script,
since my problem was it was executing before network drivers and such were ready, i had to name the link S99rclocal (S99 being the order of the scripts run on startup. i put this file in rc3.d so that its the last thing executed on a runlevel3 bootup.

Funny enough, I think the last thing I was about to try before I left was change the order to S99 in rc3. Maybe I already did it, I'll find out tomorrow!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Please explain the way the program works. Gins Programming 7 03-22-2006 06:17 PM
Please explain how dd works and what it does ohovus Linux - Newbie 4 12-13-2005 08:48 AM
Please explain to me how VDR works with DVB? jimdaworm Linux - Software 2 09-04-2004 04:06 PM
Shell script works on RH 7.2 but not on RH ES brian Red Hat 1 08-31-2004 11:55 AM
explain this little script plz nukeu666 Linux - Newbie 4 08-18-2004 12:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration