Category: Operating System

  • Displaying Linux Distro Version and Showing 32Bit or 64Bit OS

    Checking whether you have 32bit linux or 64bit linux. From the code below we can see that it is a 64 bit linux.


    [user1@ubuntu 17:14:37 ~/dev]$ uname -mrs
    Linux 3.2.0-30-generic x86_64

    Checking which distro you’re using:

    [user1@ubuntu 17:14:48 ~/dev]$ lsb_release -a
    No LSB modules are available.
    Distributor ID: Ubuntu
    Description: Ubuntu 12.04.1 LTS
    Release: 12.04
    Codename: precise

  • Find and Replace File Content using Linux Command Line


    find . -name "*.pgsql" -print | xargs sed -i 's/STRINGTOREPLACE/REPLACERSTRING/g'

  • How To List All Users or Groups in Linux

    To list all users in Linux:

    cat /etc/passwd

    See only the first word before double colon (:)
    for exampple, user “root” will look like: root:x:0:0:root:/root:/bin/bash

    And to list all groups do the following

    cat /etc/group

  • Preventing Another Instance of A Program Running in Linux

    The code below is my solution for preventing a program from running more than once at the same time in Linux.
    The scenario: Consider if you have a cron job (jobA.sh) set to run at 6PM, 7PM, and 8PM.
    While the 1st instance of jobA.sh started, it might (or might not) need more than an hour to finish. If it does requires more than one hour, while the 1st instance is still running, a 2nd instance of jobA.sh started.
    Now you ended up with two instances of a same program consuming your CPU, files, etc – which will slow things down even more.

    So here how I solve it:
    When the 1st instance is started, check if there is a temporary lock file, if yes then just exits or quit. If not create the temporary lock file. And do your code/algorithm. After it finishes delete this lock file. When the 2nd instance started it will also do the same thing, checking if there is lock file.
    It will be wise to put another cron job that cleans all lock file every 00:00AM. Just to be sure the next execution (at 6PM tomorrow) will be able to run

    Since I’m no bash expert, let me know if there is a better way to do this. Cheers!


    #!/bin/bash
    # =================================
    # author: Ronald Pringadi
    # sleep command is in seconds
    # =================================
    vLockFile=/tmp/lockfile1.lock
    echo 'start'
    # If lockfile does not exist do the followings
    if [ ! -f $vLockFile ]; then
    touch $vLockFile
    echo "Lock file doesn't exists, executing code"
    sleep 7 # this can be your big process that takes longer than an hour, but just for proof of concept, I use 7 seconds
    rm $vLockFile
    else
    echo "Lock file exists do nothing"
    fi
    echo 'done'

  • MySQL Backup and Restore – Using Command Line

    Here is how you backup database using command line. The first line is to backup and the second line is to restore.
    These commands work on both Windows and Linux. In Windows you might want to setup mysql bin directory the environment PATH, you can do this easily by pressing Window + Break > Advanced System Settings > Environment > PATH. Then run the cmd or the command prompt. If you already open a command prompt before you setup the PATH, you need to restart your command prompt

    MySql doesn’t export stored procedure/function by default. It also doesn’t export triggers. If you need those, the you’ll need to add “–routines –triggers” to your command. Even if you don’t use stored procedure, function, or triggers it is safer if you issue your backup with those option enabled.


    $ mysqldump -uusername -ppassword --routines --triggers --lock-tables=false database_name > YOURSQLFILE.sql
    $ mysql -uusername -ppassword database_name < YOURSQLFILE.sql

    If you need to backup all database. The user that we usually use to backup all database is "root". This will backup all triggers, and functions. You will need to pre-create other users that has certain privilege over stored procedure or functions.

    mysqldump -uusername -ppassword --opt --events --all-databases > alldb.sql
    mysql -uusername -ppassword < alldb.sql

  • Delete All Symlink in The Current Directory

    find . -maxdepth 1 -type l -exec rm -f {} \;

  • Benchmark How Long a Program Runs In Linux Using Bash

    The following bash code might come handy for those of you who want to benchmark how long does a program runs.
    In the following example the assumption is that you want to pass along two parameters for your program to run properly.
    A time will be recorded at the beginning and at the end of the execution. And a time diff, the processing time, will be reported in seconds.


    #!/bin/bash
    ##################################################
    # Benchmark the processing time when a program
    # executes with two required parameters: parameter1 parameter2
    ##################################################

    if [[ $1 = "" || $2 = "" ]]; then
    echo "Usage: `basename $0` parameter1 parameter2"
    else
    _start_time=`date +%s`
    _parameter1=$1
    _parameter2=$2
    ### YOUR COMMAND HERE WITH parameter1 AND parameter2 ###
    _end_time=`date +%s`
    _processing_time=$((_end_time-_start_time))
    echo "Source File: $_parameter1"
    echo "Destination File: $_parameter2"
    echo "Start time: $_start_time"
    echo "End time: $_end_time"
    echo "Processing time is: $_processing_time"
    fi

  • Auto Login Using SSH Public and Private Keys

    Assume that your main server (the one you use the most) is ServerA.
    And from ServerA, you want to automatically login to ServerB.
    For illustration purpose, ServerB can be a repo server and you don’t want to keep being asked for password everytime you want to commit files.

    1. At ServerA issue: # ssh-keygen -t rsa
    This will create a public + ptivate key for ServerA
    2. Enter (empty) for paraphrase
    Your identification has been saved in /home/ronald/.ssh/id_rsa.
    Your public key has been saved in /home/ronald/.ssh/id_rsa.pub.
    3. Copy over the newly created public key from ServerA to ServerB
    scp .ssh/id_rsa.pub ronald@ServerB:~/id_rsa.pub.ServerA
    Note that once the file arrives at ServerB, it named as id_rsa.pub.ServerA (not id_rsa.pub), and it will be stored at ronald’s home directory at ServerB.

    4. Ssh to ServerB, and issue this command:
    cat id_rsa.pub.ServerA >> .ssh/authorized_keys
    ServerA public key will be merged/appended to ServerB authorized_keys file.

    This way everytime you want to access ServerB from ServerA, you won’t be asked for a password.

  • Linux Soft Symlink Folder or Directory


    $ ln -s /home/rpringad/somefolder /home/rpringad/newfolder

    #or if your current directory is already /home/rpringad:
    $ ln -s /home/rpringad/somefolder /home/rpringad/newfolder

    $ ln -s existingSourceFolder newLinkedFolder

  • How To Know Which Linux Distribution You Are Using?

    There are three commands that you can use, pick one of them:

    cat /etc/issue

    cat /proc/version

    dmesg | head -1