Restart Asterisk from IVR

In order to release channels and to reregister, just in order to solve some un-explained issues while being remote on the phone…

maybe even a way to restart the computer (release memory etc…)

can anyone help?

First, you should set this up via SSH.
And before I give any suggestions, BE WARNED, THIS MAY AFFECT SYSTEM SECURITY, SO USE WISELY

To have the server reboot:
Do the following:

  1. cd to /var/lib/asterisk/agi-bin
  2. Create a file named restartserver.sh (owned by asterisk.asterisk - so chown asterisk.asterisk restartserver.sh)
  3. In that file put the following:
#!/bin/bash

##logger is a program which can send commands to syslog
logger "System Rebooted requested from /var/lib/asterisk/agi-bin/restartserver.sh"
## If logger is not installed, comment out the line above, this is just for an extra /var/log/messages log record.. I like having logging like that

# Run the restart command
/sbin/shutdown -r now

(I WOULD NOT RECOMMEND MESSING WITH PERMISSIONS TO GET IT TO FULLY KILL AND RESTART ASTERISK.
It is much better to do this as root MANUALLY via SSH.)
However, you could get an agi script to run an asterisk reload, but not a full amportal kill or similar, as those
scripts need to run as root, and would require LOTS of permission fixing, or giving the asterisk a password for sudo,
also bad, and would GREATLY reduce security of your asterisk system… However, just a server reboot doesn’t require as many changes… keep reading…

To be able to restart the PBX, you would need to do the following (or similar):
NOTE: Here is where you start messing with system security, so either know what you are doing and the implications, or don’t do it!

chmod u+s /sbin/shutdown
(Change the command shutdown to allow any system user to execute it)
(To revert this, run chmod u-s /sbin/shutdown, if you no longer want this functionality)
There are more secure approaches, so you should research and try to implment them instead!!

Now, let’s setup asterisk to handle this:
Put the following in extensions_custom.conf (anywhere, the bottom is fine)

[custom-restart-server]
exten => restartserver,1,Answer
exten => restartserver,2,swift("The P B X will now hang up and completely restart")
; If you do not have Swift installed, remove this line, and change the 3 & 4 in the next lines to 2 and 3 respectively
exten => restartserver,3,AGI(restartserver.sh)
exten => restartserver,4,Hangup()

Ok, now to configure your freepbx to get this to work
Go into Tools->Custom Destinations

Add a custom Destination like this:
Custom Destination: custom-restart-server,restartserver,1
Quick Pick: (do not touch this)
Description: AGI(restartserver.sh) <-- This is cosmetic, to make it easier to find what this does and not get confused!!
Notes: AGI Script to Restart the PBX

And click Submit

Now, here is how I restricted my restart server options…

I added a “hidden” menu option to my main IVR (an option such as 666 which is not in the message that plays)
Which redirects to a NEW IVR I create

This new IVR just plays a message "Please enter your pin #"
The pin # is a 10 digit number I have, which is really just an IVR menu option
And as the destination for that, I have the customer application AGI(restartserver.sh)
I have the t and i options in that IVR to terminate call if it is incorrect or takes too long.

Now, restart asterisk(apply configuration changes)

Voila, you should now be able to dial in, enter your hidden ivr option, then enter your “pin” (ivr option) and the server will reboot.