CRON Job help - Activate CRON job if a device is present

Net Noob

Occasional Visitor
Hi guys,

Im wondering if you can steer me in the right direction. I have a cron job in my AX86u in services-start which works fine. It to send a WOL to my NAS at a certain time on a certain day:

Code:
arp -i br0 -s 192.x.x.x xx:xx:xx:xx:xx:xx

cru -a naswol "55 19 * * 6 /usr/sbin/ether-wake -i br0 xx:xx:xx:xx:xx:xx"

So the first part marries the IP and the MAC upon router start in ARP so the cron job works without an entry needing to be present in ARP.

My question is: Is it possible to run the above lines only if my phone is present on the network? So, something like "IF 192.168.1.3 is present THEN run ............ ELSE exit. Iv tried this in a few ways but cant get it to work.

A lot of times i have been away for a few days, the NAS starts regardless and just sits there until i return and switch it off. My "NAS" is actually a windows 10 mini machine with 4 HDDs in storage spaces. Id put sleep on the machine but it doesnt really work very well for some reason.

Thanks a lot and sorry for the simple question.

:)
 

ColinTaylor

Part of the Furniture
If you want to use the IP address you can use ping and check the return code.
Code:
# ping -q -c1 192.168.1.1 > /dev/null
# echo $?
0

# ping -q -c1 192.168.1.5 > /dev/null
# echo $?
1

Otherwise you could use wl to check the wireless association of the MAC address.
Code:
# mac="B4:7C:9C:50:81:BF"
# wl -i eth6 assoclist | grep "$mac"
assoclist B4:7C:9C:50:81:BF
# wl -i eth7 assoclist | grep "$mac"
#
 

eightiescalling

Regular Contributor
I haven't tried running it but something like this might do it (assuming you're not interested in what's on teh guest wifi)
Code:
#!/bin/sh

# | seperated list of MACS that count as "I'm home"
AT_HOME_MACS="yy:yy:yy:yy:yy:yy|zz:zz:zz:zz:zz:zz"
SEND_WOL="no"

for WLAN_IF in $(nvram get wl_ifnames)
do
  wl -i ${WLAN_IF} assoclist | grep -E "${AT_HOME_MACS}"
  [[ $? -eq 0 ]] && SEND_WOL="yes"
done

[[ ${SEND_WOL} == "yes" ]] && /usr/sbin/ether-wake -i br0 xx:xx:xx:xx:xx:xx

A bit messy on lack of error checks and relies on the fact grep only gives a 0 response if it finds something but you get the idea.
 

Oracle

Regular Contributor
Which one is the part that you couldn't get to work?
Recognizing if your phone is in or the IF THEN ELSE logic?
A little hint: you can send messages from your code to the system log, so that you can see which part executes and where it stops / exits. Just put
logger name-of-your-script message
in a few places and you can debug it.
 

Net Noob

Occasional Visitor
Hey guys,

Sorry for the late reply. What i have so far is:

Code:
cru a naswol "10 14 * * 6


AT_HOME_MACS="aa:aa:aa:aa:aa:aa"

SEND_WOL="no"



for WLAN_IF in $(nvram get wl_ifnames)

do

  wl -i ${WLAN_IF} assoclist | grep -E "${AT_HOME_MACS}"

  [[ $? -eq 0 ]] && SEND_WOL="yes"

done



[[ ${SEND_WOL} == "yes" ]] && /usr/sbin/ether-wake -i br0 ff:ff:ff:ff:ff:ff"


I cant seem to get it to work. Iv put that in "services-start"

How would the ping thing work? Could someone please give me an example? Im sorry, i have no clue what im doing in this respect.

Thanks again
 

ColinTaylor

Part of the Furniture
Change the cru command in your services-start script so that it calls another script rather than the ether-wake command. For example,
Rich (BB code):
#!/bin/sh

arp -i br0 -s 192.x.x.x xx:xx:xx:xx:xx:xx
cru -a naswol "55 19 * * 6 /usr/sbin/ether-wake -i br0 xx:xx:xx:xx:xx:xx"
cru -a naswol "*/5 * * * * /jffs/scripts/myscript.sh"
As it was your cru command was only running once a week. I'm assuming you now want to run this more often, like every five minutes.

Then put your preferred code in the new script, e.g.
Code:
#!/bin/sh

AT_HOME_MACS="yy:yy:yy:yy:yy:yy"
SEND_WOL="no"

