User Tools

Site Tools


howto:networkupscript

Running a Script or Program on Network Startup

This is useful if you don't want lots of programs running until you get a network connection. I use it for gmail notifier, which gives error messages if there's no network. There's only easy mode at the moment, as that's what I'm using.

Easy Mode

  • Open a terminal (Ctrl+Alt+t) and type:
    sudo kwrite /etc/network/if-up.d/mynetstart
  • This file should be blank. Put this into the file:
#!/bin/bash
export DISPLAY=:0.0
MYNETSTART=/home/user/.network-autostart
if [ -x $MYNETSTART ]; then
      su -c "$MYNETSTART" user
fi
  • Save (Ctrl+s) and Quit (Ctrl+q)
  • Make the file executable:
    sudo chmod 0755 /etc/network/if-up.d/mynetstart
  • Make a symlink for the ppp interface too:
    sudo ln -s /etc/network/if-up.d/mynetstart /etc/ppp/ip-up.d/
  • Now create the .network-autostart file:
    kwrite ~/.network-autostart
  • This code will run Pidgin, but you can add any program you want:
#!/bin/bash
logger -p user.info "Network Autostart Script starting"
if [ "`/sbin/ifconfig | grep -v lo | grep Link -A1 | grep inet `" ]; then
   logger -p user.info "An interface is up."
   if [ -z `pgrep pidgin` ]; then
      logger -p user.info "Loading Pidgin"
      pidgin &
   fi
   #if [ -z `pgrep ANOTHER_PROGRAM` ]; then
   #   logger -p user.info "Loading ANOTHER_PROGRAM"
   #   ANOTHER_PROGRAM &
   #fi
fi
  • The logger command writes to a logging system (known as syslog) if it's running. To start it - for debugging purposes, enter these commands:
    /etc/init.d/sysklogd start
    tail -n0 -f /var/log/*/*
  • The line showing ifconfig has a double quote (”) and then a backtick (the key between F1 and ESC on a British English Keyboard) before the /. There are pipe characters (Ftn+Shift+Z) between each command and then there's a backtick followed by a double quote after the word inet.
    • The pipe symbol joins multiple commands together.
    • The ifconfig command lists all the interfaces and some settings about each interface.
    • The grep command looks for words in a string.
    • The “Local” (or lo) adaptor will always show up as having the address 127.0.0.1. The grep command -v stops that line from appearing.
    • We're looking only for the lines which specify an interface. The interface lines have the word Link in them, hence the next grep command. The -A1 tells grep to show the line we're looking for and the line after.
    • Lastly, the line after the word “Link” shows the string inet followed by the IP address. If we see the word inet, then it means we've got an address.
  • The pgrep command returns the process ID for a running program. In combination with the if command, this should to stop the program starting repeatedly if your connection comes and goes.
  • For more programs, copy and insert more copies of the part showing ANOTHER_PROGRAM, first removing the # symbol from each of *THOSE* lines
  • Save and exit.
  • Make this file executable too:
    chmod 0755 ~/.network-autostart

And you're done!

Delaying Network mounts

As explained in the forums this method can also be used to mount network shares only when the network is connected by adding the mounting commands to the .network-autostart file. However, this creates a mounted folder to which the default user does not have access, so it also requires a line to change permissions.

The code will depend on each user's network configuration, but Niel1952 reports that this code works for him:

#! /bin/bash
 logger -p user.info "Network Autostart Script starting"
 if [ "`/sbin/ifconfig | grep -v lo | grep Link -A1 | grep inet `" ]; then
   logger -p user.info "An interface is up."
   if [ -z `pgrep firefox` ]; then
     logger -p user.info "Loading Firefox"
     /opt/firefox/firefox &
   fi
   if [ -z `pgrep Videos` ]; then
   logger -p user.info "Mounting Videos"
   sudo mount -t cifs -o username=Public,password=******,uid=user,gid=users '//192.168.1.5/Movies' /home/user/My\ Documents/My\ Videos/;
   sudo chmod 0755 /home/user/My\ Documents/My\ Videos
   fi
   #if [ -z `pgrep ANOTHER_PROGRAM` ]; then
   #  logger -p user.info "Loading ANOTHER_PROGRAM"
   #  ANOTHER_PROGRAM &
   #fi 
 
fi

Major credit goes to Jefficus and jimbox51, see this thread. To get gmail notifier, see this thread.

howto/networkupscript.txt · Last modified: 2008/08/21 14:01 by garabik