LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Start 2 programs at boot time (https://www.linuxquestions.org/questions/linux-newbie-8/start-2-programs-at-boot-time-606363/)

Avatar 12-12-2007 03:24 PM

Start 2 programs at boot time
 
Hello, I know this has been asked a bazillion times and I did several searches to that effect but I'm still foggy (hence why I'm posting in Newbie).

I'm running a server with Ubuntu Edgy (6.10).

I need to add two things to the startup sequence so that I don't have to do it manually every time the power goes off or the server gets rebooted.

1. Run my firewall script, located in /etc/init.d
2. Start the "pon" internet connection.

It absolutely MUST be in that order, if I start the connection first then run the firewall, none of the clients can connect. In fact, I would like to ensure it is done correctly by first running poff then pon again.

By searching LQ I have discovered the following things.

- that I must determine my runlevel (which is 2)
- that I must link my script in the /etc/rc2.d directory
- I must use the "man" command to determine how to do this exactly
- I must read the README file that is included in the rc2.d directory

So the readme has me all confused.

It seems to suggest that you can only run scripts that are in the specific /etc/init.d directory

Quote:

The scripts are all symbolic links whose targets are located in /etc/init.d/ .
I was all proud of myself for figuring out how to do my startup sequence without any help, but alas. It was not to be.

I need to start the pon program which is in /usr/bin and not in /etc/init.d - so how do I do this?

Additionally, how can I make sure pon is not already running, and if it is, disable it using poff, then run the firewall script, then run pon? I tried putting a link in my /etc/rc2.d that starts with K but it involves complicated math and I don't think it would work anyway seeing as how poff is located in /usr/bin as well.

derxob 12-12-2007 03:58 PM

Quote:

It seems to suggest that you can only run scripts that are in the specific /etc/init.d directory

Quote:
The scripts are all symbolic links whose targets are located in /etc/init.d/ .
I was all proud of myself for figuring out how to do my startup sequence without any help, but alas. It was not to be.

I need to start the pon program which is in /usr/bin and not in /etc/init.d - so how do I do this?
Create a file in /etc/init.d/ called "run-pon"
Code:

touch /etc/init.d/run-pon
Open run-pon file
Code:

vi /etc/init.d/run-pon
Tell run-pon file to execute /usr/bin/pon
Code:

#!/bin/sh
/usr/bin/pon

Make run-pon file executable
Code:

chmod +x /etc/init.d/run-pon
Link the /etc/init.d/run-pon file to /etc/rc2.d/
Code:

ln -s /etc/init.d/run-pon /etc/rc2.d/S99run-pon
Hope this helps!

forrestt 12-12-2007 04:04 PM

OK, any startup scripts in your /etc/rc*.d directories that start with K are run with the "stop" parameter. You need it to start with S so it will be run with the "start" parameter. Also, the number after the S or K is the order in which the scripts are run (starting with 01 up to 99).

To make the link for runlevel 2 run:

Code:

cd /etc/rc2.d
ln -s /etc/init.d/firewall S08firewall

(I chose S08 above because that is the order that my iptables script runs. You may want to make "08" something else.)

Also, how did you determine 2 was your default runlevel? Typically things are run at 3 or 5, but I'm not super up on my Ubuntu. Anyway, you probably also want to make the links for the other runlevels 3, 4, and 5 as well as the stop scripts (ln -s /etc/init.d/firewall K92firewall) for runlevels 0, 1, and 6. That way when you switch runlevels, it will run those scripts if they need to be run.

Also, not sure about Ubuntu, but Red Hat and Fedora have a tool, chkconfig that does all of this automatically.

The following script SHOULD work to start and stop your internet connection. I give it with no guarantee (but I'll be glad to look at it if it doesn't work and you supply feedback as to why). Put it in /etc/init.d and call it something (ispconnect ?). Then link it as you did firewall above but increment the number (in this case it would be 09).

Code:

#!/bin/sh
# See how we were called.
case "$1" in
  start)
        # Start daemon.
        echo -n $"Starting internet connection: "
        /usr/bin/pon
        ;;
  stop)
        # Stop daemon.
        echo -n $"Shutting down internet connection: "
        /usr/bin/poff
        ;;
  restart)
        $0 stop
        $0 start
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart}"
        ;;
esac

exit

You will need to make the script executable:
Code:

chmod 755 /etc/init.d/ispconnect
HTH

Forrest

Avatar 12-13-2007 08:34 AM

Wow, you guys. That was really awesome! I got two clear, detailed answers that I can understand and implement. Thanks very much, derxob and forrestt. I am really satisfied with both your answers.

Quote:

Also, how did you determine 2 was your default runlevel?
I literally typed "runlevel" at the command prompt. (I learned that trick yesterday while searching. You can also type "who -r"). This is a server only, not a desktop machine, so it doesn't have a GUI. I think that's was runlevel 2 means.

Forrestt: Can you tell me more about your script. It looks like it does the same thing as derxob's two line script. It is just curiosity on my part, but I wonder why use a complex script like that, what is the reason? is it better to use one type of script over the other? When I was looking in my init.d directory I noticed that most of the scripts are written that way.

Now with derxob's script, I would modify it slightly so that I could ensure pon wasn't already running, like so:

Code:

#!/bin/sh
/usr/bin/poff
< some command to make it wait a moment, like sleep 3 - I'll have to look it up >
/usr/bin/pon

Is there a way to do this with your type of script? or better yet, check to see first if there is a PID for pon or something?

Anyway, thanks again for your help, I really appreciate it.


All times are GMT -5. The time now is 01:20 AM.