for WLAN_IF in $(nvram get wl_ifnames)
do
  wl -i ${WLAN_IF} assoclist | grep -q -i -E "${AT_HOME_MACS}"
  [[ $? -eq 0 ]] && SEND_WOL="yes"
done

[[ ${SEND_WOL} == "yes" ]] && /usr/sbin/ether-wake -i br0 xx:xx:xx:xx:xx:xx
Remember to make the new script executable, chmod 755 /jffs/scripts/myscript.sh

xx:xx:xx:xx:xx:xx is the MAC address of your NAS.
yy:yy:yy:yy:yy:yy is the MAC address of your phone as seen on System Log - Wireless Log.

EDIT: Note the code above is slightly different than that previously posted.
 
Last edited:

eightiescalling

Regular Contributor
How would the ping thing work? Could someone please give me an example? Im sorry, i have no clue what im doing in this respect.
Why the preference for ping? Is your phone set to have an allocated IP or left on normal DHCP? If you haven't given it a reserved IP then it'll have a random IP from the pool when you come back - at least if you've been away for a few days.

Assuming no funny business on the phone with it changing MAC addresses randomly (aka excessive privacy on the home network) then using the MAC(s) of the phone means it'll pick up irrespective of the phones IP.

If your setup means ping is preferable, then ditch the for loop entirely and use
Code:
ping -c 1 -w 1 <IP here>
[[ $? -eq 0 ]] && SEND_WOL="yes"

Again, not clean but if the ping succeeds you'll get the 0 response code it needs for the WOL packet.
 

Net Noob

Occasional Visitor
Change the cru command in your services-start script so that it calls another script rather than the ether-wake command. For example,
Rich (BB code):
#!/bin/sh

arp -i br0 -s 192.x.x.x xx:xx:xx:xx:xx:xx
cru -a naswol "55 19 * * 6 /usr/sbin/ether-wake -i br0 xx:xx:xx:xx:xx:xx"
cru -a naswol "*/5 * * * * /jffs/scripts/myscript.sh"
As it was your cru command was only running once a week. I'm assuming you now want to run this more often, like every five minutes.

Then put your preferred code in the new script, e.g.
Code:
#!/bin/sh

AT_HOME_MACS="yy:yy:yy:yy:yy:yy"
SEND_WOL="no"

for WLAN_IF in $(nvram get wl_ifnames)
do
  wl -i ${WLAN_IF} assoclist | grep -q -i -E "${AT_HOME_MACS}"
  [[ $? -eq 0 ]] && SEND_WOL="yes"
done

[[ ${SEND_WOL} == "yes" ]] && /usr/sbin/ether-wake -i br0 xx:xx:xx:xx:xx:xx
Remember to make the new script executable, chmod 755 /jffs/scripts/myscript.sh

xx:xx:xx:xx:xx:xx is the MAC address of your NAS.
yy:yy:yy:yy:yy:yy is the MAC address of your phone as seen on System Log - Wireless Log.

EDIT: Note the code above is slightly different than that previously posted.
Thank you so much!

Its finally worked.

Why the preference for ping? Is your phone set to have an allocated IP or left on normal DHCP? If you haven't given it a reserved IP then it'll have a random IP from the pool when you come back - at least if you've been away for a few days.

Assuming no funny business on the phone with it changing MAC addresses randomly (aka excessive privacy on the home network) then using the MAC(s) of the phone means it'll pick up irrespective of the phones IP.

If your setup means ping is preferable, then ditch the for loop entirely and use
Code:
ping -c 1 -w 1 <IP here>
[[ $? -eq 0 ]] && SEND_WOL="yes"

Again, not clean but if the ping succeeds you'll get the 0 response code it needs for the WOL packet.

Thank you so much for this, on second thought, it would be better using MAC instead of IP.


Thank you to all who helped / viewed this. Hopefully it will help others in the future.

:)
 

Oracle

Regular Contributor
ColinTaylor has given an excellent answer, yet I'm still not clear what Net Noob actually wanted.
E.g.,
- run the script at a specified time, once a week, and only if the phone happens to be on the subnet at that precise moment, execute the WOL;
- check regularly if the phone "is home" and then do the WOL.

The nice thing is the code above will work for both cases, it just depends how often the cronjob is executed.
 

Net Noob

