What's new

Cron job example to do a hearbeat ping

  • 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!

Hi all. Is there a script where I can ping www.google.com every 5 seconds as a heartbeat? My VPN gets shaped if there is no traffic.

Cron has down to the minute triggering, so you would need to create a script that runs for 1 minute pinging google 12 times during its execution.

Ping Script that should saved as /jffs/scripts/ping_script:
Code:
#!/bin/sh
ping google.com -c1
sleep 5s
ping google.com -c1
sleep 5s
ping google.com -c1
sleep 5s
ping google.com -c1
sleep 5s
ping google.com -c1
sleep 5s
ping google.com -c1
sleep 5s
ping google.com -c1
sleep 5s
ping google.com -c1
sleep 5s
ping google.com -c1
sleep 5s
ping google.com -c1
sleep 5s
ping google.com -c1
sleep 5s
ping google.com -c1
sleep 5s
ping google.com -c1

Cron Entry:
Code:
cru a ping_script "* * * * * /jffs/scripts/ping_script"

---

The above would only be active until you reboot the router.

To make it persistant, you would have to add a cron "ping_script" entry upon reboot into "init-start"

if /jffs/scripts/init-start already exists and is configured to accept sh execution
Code:
echo 'cru a ping_script "* * * * * /jffs/scripts/ping_script"' >> /jffs/scripts/init-start

if /jffs/scripts/init-start does not exist / is not configured
Code:
echo "#!/bin/sh" > /jffs/scripts/init-start
echo 'cru a ping_script "* * * * * /jffs/scripts/ping_script"' >> /jffs/scripts/init-start
chmod 0755 /jffs/scripts/init-start

( Why not ping bing? :rolleyes: )
 
Last edited:
Cron has down to the minute triggering, so you would need to create a script that runs for 1 minute pinging google 12 times during its execution.

Ping Script that should saved as /jffs/scripts/ping_script:
Code:
#!/bin/sh
ping google.com -c1
sleep 5s
ping google.com -c1
sleep 5s
ping google.com -c1
sleep 5s
ping google.com -c1
sleep 5s
ping google.com -c1
sleep 5s
ping google.com -c1
sleep 5s
ping google.com -c1
sleep 5s
ping google.com -c1
sleep 5s
ping google.com -c1
sleep 5s
ping google.com -c1
sleep 5s
ping google.com -c1
sleep 5s
ping google.com -c1
sleep 5s
ping google.com -c1

Cron Entry:
Code:
cru a ping_script "* * * * * /jffs/scripts/ping_script"

---

The above would only be active until you reboot the router.

To make it persistant, you would have to add a cron "ping_script" entry upon reboot into "init-start"

if /jffs/scripts/init-start already exists and is configured to accept sh execution
Code:
echo 'cru a ping_script "* * * * * /jffs/scripts/ping_script"' >> /jffs/scripts/init-start

if /jffs/scripts/init-start does not exist / is not configured
Code:
echo "#!/bin/sh" > /jffs/scripts/init-start
echo 'cru a ping_script "* * * * * /jffs/scripts/ping_script"' >> /jffs/scripts/init-start
chmod 0755 /jffs/scripts/init-start

( Why not ping bing? :rolleyes: )

This is so very helpful. who should I ping. akamai, bing or google. just curious. the idea is just to keep it alive.
 
Last edited:
I know. :)
Also how can I tell if the script is pinging? Is there a way to test?

I tried to see if netstat would show the connection but it wouldn't.

The next easiet step would be to check ps (task manager) if your created script is currently running via this command

Code:
ps | grep "ping_script"

To really be sure, it is possible to setup an iptable rule entry to log all outgoing connections opened by the router itself into the system log and then manually analyze that.
 
I tried to see if netstat would show the connection but it wouldn't.

The next easiet step would be to check ps (task manager) if your created script is currently running via this command

Code:
ps | grep "ping_script"

To really be sure, it is possible to setup an iptable rule entry to log all outgoing connections opened by the router itself into the system log and then manually analyze that.

Or alternatively, I would think if I can ping a ping logger website that would enlist a hit or a local ping and monitor a hit then that would suffice too?
 
Or alternatively, I would think if I can ping a ping logger website that would enlist a hit or a local ping and monitor a hit then that would suffice too?

There are many ways to achieve the same solution so use whatever you feel comfortable with.

eg simply logging to system log upon execution or once per ping would also work aswell

Code:
#!/bin/sh
logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1

Welcome to linux! This router taught me so much. Linux is so flexible and open its insane!
 
There are many ways to achieve the same solution so use whatever you feel comfortable with.

eg simply logging to system log upon execution or once per ping would also work aswell

Code:
#!/bin/sh
logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1

Welcome to linux! This router taught me so much. Linux is so flexible and open its insane!

Great. I'll try the logger way :)
 
What not use the -i parameter to specify the interval (12 x 5 seconds = 60 seconds):

