Table of Contents

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

#!/bin/bash
export DISPLAY=:0.0
MYNETSTART=/home/user/.network-autostart
if [ -x $MYNETSTART ]; then
      su -c "$MYNETSTART" user
fi
#!/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

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.