Reboot script assistance

Hello,

I would like to schedule a system reboot via cron and the below works almost perfectly. However, how do I get the script to wait until the “/usr/sbin/asterisk -rx ‘core stop gracefully’ >> $LOG” command completes before moving on to the next line? I tried a wait, but something is wrong and isn’t working properly.

I want to do a core stop gracefully just in case any calls are in progress when the script runs. using core stop gracefully will wait until the current call is finished and then shutdown Asterisk.

#Shutdown Asterisk cleanly, then reboot the server
NOW=$( date '+%F_%H:%M:%S' )
LOG=$'/opt/scripts/log.txt'
TIME=$(uptime)
echo " " >>$LOG
echo "======================================= " >>$LOG
echo $NOW >> $LOG
echo "Current uptime is:" $TIME >> $LOG

echo "Active Channels before stopping asterisk:" >> $LOG
/usr/sbin/asterisk -rx 'core show channels' >> $LOG

echo "Stopping Asterisk gracefully" >> $LOG
/usr/sbin/asterisk -rx 'core stop gracefully'  >> $LOG

#echo "Shutting down in 1 min" >> $LOG
/sbin/shutdown -r +1 >> $LOG

Any help would be appreciated. Thanks!

while [ -f /var/run/asterisk/asterisk.pid ];sleep 1 ;done&& reboot

Instead of stopping Asterisk, you can maybe simply hang up all active channels

echo "Hanging up all active channels" >> $LOG 
/usr/sbin/asterisk -rx 'channel request hangup all' >> $LOG

That would be exactly opposite of the graceful stop he intends.

I misread. Apologies.

So I’ll be the one to ask the questions. Why does the server need to be rebooted during a point where calls could be active? On top of that, why does this need to be done via a cron job to automate rebooting the server? This isn’t Windows, it’s Linux most things can be resolved by restarting or stopping/starting services on the system.

So again, why do you need to automate rebooting your PBX on a regular basis?

1 Like

I agree 100% But I am currently working through an issue on this server and this is a just temporary band aid until we can resolve the issue with Asterisk/FreePBX. (Which I am working with support to resolve)

What is this issue? Does it absolutely require the server itself to be rebooted or can restarting services like Asterisk or MySQL or whatever resolve the issue?

I am not 100% sure that a reboot is necessary. The problem is that the server stops accepting calls and phones disconnect from the server. It only happens about once a week. But ever since i started rebooting the system, the issue doesn’t happen. So rebooting is obviously just masking the problem until we figure out what is causing this.

OK, so now the next time this happens check to see what is going on. Is Asterisk running? But then just restart Asterisk/FreePBX by doing a fwconsole restart and see if that solves the issue. Then you can just do that instead of rebooting the entire system.

Thanks Dicko. This looks great. However, the asterisk.pid file will not go away, even after a asterisk -rx ‘core stop now’ or a ‘fwconsole stop’ The file just stays there in the directory, thus causing the while loop to never exit.

Yep, I will try that next time it happens.
Thanks

Well, it’s supposed to go away, maybe you have other problems like two asterisk processes running.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.