ping -i 5 -c 12 www.google.com

With the firmware I use cron puts a message in syslog every time it runs. So having something run every minute is rather annoying. Given that this is a non-critical task I'd be tempted to run the cron job every 15 minutes and use the following command.

ping -i 5 -c 180 www.google.com

I have to do the same sort of thing with my cable modem to keep the management interface working. But in my case I only need to ping it once every 5 minutes. I log the output to a file in /jffs.
 
There are many ways to achieve the same solution so use whatever you feel comfortable with.

eg simply logging to system log upon execution or once per ping would also work aswell

Code:
#!/bin/sh
logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1

Welcome to linux! This router taught me so much. Linux is so flexible and open its insane!


Can't thank you enough. This is what I have done. For not I have done a ping to google.com every 15 seconds. Will test how it works. I've used the logger command for now and once per script to see how it works. I'll leave it like that for a few days and will eventually remove the logger command from there.

ping_script
#!/bin/sh
logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 15s
ping google.com -c1
sleep 15s
ping google.com -c1
sleep 15s
ping google.com -c1

In the init-start I have added this line in the end.
cru a ping_script "* * * * * /jffs/scripts/ping_script"

For now it seems to be logging well which may also mean that it is pinging right. Hope so. :)

If this works this is a perfect solution.
 
There are many ways to achieve the same solution so use whatever you feel comfortable with.

eg simply logging to system log upon execution or once per ping would also work aswell

Code:
#!/bin/sh
logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1
sleep 5s

logger -t "ping_script" -s "pinging google.com"
ping google.com -c1

Welcome to linux! This router taught me so much. Linux is so flexible and open its insane!

Thank you so much for your help, however, the ping on the router didn't cause the VPN to remain alive. This could happen due to a few issues in my opinion such as.

ping on google.com is getting routed through the internet and not through the VPN as it was on the laptop.

Is there a way to get the output of the ping to the logger.? is the cron job using the same route as when the command is being run on terminal?

for example if I ping www.google.com on local network, I'll get 10ms in response. however when its on VPN it will give a response of 189ms. hence I know its going through the vpn.

Alternatively do you suggest to increase the size of ping packets?
 
Thank you so much for your help, however, the ping on the router didn't cause the VPN to remain alive. This could happen due to a few issues in my opinion such as.

ping on google.com is getting routed through the internet and not through the VPN as it was on the laptop.

The ping will be exactly the same as you SSH'ing into the router and preforming it manually. So the answer completely depends on how you configured the client.

Is there a way to get the output of the ping to the logger.? is the cron job using the same route as when the command is being run on terminal?

for example if I ping www.google.com on local network, I'll get 10ms in response. however when its on VPN it will give a response of 189ms. hence I know its going through the vpn.

Alternatively do you suggest to increase the size of ping packets?

I think the real issue here is your VPN provider. I've never heard of any provider closing a session or throttling it due to inactivity over a 5s period. Sounds like its time to start shopping for a new VPN.
 
The ping will be exactly the same as you SSH'ing into the router and preforming it manually. So the answer completely depends on how you configured the client.



I think the real issue here is your VPN provider. I've never heard of any provider closing a session or throttling it due to inactivity over a 5s period. Sounds like its time to start shopping for a new VPN.

It's really not the VPN Provider but the country where this is being achieved in. The Service Provider kills all encrypted channels when not in use for an elongated period of time. If I run a ping sequence on my laptop using ping plotter, the connection is alive and works well the next day. The provider is Astrill and they are good. Its the service provider that is annoying. Almost all Protocols are shaped with this provider.
 
for example if I ping www.google.com on local network, I'll get 10ms in response. however when its on VPN it will give a response of 189ms. hence I know its going through the vpn.
Log onto the router through SSH and issue the ping command interactively. Then you can see what the response time is.
 
Log onto the router through SSH and issue the ping command interactively. Then you can see what the response time is.

I did some indepth tests on the router to see the route with and without VPN and concluded that the router was NOT going through the VPN. I have not done an exclusion rule on the VPN Applet and the router is now routing through the vpn tunnel. I have restored the nightly restart as well. Will give it another go and hopefully "fingers crossed" will be able to report some success tomorrow morning.

I have also changed the ping to once in 20 seconds and the ping ip to 8.8.4.4 instead of a dns resolution of google.com
 
I did some indepth tests on the router to see the route with and without VPN and concluded that the router was NOT going through the VPN. I have not done an exclusion rule on the VPN Applet and the router is now routing through the vpn tunnel. I have restored the nightly restart as well. Will give it another go and hopefully "fingers crossed" will be able to report some success tomorrow morning.

I have also changed the ping to once in 20 seconds and the ping ip to 8.8.4.4 instead of a dns resolution of google.com
Try ping -I tun1X (or whatever interface name astrill uses)
 

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