Auto-reconnect script doesn't run under CRON

Hi all,

I do have a problem that took me hours to solve, but without success.
I have a script fired by cron every 15 minutes. It check if node T1 is present in the node lists. If not it connects to it.

While the script works perfectly when fired manualy, it fails under cron.
The CHK variable is always 0 (zero), as if the test were never performed.

I’ve checked everything that was in my knowledge… I’m stucked.
Does someone has an idea ?

Here my script :

#!/bin/bash
date >> /usr/tknet/cron_check.txt
CHK=
echo "Checking if connected to the hub"

CHK=`asterisk -rx "rpt nodes 2" | grep -c "T1"`
echo $CHK >> /usr/tknet/cron_check.txt

if [ $CHK -eq 1 ]
then
  echo "OK, Connected."
  echo "OK, connected." >> /usr/tknet/cron_check.txt
else
  echo "Reconnecting."
  echo "Reconnecting." >> /usr/tknet/cron_check.txt
  asterisk -rx "rpt cmd 2 ilink 3 1"
fi

Subsidiary question: does the asterisk scheduler use the same process as cron and can it schedule a script ?

73 Patrick

Just a quick check Patrick, without looking at your script,

could you show your entry in crontab for running the script.

More likely some fat finger there.

But to answer the other, you can run this from the system sched.
As long as the file permissions of the .sh permissions and ownership are in line with user/group asterisk

While many will tell you one way is better than the other, and having done both for more than a decade (and a lot of them), I find little difference. Not that might be true in all instances.
For me, sometimes it comes down to if I need quicker access to change it periodically. I am in the rpt.conf file at least weekly and do a reload of dialplan with changes.
Choose method best for your need. Pay little attention to anyone else LOL.
But it’s nice to know what I’m doing with asterisk inside of asterisk in one place to avoid timing conflicts.
(doing to many things at the same time can effect network performance)

Hi Mike,
Thanks for answering.

Here my cron entry:

*/15 * * * * /usr/tknet/autoconnect > /dev/null 2>&1

autoconnect being the script I want to run.

In the script, I’ve added a sort af logging, and I’m sure the script is fired every 15min. But it does not give the good result. :weary:

73 Patrick

One problem in this forum software is that sometimes formatting get askew for text.
So, I will not judge what I see.

here are a few simple example crontab entries you might follow by example.

29 8 * * * root asterisk -rx reload

27 05 * * * root cd /var/www/html/allmon; ./astdb.php

12 0 * * * root ./var/www/html/audio/recorder-maint.sh

in your line rpt nodes 2 < the 2 has to be your nodenumber that you are checking and in “T1” should be the node number you are looking for ex: T1999 or T43567

then in the rpt cmd 2 < the 2 needs to be replaced with your nodenumber that you did the check on. 1 at the end of the same line should be the node number that you want to link.

that should solve your issue. Here is what I did and it works. try it this way. just replace the node numbers that I have in there with your node numbers that you are looking for. You do not need to set a Null for it to run in your crontab. you just need this: */15 * * * * /usr/tknet/autoconnect

#!/bin/bash
date >> /usr/tknet/cron_check.txt
CHK=
echo “Checking if connected to the hub”

CHK=asterisk -rx "rpt nodes 457430" | grep -c "57841"
echo $CHK >> /usr/tknet/cron_check.txt

if [ $CHK -eq 1 ]
then
echo “OK, Connected.”
echo “OK, connected.” >> /usr/tknet/cron_check.txt
else
echo “Reconnecting.”
echo “Reconnecting.” >> /usr/tknet/cron_check.txt
asterisk -rx “rpt cmd 457430 ilink 3 57841”

With Asterisk v 1.01, I have found that you need to be root in order to execute “asterisk -rx” (etc),

That means you need to use the root crontab. (The user crontab fails silently).

One way to do this is to invoke the crontab editor with:

sudo crontab -e

You will see a different crontab file than the user crontab.

