What's new

Help with cron job not runnning

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

TheHighFlyingBirds

New Around Here
Hi,

First post and novice, so go easy on me.

I'm using an ASUS RT-AX86U pro with merlin 3004.388.4 FW. I am trying to get a cron job running to check a VPN connection, and re-instate it if it has failed.

I have enabled the "enable jffs custom scripts and config" option.

I have created a script > vpncheck.sh which is located in /jffs/scripts and runs fine manually. Script is:
Code:
#!/bin/sh
# check state of VPN client1
#if = 0, VPN is down
#if != 0, VPN is running

if [[ $(nvram get vpn_client1_state) != 0 ]]
then
        echo "VPN Running - test last run at $(date)" | tee output.txt

elif [[ $(nvram get vpn_client1_state) == 0 ]]
then
        service start_vpnclient1
        sleep 10
        if [[ $(nvram get vpn_client1_state) != 0 ]]; then
                echo "VPN Restarted Successfully - test run at $(date)" | tee output.txt
        else
                echo "VPN Failed - test last run at $(date)" | tee output.txt
        fi
fi
When I run it manually it updates the output.txt file accordingly, and if the VPN is down, it reconnects it.

I have also created a services-started file, which contains the following:

Code:
#!/bin/sh
# This cript get called after all other system services
# have been started at boot on router
# ---------------------------------------------------------
# cron job to run vpn check script - this ensures cron task is started following reboot

cru a vpncheck "*/1 * * * * /jffs/scripts/vpncheck.sh"

I have applied chmod 777 to all three files in scripts folder, such that they are all -rwxrwxrwx

I then added the cron task using the following:

Code:
cru a vpncheck "*/1 * * * * /jffs/scripts/vpncheck.sh"

When I check the cron file in /var/spool/cron/crontabs/{my username} it shows up, with the output being:

Code:
*/1 * * * * /jffs/scripts/vpncheck.sh #vpncheck#
*/2 * * * * /etc/openvpn/server1/vpn-watchdog1.sh #CheckVPNServer1#
46 9 */7 * * service restart_letsencrypt #LetsEncrypt#

The task remains after a reboot. However, it just doesn't seem to run, as the output.txt file does not change and if I turn off the vpn it does not restart it. I cannot see any mention of cron in syslog.log, and doing a search of the other tasks also draws a blank.

I have even tried creating a simple cron task using the same approach as above and still nothing.

Can anyone help?

Thanks in advance.
 
It should be services-start not services-started. I assume that was a typo in your post.

You have not specified a full path for the output.txt file so that might be an issue (it's probably going to /tmp/home/root). There's also no terminal attached to a running cron process so that might also be causing problems for the tee command. I suggest you send all output to a fully qualified file path. For example:
Code:
echo "VPN Running - test last run at $(date)" >> /jffs/output.txt 2>&1
 
Thank you so much @ColinTaylor you are a genius; I have been trying for the last two days to get it working. Yes you were right services-started was a typo in my post. It was the lack of path for the output file that was causing it to not work.

I added the path as suggested and it started working. Replacing the tee with >> makes it add a new line as opposed to overwriting the previous entry.

The next problem is to work out how to have it so it only records the output for a fixed period of time, e.g. 2 weeks, so it will only store the output for the last two weeks, and anything older is deleted. Any ideas on that one :)?
 
The next problem is to work out how to have it so it only records the output for a fixed period of time, e.g. 2 weeks, so it will only store the output for the last two weeks, and anything older is deleted. Any ideas on that one :)?
It could get pretty ugly trying to "be clever" and doing it in the script. I think I'd use the KISS approach and setup a second cron job that ran twice a month and deleted the output file.

Code:
cru a Delete "01 03 1,15 * * rm /jffs/output.txt"

P.S. I'd also consider running your vpncheck.sh job every two minutes rather than every minute.
 
Last edited:
Thanks again, I will have a play, but your suggestion seems logical and far easier, which suits me.

Yes already changed task frequency to every 30 mins, only had it at 1 min whilst trying to get it to work.
 

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