Restart script when trunk is down

Hi

I have seen this script in the forum but it is not working
#!/bin/bash

[email protected]
HOST=xxxxxx.dk
asteriskbin=/usr/sbin/rasterisk

thistrunk=Viptel_xxxxxxx
checkifok=$asteriskbin -rx "sip show peer $thistrunk" | grep Status | grep -wc OK
if [[ $checkifok == 0 ]]; then
$asteriskbin -rx "core restart now"
echo "Trunk is DOWN" | mail -s " - Trunk is Down [$HOST] -" "$EMAIL"
else
echo "Trunk OK"
 fi

I get "line 8: -rx: command not found"
I run Freepbx Current Asterisk Version: 13.9.1

is -rx not a command any more?

cb

Try replacing

with

asteriskbin=$(which asterisk)

Hi

it the same error
line 8: -rx: command not found

#!/bin/bash

[email protected]
HOST=xxxxx.dk
asteriskbin=$(which asterisk)

thistrunk=Viptel_xxxxxx
checkifok=$asteriskbin -rx "sip show peer $thistrunk" | grep Status | grep -wc OK
if [[ $checkifok == 0 ]]; then
$asteriskbin -rx "core restart now"
echo "Trunk is DOWN" | mail -s " - Trunk is Down [$HOST] -" "$EMAIL"
else
echo "Trunk OK"
 fi

cb

Your asteriskbin variable isn’t set.

Try using ‘back ticks’ (typically the tick mark next to the ‘1’ key).

asteriskbin=`which asterisk`

Hi

I have tried with
asteriskbin='which asterisk’
asteriskbin=“which asterisk”

but the same error: line 8: -rx: command not found

I hope someone have the answer

So you didn’t do what I said, and then you’re surprised it didn’t work? Use the back tick (the one under the tilde ~). One other thing that might be jamming you up you can try adding the “export” to your command

export asteriskbin=`which asterisk`

Hi

it also the same error I get line 8: -rx: command not found
and this is also tried
export asteriskbin=which asterisk

!/bin/bash

[email protected]
HOST=xxxx.dk
asteriskbin=which asterisk

thistrunk=Viptel_xxxxxxx
checkifok=$asteriskbin -rx “sip show peer $thistrunk” | grep Status | grep -wc OK
if [[ $checkifok == 0 ]]; then
$asteriskbin -rx “core restart now"
echo “Trunk is DOWN” | mail -s " - Trunk is Down [$HOST] -” "$EMAIL"
else
echo "Trunk OK"
fi

you can’t the back tick made in this forum I don’t know why
when I save the go away

CB

Can I just say as you debug this that it is a

idea…

This is like firing a nuke at your girlfriend because she didn’t answer the first time you called.

If a trunk goes down in a manner that can be fixed by “restarting asterisk” then you need to fix the problem that is making it go down NOT restart asterisk. If it is going down for some other reason then restarting asterisk doesn’t matter.

Hi

It is my VDSL there is going Down some time and than the only thing I can get it to Work again is to reboot freepbx
or restart asterisk, and it is only when it goes real Down if I unplug the ehternet from my modem an put back in everything is ok
perhaps this should not be so, but it is

hi

now it is working
Use the back tick around which asterisk and $asteriskbin -rx “sip show peer $thistrunk” | grep Status | grep -wc OK

#!/bin/bash

[email protected]
HOST=192.168.1.1
asteriskbin=`which asterisk`

thistrunk=Viptel_48390613
checktrunk=`$asteriskbin -rx "sip show peer $thistrunk" | grep Status | grep -wc OK`
if [[ $checktrunk == 0 ]]; then
asterisk -rx "core restart now"
echo "Trunk is DOWN" | mail -s " - Trunk is Down [$HOST] -" "$EMAIL"
else
echo "Trunk OK"
 fi