4

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?
slm
  • 369,824
zengr
  • 143
  • You can use select() to monitor error output on particular file descriptor. If some error happens, then you restart the network. See http://www.beej.us/guide/bgnet/output/html/multipage/selectman.html . You need to spend some time understanding select(). – ernesto Apr 10 '14 at 05:12
  • select() is a c construct. I am looking for a bash solution. – zengr Apr 10 '14 at 05:31
  • I think u can use inotifywait. http://blog.lagentz.com/general/automate-your-shell-scripts-using-inotify-and-inotifywait/ – ernesto Apr 10 '14 at 06:19

3 Answers3

2

UPDATE

This is much simpler:

( 
{ ${PROC1_CMD} >&3 } 2>&1 | 
    while : ; do 
        grep -q "YOUR ERROR MESSAGE" && ${YOUR_ACTION} 
    done
) 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.

mikeserv
  • 58,310
  • 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: http://unix.stackexchange.com/questions/122845/using-a-b-for-variable-assignment-in-scripts/122892#122892 – mikeserv Apr 11 '14 at 19:13
1

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 : https://github.com/rvoicilas/inotify-tools/wiki

Examples can be found at: http://blog.lagentz.com/general/automate-your-shell-scripts-using-inotify-and-inotifywait/

ernesto
  • 191
1

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.

slm
  • 369,824