Debian – Start non-GUI application after network connection is up and running

debianlinuxraspbian

I'm using Raspbian, which is based on Debian. My Raspberry automatically boots into the desktop. I have a small program written in C and compiled with g++ as executable. The program needs a running network connection and outputs some data to the terminal. Nothing fancy.

To use it, I have to open a LXterminal window and run the program manually with sudo rights

sudo ./Desktop/rpiMainProgram

My question is: How do I automatically start a terminal based program after a network connection is established?


I use wvdial to connect a 3G dongle automatically via /etc/network/interface. The connection works. I can ping and surf

auto ppp0
iface ppp0 inet wvdial

I followed this tutorial to set up wvdial if it matters

I saw a method which uses the post-up method in /etc/network/interfaces to call a bash (?) script. But I couldn't get this method to run a non-GUI application

My last attempt before I gave up was

auto ppp0
iface ppp0 inet wvdial
post-up LXterminal "sudo ./Desktop/rpiMainProgram"

Best Answer

You could put something like this in your /etc/rc.local file (untested):

{ while ! ping -c 1 -W 1 8.8.8.8; do  sleep 1; done; /home/youruser/Desktop/rpiMainProgram > /home/youruser/Desktop/rpiMainProgram.log 2>&1; }&

ping -c 1 -W 1 8.8.8.8: sends out one ping packet and waits 1 second for its return

The while loop continues as long as the ping command exits with a status code >0, meaning, as long as it fails.

When you have a network connection and can ping 8.8.8.8 successfully the loop will stop and execute your program (check if calling it like this makes a difference, for example with the pwd).

With the {} we make it into a singular command kind of thing, so that we can push the whole thing into the background using &. Otherwise it would block the rest of /etc/rc.local until you have a network.

There might be more elegant solutions, but I have something similar to that on my Pi.

Related Question