GL
Ken
KE2N

To be straight on a follow up, Since you know as you stated the script runs as it should from a command line,

12 0 * * * root ./var/www/html/audio/recorder-maint.sh

The user is specified in the crontab line. And it should be executed syntax just the same as if you executed it from a command line by the specified user.

I am not sure how the redirect output quash (/dev/null 2>&1) is helping you see any issues.
I believe this prevents error logging as well.

Let us know how you make out.

Hi Ken & Jim,

Thanks for your concern.

I finally solved my problem on all of my nodes.
In fact, I do have the later one being an ASL 2 and the older ones ASL 1.
Indeed do they react differently !

I’ve tried all sort of things that you suggest, before asking help.

I was sure that it was root that was executing the cron and scripts, etc… I also was sure that root had all rights (chown and chmod)
Also the syslog indicated that the cron was fired.

Adding root or sudo in the commands did make more troubles than cures !

Ken your trick with sudo crontab fails on ASL1 and probably older Debian versions, but works on Debian 10 with ASL2.

The script was working perfectly when executed from the console, but failed on ASL1 while working on ASL2.

We do have weird node numbers as we run a private network that the first sysop numbered this way. 1 being the server and 2,3,4 etc… the nodes.

I’ve added some tracing and found out that the grep never worked.

At first, I didn’t see it because I was making the test searching for to detect a broken link.
This returned 0, but I couldn’ make a difference between a comparison and a failure !

So, I made the test checking if the link was active and returning 1. :grinning:
That brought me a step further.

The redirect output is to prevent logging and not sending a mail. We don’t have a mail server installed.

Finally I added the full path everywhere in the script:
/usr/sbin/asterisk -rx
/usr/tknet/reconnect

Et voilà, it works or at least seems to work ! One never knows !! :rofl:

Thanks again,
73 Patrick TK5EP

1 Like

Thanks for the update as I learned something as well.

I have only one install under deb 10 and I don’t think I ever edited the crontab on it.
See what I get for assuming.

My centos boxes have a proprietary management, so no reference there.
So yes, I gave info for debian 9 /asl 1.01
You might not notice, but this is the kinda stuff that keeps me from upgrading my systems very often. It breaks many scripts. And I have a bunch. It just takes to long to sort out, but not permanent.

For others, I found a link to deb 10 crontab info…

That should help those that never dabbled as well as newbs like me LOL


After a quick test review…
I see that for those that manually directly edit /etc/crontab file, like me, there really is no change
It’s just I don’t have users, I run all as root. Probably not a good practice for most.

A different environment exists under cron, so you might try sourcing the environment variables first, e.g. (beware line-wrap here):

*/15 * * * * source /usr/local/etc/allstar.env; /usr/tknet/autoconnect > /dev/null 2>&1

Hi,

Now that my script works nicely, I would like to make it even better. :grinning:

Is there a variable that can be tested in a bash script that can test if the link has been disconnected by the sysop (using ilink,1) and not broken ?

Thanks,
Patrick TK5EP

So, if I understand you correctly…

You are looking for a var to know how the disconnect came to be.
If so, then no. No ability to know the HOW with out perhaps probing logs.

A disconnect is a disconnect. If it came from a user at the other end or perhaps a link timeout timer.
You would need access to the targets logs. It would show there if initiated from there.

After a bit of thought,
Could the issue you are having be related to ‘link inactivity timer’ on the target side ?
or even your side?

If set, it will disconnect you after ‘timer’ period of no key-ups.
This prevents a node from just loitering around, in cases where you have limited bandwidth/connections,
These can be disabled by setting them to ‘0’ since your connections are private network and you likely know everyone.

Thanks MIke,

My concern is that if the link has been closed by a sysop command, I don’t want the autoreconnect script to establish the link. when the cron runs.

What are these targets logs ?

Patrick

Sorry for not being clear on that.
That is the logs at your intended target connected node.(the node you are imitating a permalink for)

