0

I've been experimenting with Pis for a little while now and am close to completing my first project, I have all the bits working but I'm struggling to put them all together into an automated process.

Basically I have a Pi setup to run a fbi slideshow from a specific folder and I want it to constantly be looking for a pre established Wifi network and when it finds this network it needs to run an update script. I've got these two bits working.

From here, I want the Pi to be turned on and load straight into fbi whilst running the checking script in the background, if the checking script finds the Wifi network then it will run as normal (preferably without ending the slideshow) and when it's done fbi should have an updated selection of images to run (if a restart of fbi is necessary then so be it).

I'm coming up short on achieving this. I can run one script or the other, and if I automate the checking script (cron hasn't worked though I dare say I'm doing something wrong) with rc.local it just gets stuck in a checking loop before login, which kinda makes sense given the script.

Here's the monitoring script:

#!/bin/bash

while true ; do
  if ifconfig wlan0 | grep -q "inet addr:" ; then
    echo "Wifi connected!"
    echo "Initiating Grive sync!"
    (cd /home/pi/images/; ./grive -s Pi_Test -V)
    sleep 60
  else
    echo "Wifi disconnected!  Attempting to reconnect now."
    ifup --force wlan0
    sleep 10
  fi
done

and in case it's relevant, here's the command the run the fbi slideshow:

fbi -noverbose -a -t 10 -u /home/pi/images/Pi_Test/*.jpg
Piotr Król
  • 3,350
  • 2
  • 23
  • 25

1 Answers1

1

I do not have a Pi but I have used cron on my VPS which is running a CentOS, but the overall procedure should be similar.

To have some script to be executed by cron, you need to:

  1. Edit /etc/cron.allow
    You need to add in your user id to this file so that you can use crontab.
  2. crontab -e
    Use this command to add rules you want to fire. From your description, it seems to me that you already know the syntax to add rules into cron table.
    After that, you can use crontab -l to verify your change.

As for stuck at before login, that is very likely due to the while loop. You might want to get rid of the while and sleep because cron is helping you out by invoking your script periodically.
Therefore the following should not suffer from the stuck issue.

if ifconfig wlan0 | grep -q "inet addr:" ; then
    echo "Wifi connected!"
    echo "Initiating Grive sync!"
    (cd /home/pi/images/; ./grive -s Pi_Test -V)
else
    echo "Wifi disconnected!  Attempting to reconnect now."
    ifup --force wlan0
fi

The trick instead, is to have some line similar to this in your cron table

*/1 * * * * /home/David_Legassick/test.sh

The */1 asks cron to call your script test.sh every minute.

Michael
  • 26
  • 1
  • 2
  • Interesting, I've got to ask though, in your **cron table** bit, just so I'm totally clear, what do the second four *'s represent? If it's root file structure wouldn't ~/home work? As for the loop, I suspected it was the cause, I didn't really realise exactly where in the boot process it would be run. – David Legassick Aug 07 '14 at 14:54
  • What I'm still unsure of is if this script is running as a **cron** script, would it still output to the screen? Obviously I've put echoes in for testing purposes, but would it run completely in the background, or is there a way of forcing it to? Oh, by the way, the Raspian OS is pretty much just Debian optimised for Pi. Thanks for the help! – David Legassick Aug 07 '14 at 14:56
  • @DavidLegassick As for the meaning of *, please use `man 5 crontab` to see the details. Quick answer would be from left to right, there are 5 *s, representing `minute, hour, day, month, week` The `*` here means anything. So `5 0 * * *` /your_script.sh means **cron** will invoke `your_script.sh` every 5 minutes. – Michael Aug 07 '14 at 22:00
  • @DavidLegassick There is no output to stdout. No error message either, which is the very reason that makes it hard to debug. Also, all the environment variables are set be the same as `nologin` shell. – Michael Aug 07 '14 at 22:02
  • @DavidLegassick One very useful trick to debug would be `tail -f /var/log/cron`. This is the log for cron activity. – Michael Aug 07 '14 at 22:03
  • Cool, that all seems to make sense! I'll have a play with it and see how I go, but I probably won't get around to it for a few days. Cheers for you help bud. – David Legassick Aug 08 '14 at 07:05
  • @DavidLegassick If it helps, could you please accept it as the answer? – Michael Aug 08 '14 at 14:40
  • Sure thing, once I've had a chance to test it out and see where it gets me I'll happily accept it. Cheers again bud. – David Legassick Aug 08 '14 at 23:07
  • This worked nicely, the only thing that needed tweeking was the calls the ifconfig and ifup, these required their full paths to run, so /sbin/ifconfig and /sbin/ifup. Apart from that it worked wonderfully, now if I could only get it to run under a program! – David Legassick Aug 12 '14 at 16:31