Month: July 2017

  • Detect sim card availability in the Android dual sim device

    Detect sim card availability in the Android dual sim device

    Below you can find code which help you find if SIM card available in the device.
    There two solutions. One for Android API less than 22 and for Android API greater than or equal to 22.

    Problem that dualsim support added in API-22. And we should deal with devices which support dual sim and has API less than 22.

    So solution for API less than 22.

    public boolean isSimAvailable(Context context){
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (telephonyManager.getSimSerialNumber() != null){
            return true;
        }
        return false;
    }

    You can ask why I’m not checking for phone number. I do this because the phone number is not 100% available. Sim card serial number always available.

    Second solution for API greater than or equal to 22

    public boolean isSimAvailable(Context context){
        SubscriptionManager sManager = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
        SubscriptionInfo infoSim1 = sManager.getActiveSubscriptionInfoForSimSlotIndex(0);
        SubscriptionInfo infoSim2 = sManager.getActiveSubscriptionInfoForSimSlotIndex(1);
        if(infoSim1 != null || infoSim2 != null) {
             return true;
        }
        return false;
    }

    And here is universal solution:

    public boolean isSimAvailable(Context context){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
            SubscriptionManager sManager = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
            SubscriptionInfo infoSim1 = sManager.getActiveSubscriptionInfoForSimSlotIndex(0);
            SubscriptionInfo infoSim2 = sManager.getActiveSubscriptionInfoForSimSlotIndex(1);
            if(infoSim1 != null || infoSim2 != null) {
                 return true;
            }
        }else{
            TelephonyManager telephonyManager = (TelephonyManager) App.getInstance().getSystemService(Context.TELEPHONY_SERVICE);
            if (telephonyManager.getSimSerialNumber() != null){
                return true;
            }
        }
        return false;
    }

    Let me know your solution in comments.

  • Letsencrypt automatic certificate renew

    Letsencrypt automatic certificate renew

    To make automatic renew you Letsencrypt certificates you need create crontab job.

    Here is simple instructions

    1. Create /root/certrenew.sh file with content:

    #!/bin/bash
    PATH=/usr/sbin:/usr/bin:/sbin:/bin
    sudo service nginx stop
    /home/igor/letsencrypt/letsencrypt-auto renew
    service nginx start
    exit 0

    2. Make it executable `chmod +x /root/certrenew.sh`
    3. `sudo crontab -e`
    4. Add crontab job. Job for running every day at 3:15AM will be next:

    15 3 * * * /root/certrenew.sh >> /var/log/le-renew.log

    /var/log/le-renew.log – log file

    Let me know better approach in comments.

  • How to automatically restart MySQL service

    How to automatically restart MySQL service

    Here is instruction how to launch MySQL server after close unexpectedly.

    1. Create /root/mysqlfix.sh file with next content:

    #!/bin/bash
    PATH=/usr/sbin:/usr/bin:/sbin:/bin
    if [[ ! "$(/usr/sbin/service mysql status)" =~ "active (running)" ]]
    then
        echo $(date -u) "MySQL service on the server has been stopped. It has now been restarted."
        sudo service mysql start
    fi

    2. Make it executable `chmod +x /root/mysqlfix.sh`
    3. `sudo crontab -e`
    4. Add crontab job. For 3 min period job will be next:

    */3 * * * * /root/mysqlfix.sh >> /var/log/mysqlfix.log 2>&1

    /var/log/mysqlfix.log – log file

    Let me know better approach in comments.

  • How to automatically restart Nginx

    How to automatically restart Nginx

    Here is instruction how to launch Nginx server after close unexpectedly.

    1. Create /root/nginxfix.sh file with next content:

    #!/bin/bash
    PATH=/usr/sbin:/usr/bin:/sbin:/bin
    if [[ ! "$(/etc/init.d/nginx status)" =~ "active (running)" ]]
    then
        echo $(date -u) "NGINX server has been stopped. It has now been restarted."
        service nginx start
    fi

    2. Make it executable chmod +x /root/nginxfix.sh
    3. sudo crontab -e
    4. Add crontab job. For 3 min period job will be next:

    */3 * * * * /root/nginxfix.sh >> /var/log/nginxfix.log 2>&1

    /var/log/nginxfix.log – log file

    Let me know better approach in comments.

  • How to fix Attempting to renew cert from /etc/letsencrypt/renewal/example.com.conf produced an unexpected error: Problem binding to port 443: Could not bind to IPv4 or IPv6.. Skipping.

    How to fix Attempting to renew cert from /etc/letsencrypt/renewal/example.com.conf produced an unexpected error: Problem binding to port 443: Could not bind to IPv4 or IPv6.. Skipping.

    Issue:

    Attempting to renew cert from /etc/letsencrypt/renewal/example.com.conf produced an unexpected error: Problem binding to port 443: Could not bind to IPv4 or IPv6.. Skipping.

    How to fix:

    Just stop webserver before renew letsencrypt.

    sudo service nginx stop
    cd letsencrypt
    sudo ./letsencrypt-auto renew
    sudo service nginx start