Script to Monitor and Restart Jail

Status
Not open for further replies.

dnjordman

Dabbler
Joined
Jun 12, 2015
Messages
14
First, apologies for what I know is an extremely noob question. I'm running a jail on my FreeNAS box that for some unknown reason loses its internet connection periodically. Manually restarting the jail fixes the issue. I'm trying to write a script I can call via cron to check the status of the jail and restart it automatically if need be. Here's what I have for the script:

Code:
#!/bin/bash                                                                    
# MyJail Checker                                                            
                                                                               
mj_con_string='Current IPv4 Connections: '                                     
                                                                               
mj_status=$(/usr/local/bin/warden details myjail | grep -i                  
"$mj_con_string[0-9]\+")                                                       
                                                                               
mj_con_num="${mj_status//$mj_con_string}"                                      
                                                                               
if [ "$mj_con_num" = "0" ]; then                                               
        echo restarting myjail
        /usr/local/bin/warden stop myjail && /usr/local/bin/warden start myjail
else                                                                           
        echo myjail looks fine                                              
fi


In this example, the jail's name is "myjail." When the number of connections drops to zero, that's when I know it needs to be restarted. When I run this script, I get this output:

Code:
Usage: grep [OPTION]... PATTERN ...                                      
Try `grep --help' for more information.                                        
/usr/bin/mj_check: line 8: Current IPv4 Connections: [0-9]\+: command not found
myjail looks fine  


Something's clearly not right with my grep command, but I just don't know what it is. I've searched around and haven't really been able to figure out what I'm doing wrong. Any help would be greatly appreciated.
 

DrKK

FreeNAS Generalissimo
Joined
Oct 15, 2013
Messages
3,630
I'm about to go to bed, so I am not going to dissect the grep command, but I can tell you, I get PRECISELY this behavior if my router/switch resets. If I reboot the router, I will need to manually restart all of the jails, because they will all lose connection, and only the FreeNAS appliance IP itself will be accessible after such a reboot.
 

rogerh

Guru
Joined
Apr 18, 2014
Messages
1,111
I think the problem is that your "grep -i" argument has ended up on a new line. The brackets alone aren't sufficient for the shell to ignore the new line. Either put the ' "$mj_con_string[0-9]\+")' on the same line or escape the newline character with a "\" at the very end of the line that "grep -i" is on, after any white space.
 
Status
Not open for further replies.
Top