What's new

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

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

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.

:)
 
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"
#
 
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.
 
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.
 
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
 
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:
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.
 
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.

:)
 
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.
 
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 :)
 
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.
 
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

:)
 
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:
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.
 
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?
 
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?
Hello there,

Apologies for the delay, i didnt realise you had made a comment. I guess it would but this way is better :D
 
Hello again everyone,

Since i have update my AX86U to the 388 branch the script has stopped working, and i can't for the life of me work out why. I flashed the 388 by doing a nuclear reboot, thank you L&LD :) . In total i have nuclear-ly rebooted the router atleast twice trying to figure this out. This is what i have so far for the WOL:

In services-start:

Code:
#!/bin/sh

arp -i br0 -s 192.x.x.x xx:xx:xx:xx:xx:xx
cru -a naswol "55 19 * * 6 /jffs/scripts/mynas"

The WOL script (file is called mynas):

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

I have checked the IP address / MAC address many many times. The script wasn't changed at all, it just does not work anymore. The script has chmod 755

Does anyone know if there has been any changes which would cause the script to stop working?

Thank you all for any help.

:)
 
Last edited:
Hello again everyone,

Since i have update my AX86U to the 388 branch the script has stopped working, and i can't for the life of me work out why. I flashed the 388 by doing a nuclear reboot, thank you L&LD :) . In total i have nuclear-ly rebooted the router atleast twice trying to figure this out. This is what i have so far for the WOL:

In services-start:

Code:
#!/bin/sh

arp -i br0 -s 192.x.x.x xx:xx:xx:xx:xx:xx
cru -a naswol "55 19 * * 6 /jffs/scripts/mynas
I thing you have missed ' " ' "cru -a naswol "55 19 * * 6 /jffs/scripts/mynas" <<< here "
 
Last edited:
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?

I tend to agree here - the phone is much more aware of the network than the network trying to detect if the phone is present.

Thinking out loud - it's early and I haven't had my coffee yet...

Android has this capability thru the ConnectivityManager API, so it can tell when the network has actually changed (e.g. from idleState to connected). Writing an app is likely outside of the skillset of OP, but I'm not making that assumption just yet...

This almost sounds like a job of IFTTT - https://ifttt.com/

And get the phone to send the magic packet when it attaches to the home network (and during a time/day of one's choice) - again, because it's easier for the phone to detect where it is...
 

Similar threads

Support SNBForums w/ Amazon

If you'd like to support SNBForums, just use this link and buy anything on Amazon. Thanks!

Sign Up For SNBForums Daily Digest

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