Occasional Visitor
ColinTaylor has given an excellent answer, yet I'm still not clear what Net Noob actually wanted.
E.g.,
- run the script at a specified time, once a week, and only if the phone happens to be on the subnet at that precise moment, execute the WOL;
- check regularly if the phone "is home" and then do the WOL.

The nice thing is the code above will work for both cases, it just depends how often the cronjob is executed.
Hi there,

Apologies, I was a bit vague. I have an android phone and iv lost all my data a few times on it due to.... well stupidity mostly. So, I back it up to my NAS through an app on my phone at a specific time.

My NAS needs to be switched on at this time and i need to be at home. There is no need to keep the NAS on all the time so I thought I'd try to figure out how to use CRON and wake on lan once a week at a specific time where I am usually home.

The bit I figured out worked fine. But when I'm not at home, the NAS starts up for no reason. Hence, I wanted to refine what I had so that the NAS starts up only if my phone was on the network, that way I'm definitely home, the NAS can start and my phone can be backed up onto it.

Thank you again for your time :)
 

Oracle

Regular Contributor
I like this.
Do you mind sharing how you do the phone backup?

I know most people prefer to use Google for contacts and apps and put all their photos and videos on FB, Instagram and so on. I'm one of those dinosaurs that avoids social networks and prefers to keep their data to themselves.
 

Net Noob

Occasional Visitor
I like this.
Do you mind sharing how you do the phone backup?

I know most people prefer to use Google for contacts and apps and put all their photos and videos on FB, Instagram and so on. I'm one of those dinosaurs that avoids social networks and prefers to keep their data to themselves.

Hiya,

So, I use a free app called smbsync2 and iv set that up to see my NAS. There are a whole load of options in there; sync types, filters, method to determine changed files, all sorts. In that app you can set a schedule to run at certain times where you can set specific jobs in that schedule.

What I do is sync my whole internal storage to my NAS. Be careful though. There is an option to run a sync in test mode. I'd definitely advise it as there will be some files that you can't sync, mostly android/data stuff and a job will just error out when smbsync2 comes across one. There is a very good history and message section where you can find out what is going wrong and where. Here you would just add that file / folder to the exclude filter. Once you've tested and altered until you have no errors you are good to go. Use the above script to switch your NAS on at a specific time and time your smbsync2 schedule accordingly.

There is only one caveat though. Smbsync2 is verrrrry slooooow. The best transfer speed for files I have got I'd about 4MB/sec. Iv tried everything to speed it up but nothing has worked. From what I can tell, it's an android thing, smb 2 / 3 just doesn't work well on it.

There are probably better faster options but this works for me. The first sync took forever but subsequent syncs take around 20 mins. I don't take a lot of pics etc.

If anyone else wants to pipe in with any better apps, please feel free. There is FTP, webdav etc, each with their own pros and cons, however, my NAS is windows 10 and Smbsync2 is free and works, i don't think there ads (or diversion has taken care of them! :) )

Your use case will be different to mine and therefore YMMV.

I hope this helps

:)
 

ColinTaylor

Part of the Furniture
There is no need to keep the NAS on all the time so I thought I'd try to figure out how to use CRON and wake on lan once a week at a specific time where I am usually home.
Ah, OK. I didn't realise that (I thought you wanted the NAS on all the time your were at home). So you'll need to set the schedule of your cron job the way you had it before (55 19 * * 6) rather than every 5 minutes (*/5 * * * *) like I thought.
 
Last edited:

eightiescalling

Regular Contributor
If anyone else wants to pipe in with any better apps, please feel free.
The backup scenario makes sense on the WOL packet. Interesting setup with the smbsync2 app - there are many that don't trust Google or Facebook to handle their backups! :) (Ignoring for a minute Facebook isn't a backup because of the way it drops photo quality/sizing...)

The only thing I would say is that RAID isn't backup (assuming that's the reason for the 4 disks in your Windows machine). It gives you some data resilience but not backup. I have similar DIY NAS setup (LInux rather than Windows but...) and have recovered the array from disk failure, motherboard failure and OS screwups (to name a few things over the years) but I wouldn't forego the independent backups.

Given the way you clearly care about your data, don't forget the backups. :)

@ColinTaylor Good tweaks to the script, thanks for adding.
 

dosborne

Very Senior Member
Wouldn't it be better to have your phone initiate the WOL before it performs the backup instead of having the router wake up the MAS just in case it is needed?
 

Similar threads

Latest threads

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top