I would say, ask the sysop at the other end. Tell them what you are doing and find if there are prerequisites for it.(if they have a grip or will change something they might be doing against you potentially)

You know it could be that they are using some kind of script to limit how many dead beat nodes loiter from a bandwidth issue they have. Don’t know, but explaining some possibilities. Ask and find out.
There are many possibilities…

But I might ask, since I know as you have said you are ‘all private nodes’
if the target connection is a private node as well?

Hi,

It’s me who should have been clearer…

We’re on a private network, so we have control over everything.

We want to keep our links permanent.

At first, I made the connections permanent using ASL permalink.
BUT the problem (one of my other posts) is that the courtesy tone DO NOT send the unlinkedct when the link is broken.
I understand that it’s not a bug, it’s a feature. :grinning:

So I tried to make a script + cron that auto-reconnects a broken link.
That works now perfectly.
BUT with another side effect : when a sysop disconnects a node for any reason (QRM, tests, etc), the auto-reconnect doesn’t care of this and reconnects.

SO, I have 2 solutions :

  • finding a trick to make the courtesy tone send the unlinkedct with permalinks
  • finding a trick so that the auto-reconnect knows that the link has been shutdown intentionally.

I hope this is clearer.

73 Patrick.

So Patrick,
I did not forget about your issue.

I needed some time to test the syntax of what I could not remember. (I have a issue with that)

So, while I will probably use this is some other scripts myself, as I think I did this a long time ago but can not find the files.

Here is what I tested that you will have to incorporate in your scripts pretty much on your own.

First, I don’t think this was ever documented. But you need to set your ct for linked ct to a sound file.
Choose the correct one not my example.
ct2=and
Test it as is -ct#. Should play the ulaw file located in /var/lib/asterisk/sounds named ‘and’.ulaw

So, in your script, you need to be able to swap the value of a sound file you produce as a .ulaw into that directory. In fact multiple files for each indicator you want.
But know you can not change the name on the fly. So you will actually be copying the file you want over it based on the actions you need in the script.

So, let’s say we name the file ctindicator.ulaw

ct2=ctindicator

You only need for your script to overwrite that /var/lib/asterisk/sounds/ctindicator.ulaw with the one you need. Of course you need to create them, perhaps with audicity and a recorded/converted sound.

It works. I have done the same with rotating ID’s and tail msgs years ago.

So, there is at least one method for you to try.
It’s all in the shell script you have already framed up by testing the link and the one setting in rpt.conf

But perhaps someone has a better idea?

So, one additional thought for you on this.

If you were to declare a system wide variable at the beginning of extensions.conf for the name of this sound,
ctstatus=normal ctstaus=broken - $ctstatus / ${ctstatus}

You should be able to use it as the file name and change the value of the var as needed.
So, a change on the fly is possible without overwriting the single sound file via bash.
But you would need to change the value of the var name there.
Help for that can be found by searching ‘asterisk global variables’.

This method could be used as well for those wanting to declare a separate CT on various inputs.
Something I was meaning to suggest some time ago as that request seems to come around once a year for a very long time.
But I could not remember how the sound file was called. And some more notes that the path can be specified when calling the sound file ie
ct2=/etc/asterisk/cts/linkbroken.ulaw
and the filespec is not required if it is a ulaw file. So a option to keep them organized.

So, now I have tested it. It is up to your scripting. Let me know how you make out.

After some additional testing, I will add it to the wiki so it is documented and suggest some other uses.
Many are obvious from what I have said here. But I think there are some more.

Just curious Patrick if you worked out a solution as I am sure others would have an interest,
if not now, in the future. These posts are forever.

Hi Mike,

Sorry if I didn’t come back earlier.
My solution works fine for now.

I didn’t try to change the beeps and only wrote a script that is fired by crontab every x minutes and reconnects the link if necessary.
It proved to work as we actually have a lot of lightnings and our 5 GHz links are sometimes disconnected.

73 Patrick

1 Like