VectorLinux

Please login or register.

Login with username, password and session length
Advanced search  

News:

Visit our home page for VL info. For support and documentation, visit the Vector Linux Knowledge Center or search the Knowledge Center and this Forum using the search box above.

Author Topic: How can I make an init script that requires network?  (Read 9971 times)

pierce.jason

  • Packager
  • Vectorite
  • ****
  • Posts: 250
How can I make an init script that requires network?
« on: October 14, 2011, 06:00:54 pm »

I created a startup script today for a network daemon that acts in client mode. The daemon must connect to an external server via the network. If the network isn't present when the daemon starts, then it is gameover for the daemon.

I created the symlink in rc4.d to be numerically positioned right after sshd. I figured by the point that sshd starts up then I'd probably have network connection. IIRC sshd was S51 so I made my new script at S55.

Looking in the logfiles it appears that I get DHCP several lines after my S51 and S55 init scripts are executed.

I couldn't trace down where dhcpcd is started up.

What should I do inorder to ensure my S55 rc4.d init script starts after dhcp is started?

Thanks  for any pointers,
Jason Pierce
Logged
pierce.jason
Email: $(echo -e "moc\x2eliamg\x40nosaj.ecreip" | rev)

bigpaws

  • Vectorian
  • ****
  • Posts: 1869
Re: How can I make an init script that requires network?
« Reply #1 on: October 16, 2011, 02:24:31 pm »

First  you could use wait or sleep for a period of time until the
network would normally be up and running.

Second you could make a script using expect.

Third make a script using ping then exec the script.

Fourth would be use a test of ifconfig and then run or
drop the script.

HTH

Bigpaws
Logged

M0E-lnx

  • Administrator
  • Vectorian
  • *****
  • Posts: 3250
Re: How can I make an init script that requires network?
« Reply #2 on: October 16, 2011, 07:27:27 pm »

You could test to see if there is an active NIC with an IP address
Code: [Select]
ifconfig |grep addr:|awk {'print $2'}|grep -v 127.0.0.1 | cut -f 2 -d : You can run this in a loop for 10 seconds or so and break after you get a hit of give up after the allotted time

pierce.jason

  • Packager
  • Vectorite
  • ****
  • Posts: 250
Re: How can I make an init script that requires network?
« Reply #3 on: October 16, 2011, 08:17:40 pm »

Great ideas guys, but detecting network is the easy part ;)

The concept I'm having a hard time wrapping my head around is how to efficiently wait/loop while waiting on dhcp. The scripts under rc#.d/ are blocking, so system startup would completely halt while my daemon's init script loops and waits for internet.

It seems that bash has the ability to define a function, and then run it in the background while the defining script exits. In this case I could define a do_it() type function for the start) case to call, and I could background that function such as "do_it&" to allow the init script to exit.

I worked up a 'lil tester script for this backgrounded functions idea, and it seems to work spot on. Only thing is I can not get my stop) case to run a "killall do_it". This last thing would only be relevant during the short time during system startup where I am waiting for DHCP to respond.

http://pastebin.com/0wCEEfKt
Think something along these lines would be a good option to do the trick with backgrounding the function called in start) case?
Logged
pierce.jason
Email: $(echo -e "moc\x2eliamg\x40nosaj.ecreip" | rev)

bigpaws

  • Vectorian
  • ****
  • Posts: 1869
Re: How can I make an init script that requires network?
« Reply #4 on: October 16, 2011, 09:59:49 pm »

You could also move your script to /etc/rc.d/rc.local. That is where my network
scripts are started which is after dhcp.

Bigpaws
Logged

M0E-lnx

  • Administrator
  • Vectorian
  • *****
  • Posts: 3250
Re: How can I make an init script that requires network?
« Reply #5 on: October 17, 2011, 04:22:42 am »

Yrah call it from rc.local and add a "&" to its exec line.  That would throw it to the background and let the boot process continue.