Report comment

Hi everybody .. First, please excuse me if something is not clear, but I'm not a native English person. I want to share my experience in case somebody else is having the same troubles as me.

I have followed this tutorial, and everything worked perfect until I set this into the crontab (I didn't know at first the crontab was the problem, since I did it instantly following the tutorial, but found that after extensive debugging). Over all, the main script and setup works. Great job!

The problem:
When the RPi 3 B+ (Stretch) boots, everything is fine - it connects to WiFi or create a hotspot (if no known WiFi in range) and I'm able to connect via SSH and configure whatever I need (never checked if connected to Ethernet will start to spread Internet over WiFi hotspot, but I don't need that right now). After a while I've started getting problems .... interruptions of ssh sessions and etc..
However, checking the ifconfig, after a while the IP changes to the one from the dnsmasq (192.168.50.5), which was so strange, since the RPi was connected to the WiFi and has Internet. I have checked running processes, and dnsmasq and hostapd were running for some reason?! However, if using the remote SSH (not attached to monitor), after a while I got disconnected, and I have no way to see if RPi was running or just unstable because of some other things... There were no hotspot network running, so I know it was running on WiFi, but was unresponsive.

So I debugged this with monitor attached.. After it changes the IP to the dnsmasq one, I have run the autohotspotN script manually and everytime it shows:

SSid's in range: ..... correct SSID list (removed to be more clear)
Device Available Check try 0
Device Available, checking SSid Results
Valid SSID Detected, assesing Wifi status
Shutting Down Hotspot
iptables: Bad rule (does a matching rule exist in that chain?).
iptables: Bad rule (does a matching rule exist in that chain?).
Hotspot Deactivated, Bringing Wifi Up
Checking WiFi connection ok

So, after manual run of the script, it switches back to 192.168.0.X (the proper WiFi assigned IP) and shuts down the dnsmasq and hostapd processes. After a while, switches them ON again and the IP changes once again. So I have understood this is periodic process, so it must to be the crontab... since after boot and manual run it's just perfect. I have removed the crontab entry and it worked without glitches for a while... so then I was 100% sure it's the crontab setup.

Then ... I have decided to spent some time on the crontab debug... I have logged the output of the script... and catch this:
SSid's in range: /usr/bin/autohotspotN: line 86: iw: command not found
Device Available Check try 0
Device Available, checking SSid Results
No SSid found, assessing WiFi status
No SSid found, assessing WiFi status
Creating Hotspot

Then, next log entry was:
SSid's in range: /usr/bin/autohotspotN: line 86: iw: command not found
Device Available Check try 0
Device Available, checking SSid Results
No SSid found, assessing WiFi status
No SSid found, assessing WiFi status
Hostspot already active

So, I have understood there's a problem with iw command, but when I run the script manually it just works perfect (same on boot)... So it must be something else.... It was the $PATH for the cron ... The cron wasn't seeing /sbin/ where iw is originally located. Once I have edited the crontab and added on top of any configured scripts:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

everything went perfect and this now works in the way I'm expecting.. It's no longer activating dnsmasq and hostapd unless out of WiFi range. I guess the default RPi PATH for crontab is not including all possible bin paths, or at least mine wasn't.

Hope this helps somebody else experiencing my problem.