The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

new shell script calling restartsrv httpd

Discussion in 'cPanel Developers' started by jimlongo, Nov 14, 2013.

  1. jimlongo

    jimlongo Well-Known Member

    Joined:
    Mar 20, 2008
    Messages:
    145
    Likes Received:
    2
    Trophy Points:
    18
    Hi, I've developed a script to check for new VERSION of Atomic Rules and update if necessary.

    I've tested that to my satisfaction, however the part that makes me a little nervous is the restart of Apache.

    currently I'm storing a variable and then calling that at the end of the script

    Code:
    RESTART_APACHE="/usr/local/cpanel/scripts/restartsrv httpd"
    ...
    if ! $RESTART_APACHE; then
    	echo "ERROR: Apache did not restart properly!"
    	exit 1
    fi
    
    
    Is that a safe approach?
     
  2. KostonConsulting

    KostonConsulting Well-Known Member

    Joined:
    Jun 17, 2010
    Messages:
    255
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    San Francisco, CA
    cPanel Access Level:
    Root Administrator
    A couple of things. If you're trying to execute the command and capture its output, use this command:

    Code:
    RESTART_APACHE=$(/usr/local/cpanel/scripts/restartsrv httpd)
    
    Double quotes just saves the string to the variable.

    Now, before restarting Apache, you'll want to ensure you have little to no chance to bring it down. So, I'd recommend testing to ensure the config is not broken. You can check this with:

    /usr/local/apache/bin/apachectl configtest

    It will output 'Syntax OK' if there are no errors but you may get false positives as it can output warnings for duplicate virtualhosts and such so you may need to tweak which patterns are allowed.

    Code:
    APACHE_CONFIG=$(/usr/local/apache/bin/apachectl configtest 2>&1)
    if [[ $APACHE_CONFIG =~ "Syntax OK" ]]; then
        echo "Apache config is ok."
    else
        echo "Apache config problem"
    fi
    

    Restarting Apache via the cPanel script gives this type output:

    # /usr/local/cpanel/scripts/restartsrv httpd
    Waiting for httpd to restart.....finished.

    httpd (/usr/local/apache/bin/httpd -k start -DSSL) running as root with PID 25532 (process table check method)

    Apache successfully restarted.


    So, you'll want to test for the key phrase 'Apache successfully restarted', like so:

    Code:
    RESTART_APACHE=$(/usr/local/cpanel/scripts/restartsrv httpd)
    
    if [[ $RESTART_APACHE =~ "Apache successfully restarted" ]]; then
        echo "Apache restarted successfully"
    else 
        echo "Apache restart failed: $RESTART_APACHE" 
    fi
    
    So, putting in the configtest check first gives you:

    Code:
    #!/bin/bash
    APACHE_CONFIG=$(/usr/local/apache/bin/apachectl configtest 2>&1)
     
    if [[ $APACHE_CONFIG =~ "Syntax OK" ]]; then
        echo "Apache config is ok. Restarting Apache."
    
        RESTART_APACHE=$(/usr/local/cpanel/scripts/restartsrv httpd)
    
        if [[ $RESTART_APACHE =~ "Apache successfully restarted" ]]; then
            echo "Apache restarted successfully"
        else
            echo "Apache restart failed: $RESTART_APACHE"
        fi
    else
        echo "Apache config problem. Will not restart apache: $APACHE_CONFIG"
    fi
    
     
  3. jimlongo

    jimlongo Well-Known Member

    Joined:
    Mar 20, 2008
    Messages:
    145
    Likes Received:
    2
    Trophy Points:
    18
    Thank you Dave, that's very helpful.
     
  4. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,678
    Likes Received:
    654
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
    I am happy to hear the advice you were provided was helpful. I am marking this thread as [Resolved].

    Thank you.
     
Loading...

Share This Page