Problem with while loop in a init.d service

Hi,
I'm still trying to resolve a problem that is taking me a lot of time. I have struggled some time, and created some threads (for reference):
Triggering a curl command from tcpdump event
How to capture WiFi packets with a specified MAC address
I was about to get it working but finally I found some problems and it doesn't work.
Finally I created a init.d script called dashbutton located in /etc/init.d/dashbutton. The script is as follow:

#!/bin/sh /etc/rc.common
#coded by borhacker

STOP=09
START=21
continue=true
start() {

while [ $continue = true ]
do
timeout 10 airodump-ng wlan1 --essid 68:37:E9:33:B5:90 > /root/mytestfile
if [ "$(cat /root/mytestfile | grep -c "68:37:E9:33:B5:90")" -gt 0 ]
then
echo "match"
curl https://maker.ifttt.com/trigger/button/json/with/key/mykey-sdpahdf
else
echo "no-match"
fi
done
}

stop() {
continue=false
}

I added the file dashbutton to its current location and made the command:
/etc/init.d/dashbutton enable
Then I rebooted my router and I see it started to work.

Before to continue, let me tell you what I'm trying to do.
I have a device called "Amazon dash button" and it is small plastic device with a single button, designed by Amazon to order things with only a button press. This kind of device is discontinued. But it can be hacked in several ways to achieve what you want to achieve.
So I'm trying to run airodump-ng with its MAC address (the one that finishes by B5:90) as a filter, and it triggers a curl command that, as I designed, activates a poweroff command in my PC.
So I need the button press to trigger a curl command with a specific address. This is all what the button has to do.

Now, I'm having some problems with the init.d script. First, the router doesn't react to reboot command via SSH. also it doesn't start uhttpd at boot. If I want to run Luci, I need to start it manually by:
/etc/init.d/uhttpd
When I started to use this script, I thought those two problems didn't matter, because the important thing was the button working and triggering the curl command. So I bypassed that problem. Initially it worked. Even after some button presses.
If you have read the previous threads I did, @pavelgl suggested me a solution consisting of doing a DHCP hotplug event and it worked too, but there were a moment where the button stopped working with that way of working. I think it takes the WiFi network that it was trying to connect to, as invalid. But with this new script the button presses are always captured, even after several tries.
So it was working ok, and I gave it to my friend, assuming it would work all the time, but some days later he said it wasn't working. I told him to manually reboot the router (I mean pressing the ON/OFF button). Then it worked but some time after it stopped working again.
There's a possibility that the router is too far away from the dash button. But I think it's maybe because of the init.d script design.
Right now it's working, but I can't say when it will stop working again.
Can someone tell me what error I'm doing in the init.d script, if any? should I set different STOP / START values? Or is the script well designed?
Thanks in advance
Cheers
Borhacker

There needs to be a separate script doing the service. The start section just tells the init system to launch the real program (and fork it to the background) then exits.

Thanks @mk24
So, if I understood well, should I put this as the init.d service?:

#!/bin/sh /etc/rc.common
#coded by borhacker

STOP=09
START=21

start() {
/root/dashbuttonscript &
}

stop() {
#(I don't know exactly how to stop the script, but maybe...?)
/root/dashbuttonstop &
}

... and then create /root/dashbuttonscript with this content?:

#!/bin/sh

continue=true
while [ $continue = true ]
do
timeout 10 airodump-ng wlan1 --essid 68:37:E9:33:B5:90 > /root/mytestfile
if [ "$(cat /root/mytestfile | grep -c "68:37:E9:33:B5:90")" -gt 0 ]
then
echo "match"
curl https://maker.ifttt.com/trigger/button/json/with/key/mykey-sdpahdf
else
echo "no-match"
fi
done

And, as a separate question, is it correct to put into /root/dashbuttonstop this content?:

#!/bin/sh

continue=false

I don't know if the scope of the variable $continue would be ok or do I have to do something else?
Cheers
Borhacker

init.d has a bunch of stuff behind the scenes to run and monitor the service. Basically a process is registered and init.d takes care of starting it in the background, attempting to restart it if it crashes, and killing it if you run the stop command.

Look at the existing /etc/init.d/cron for a relatively simple example. Note that nowhere is crond directly started or stopped.

Ok @mk24
I will check it out
Borhacker

Hi,
I have tried to understand the contents of /etc/init.d/cron but it is so complex that I don't understand.
furthermore, I have tried with the suggestions that I told you:

#!/bin/sh /etc/rc.common
#coded by borhacker

STOP=09
START=21

start() {
/root/dashbuttonscript &
}

stop() {
/root/dashbuttonstop &
}

and the dashbuttonscript like this:

#!/bin/sh

continue=true
export continue
while [ $continue = true ]
do
timeout 10 airodump-ng wlan1 --essid 68:37:E9:33:B5:90 > /root/mytestfile
if [ "$(cat /root/mytestfile | grep -c "68:37:E9:33:B5:90")" -gt 0 ]
then
echo "match"
curl https://maker.ifttt.com/trigger/button/json/with/key/mykey-sdpahdf
else
echo "no-match"
fi
done

and the dashbuttonstop like this:

#!/bin/sh

continue=false

Now with this setup I have seen that reboot command is working again, and Luci is working without the need to manually start /etc/init.d/uhttpd.
But if I try to stop the dashbutton service:
/etc/init.d/dashbutton stop
I see that it is still working, because I press the button and the action is triggered (the curl command).

I don't need a very advanced setup. I only need to know if the operation of the stop command is necessary to avoid problems in the future. If I can't stop the dashbutton service, I think it's not a problem for me.
Do I need to do something else in this setup? or it is already ok? will it give me some problems like before?

I think the stop command in my dashbutton service is not working because I didn't succeed making the $continue variable as global, so when I execute the stop command the scope of the continue variable is not global. So in case it is necessary to setup correctly the stop command, how do I kill the dashbuttonscript? should I try with the $continue variable? or should I get, somehow, the process ID of the dashbuttonscript? I don't know very well about processes and PIDs.

An easy way to use a global variable is the use of an external file:
deamon:

touch /tmp/dashbutton.continue
while [ -f /tmp/dashbutton.continue ]
# stuff

stop:

rm /tmp/dashbutton.continue
1 Like

Thanks @Mijzelf !
I have tried your approach and it works perfectly!
Now I'm probably done solving the problem
Cheers
Borhacker

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.