Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I have this constantly running process which POSTs some data collected locally to an endpoint. Now there is some issue in the raspberryPi wifi module and it drops connection frequently. After restarting networking, it starts working again.

So this is what I have right now based on the face that everytime internet disconnects, proc1 starts throwing stderr:

while true; do
   ./proc1 2>&1 >/dev/null | sudo /etc/init.d/networking restart
done &

But this code restarts networking multiple times, not sure why.

What I actually want is something like this:

while true: 
   if proc1 has stderr: 
      sudo /etc/init.d/networking restart; 
      sleep 10;

So my question are:

  1. How can I convert that pseudocode to bash?
  2. This is a hack to get around the actual problem of networking drops, how can I debug what’s wrong?
share|improve this question
You can use select() to monitor error output on particular file descriptor. If some error happens, then you restart the network. See . You need to spend some time understanding select(). – ernesto Apr 10 '14 at 5:12
select() is a c construct. I am looking for a bash solution. – zengr Apr 10 '14 at 5:31
I think u can use inotifywait.… – ernesto Apr 10 '14 at 6:19

3 Answers 3

up vote 2 down vote accepted


This is much simpler:

{ ${PROC1_CMD} >&3 } 2>&1 | 
    while : ; do 
        grep -q "YOUR ERROR MESSAGE" && ${YOUR_ACTION} 
) 3>&2

Basically we're just swapping stdin and stderr for $PROC1_CMD so we can grep its output over the |pipe. That way whatever your process wants to say normally it can but as soon as it writes to stderr the message you don't want it to you can take $YOUR_ACTION.

share|improve this answer
Bash question: What does ${} in ${PROC1_CMD} and ${YOUR_ACTION} mean? – zengr Apr 11 '14 at 18:48
@zengr - It's nothing to do with bash, really. And it doesn't mean much in this case, anyway - it's just habit. But it signifies POSIX-specified parameter expansion. As in ${PROC1_CMD=this is only assigned if $PROC1_CMD is unset} - see my answer here and the ones it links to:… – mikeserv Apr 11 '14 at 19:13

Busy polling is bad, especially for your situation where you have to immediately restart something and hence can't use sleep() inside while loop.

Best way is to rely on constructs provided by OS. Since it's a shell script, use inotify-tools :

Examples can be found at:

share|improve this answer

Monitoring WiFi

These are not necessarily Bash solutions but I've used many of these tools to monitor a process and then restart it. I think you're attempting to monitor the wrong thing in your approach. Rather than monitor the output from your script, you should in fact be monitoring your network service.

You can read more about their setup and use in this U&L Q&A titled: How to set proper monitoring of my services in a automated way? So that if one crash it auto on the fly restarts?.

I have a preference for God, which is extremely easy to setup and use. It does require a installation of Ruby however so one of the others may suit your needs better.

Other approaches

Given this is a Rasp Pi issue there's actually a thread on their site pertaining to this issue. There are many suggestions you could use from that thread as well. The thread is titled: Wifi Reconnect on drop.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.