Skip to content

Instantly share code, notes, and snippets.

@justinemter
Last active July 18, 2017 21:53
Show Gist options
  • Save justinemter/3eed38dd8c0f5e1bfa4d05304ce44f6a to your computer and use it in GitHub Desktop.
Save justinemter/3eed38dd8c0f5e1bfa4d05304ce44f6a to your computer and use it in GitHub Desktop.
This script pings google to see if the WAN is alive... if the network is down it restarts my pfSense VM running on proxmox.
#!/bin/bash
# file: restart_pfsense.sh
# This is a solution to a problem I have with my pfSense network. When downloading big files often pfsense will stop
# and needs to be restarted. Or, sometimes pfSense will stay running but the WAN goes down for some reason. Rather than
# investigating further I've found that by restarting the pfSense VM (in Proxmox), then the WAN goes back up. Since it takes a minute
# or two to get the VM back up with WAN connection, I have a 10 minute delay to skip the restart if WAN isn't back up. Otherwise you'd
# keep restarting the VM before it had a chance to initialize. Seems to work great!
# Set Crontab to run this every minute:
# * * * * * /bin/bash /home/justin/scripts/restart_pfsense.sh > /dev/null 2>&1
domain=google.com
vmid=100
# The amount of time to wait between VM restarts - in minutes
time_pause=10
# Output file to write the timestamp to
output_txt_file=restart_pfsense_temp_time.txt
# Output path for the timestamp text file (/var/example.txt)
output_txt_path='/tmp'
# To find out the path of "qm", type "which qm"
qmToUse=/usr/sbin/qm
#----------------------------
ping -c 1 -4 -B -q $domain
RESULT=$?
if [ $RESULT -eq 0 ]; then
logger -i "++++++++++ ${domain} is UP."
else
logger -i "++++++++++ ${domain} is Down."
status=$(${qmToUse} status ${vmid})
if [[ $status == *"running"* ]]; then
logger -i "++++++++++ ${domain} is down, but pfSense is still running, lets restart it."
last_time=$(<$output_txt_path/$output_txt_file)
if [ $? == 1 ]; then
# If getting the last time stamp errs then maybe the file doesn't exist...
date +%s > $output_txt_path/$output_txt_file
last_time=$(<$output_txt_path/$output_txt_file)
fi
now_time=$(date +%s);
time_difference_seconds=$((now_time-last_time))
time_since_last_in_minutes=$((time_difference_seconds/60))
logger -i "++++++++++ " $time_since_last_in_minutes "minutes has passed since the last automated restart of pfSense."
if (( $time_since_last_in_minutes >= $time_pause )); then
logger -i "++++++++++ Stopping VM ${vmid}."
$qmToUse stop $vmid
sleep 15
logger -i "++++++++++ Starting VM ${vmid}."
$qmToUse start $vmid
# To void running into a start / stop loop I am saving the Unix time in seconds after the last qm start
date +%s > $output_txt_path/$output_txt_file
else
logger -i "++++++++++ Waiting to restart pfSense."
fi
else
logger -i "++++++++++ Apparently VM ${vmid} has stopped, restarting."
$qmToUse start $vmid
fi
fi
exit 0
@justinemter
Copy link
Author

Added temporary file for storing a timestamp of the last VM restart to check against so the VM isn't constantly starting / stopping if the WAN network is down for some other reason. It will still loop & stop / start VM every 10 minutes if WAN is down...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment