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"

3 Answers 3


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

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

ping -c 1 -W 1 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 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.

  • The C program is a non-GUI program which outputs to stout via printf. Will I be able to view that output (in a terminal) after starting the xdesktop as user pi?
    – nixda
    Jun 21, 2015 at 21:38
  • @nixda No you won't, but you can redirect the output (+ stderr) into a file and look at it later, or track it in a terminal via tail -f /home/youruser/Desktop/rpiMainProgram.log. I edited my answer accordingly.
    – Minix
    Jun 21, 2015 at 22:19

Start your program in a terminal multiplexer such as screen or tmux. Use it to create a detached terminal where you run your program, and attach to that terminal when you want to interact with it. For example, with screen, run this during startup:

screen -S rpiMainProgram -d -m /home/nixda/Desktop/rpiMainProgram

and use this to attach to the screen instance where the program runs:

screen -S rpiMainProgram -r -d

To detach from the screen instance, press Ctrl+A then d.

On Debian, to run a program after a network interface is enabled, add a script in /etc/network/if-up.d. You'll need to check which interface is being brought up (in praticular you don't want to start the program when the loopback interface is started). Create /etc/network/if-up.d/local_rpiMainProgram containing something like

case "$IFACE" in
  ppp0) screen -S rpiMainProgram -d -m /home/nixda/Desktop/rpiMainProgram


if ping -q -W1 -c1; then
  screen -S rpiMainProgram -d -m /home/nixda/Desktop/rpiMainProgram

Using rungetty to run it on a unused virtual terminal would work quite nicely. control alt fn to view the output.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .