Tag: bash

  • Restart Apache if failed

    This bash script check web site every 5 minutes, restart the web server if web site is not responding.

    Create a file

    mkdir /usr/serverok
    vi /usr/serverok/check_httpd.sh
    

    Add

    #!/bin/bash
    # Author: Yujin Boby
    # Web: https://serverok.in
    # Email: [email protected]
    
    /usr/bin/wget --tries=1 --timeout=30 -O /dev/null https://YOUR_DOMAIN.EXTN/
    
    if [ $? -ne 0 ]; then
        systemctl restart httpd
        datetime=`date "+%Y%m%d %H:%M:%S"`
        echo $datetime "failure">>/var/log/sok-web.log
    fi
    

    In above code, replace https://YOUR_DOMAIN.EXTN/ with your sites actual url.

    Make it executable

    chmod 755 /usr/serverok/check_httpd.sh
    

    Create a cronjob

    crontab -e
    

    Add

    */5 * * * *  /usr/serverok/check_httpd.sh > /dev/null 2>&1
    

    See Server Monitoring

  • Bash check if a program is running or not

    You can find process id for a running program with command

    pidof BINARY_NAME_HERE
    

    Example

    boby@sok-01:~$ pidof gedit
    22057
    boby@sok-01:~$ ps aux | grep gedit
    boby       22057  4.4  0.7 822760 60280 ?        Sl   14:29   0:00 /usr/bin/gedit --gapplication-service
    boby       22071  0.0  0.0   8904   644 pts/0    S+   14:29   0:00 grep --color=auto gedit
    boby@sok-01:~$ 
    

    We can use pidof command in bash script to find if a program is running or not

    if [ "$(pidof chrome)" ]
    then
    	echo "chrome already running."
    fi
    

    See bash

  • bash check if a folder is empty or not

    To check if a folder is empty of not, you can use

    if [ "$(ls -A /home/boby/1/ )" ]
    then
        echo "Files found"
    else
        echo 'No files found'
    fi
    

    If you want to reverse the checking, you can use !

    if [ ! "$(ls -A /home/boby/1/ )" ]
    then
        echo "No files found"
    else
        echo 'Files found.'
    fi
    

    See bash

  • Bash check if a directory exists

    To check if a directory exists in bash, use

    if [[ -d "/path/to/directory/" ]]
    then
        echo "Directory found."
    fi
    

    To check for absense of a directory, use !

    if [[ ! -d "/path/to/directory/" ]]
    then
        echo "Directory not found."
    fi
    

    See bash

  • Bash Script to Monitor Disk Usage

    This script will check disk usage on / partition and email you if disk usage is above 80%

    We can use df / command to find current disk usage

    df /
    

    As you can see, the result have 2 lines. We don’t need first line. To ignore first line, we can use

    df / | grep -v 'Filesystem'
    

    From the result, we only need the Use% part. In this case 66%, to find this, you can use awk command, that split the line, then prient specified part. In our case, we need 5th part.

    df / | grep -v 'Filesystem' | awk '{print $5}'
    

    To use disk usage % in our script for calculation, we need it converted to number. That is remove %. This can be done with sed command

    df / | grep -v 'Filesystem' | awk '{print $5}' | sed 's/%//g'
    

    Now we have disk usage as a number. We can use it in our script.

    vi disk-usage-checker
    

    Add following content to it

    #!/bin/bash
    
    CURRENT_USAGE=$(df / | grep -v 'Filesystem' | awk '{print $5}' | sed 's/%//g')
    ALERT_ON=80
    
    if [ "$CURRENT_USAGE" -gt "$ALERT_ON" ] ; then
        mail -s 'Disk Usage Warning' YOUR_EMAIL_HERE << EOF
    Disk almost full on / partition. Current Useage: $CURRENT_USAGE%
    EOF
    fi
    

    In above script, replace YOUR_EMAIL_HERE with your email address.

    disk usage bash script

    You can run the script daily using cronjob. If disk usage ever go above 80%, you will get email alert.

    See bash

  • Bash if statement

    Check if first augment to script is –help

    if [ "$1" = "--help" ]; then
        echo "$0 --help = shows help"
        exit 0
    fi
    

    Check if a file exists and is executable

    if [ -f "/usr/bin/curl" ]; then
        echo "curl exists"
    fi
    

    Check if a binary file exists and is executable

    if [ -x "/usr/bin/curl" ]; then
        echo "curl exists and executable"
    fi
    

    Check if previous command was sucessfull or not

    if [ "$?" != 0 ]; then
        echo failed
        exit 1
    fi
    

    Check if variabe set or not

    if [ -z ${USER} ]; then
        echo "\$USER not set"
    else
        echo "\$USER =  $USER"
    fi
    
  • bash

    Bash shortcuts

    CTRL + U = Delete everything
    CTRL + W = Delete a word
    CTRL + A = Go to Line starting
    CTRL + E = Go to Line end 
    CTRL + LEFT/RIGHT ARROW = move cursor by one word
    

    Enable color prompt

    Edit .bashrc, add following

    export PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '