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!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/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'
Posted in Linux, Operating System | Leave a comment

Extjs – Ext.apply

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
console.clear();
var a = {
    abc : 1,
    def : 2
};
var b = {
    abc : 1.1,
    def : 2.2
};

var c = Ext.apply( a , b ); //Ext.apply overwrites 1st variable (by ref) with the 2nd var

console.log('c');
console.log(c);

console.log('a');
console.log(a);

console.log('b');
console.log(b);

Result:
c
Object { abc=1.1, def=2.2}
a
Object { abc=1.1, def=2.2}
b
Object { abc=1.1, def=2.2}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
var a = {
    abc : 1,
    def : 2
};
var b = {
    abc : 1.1,
    def : 2.2,
    gij : 3.3
};

var d = Ext.applyIf( a , b ); //Ext.applyIf overwrites 1st variable (by ref) with the 2nd var, only if it doesn't exists
console.log('d');
console.log(d);

console.log('a');
console.log(a);

console.log('b');
console.log(b);

Result:
d
Object { abc=1, def=2, gij=3.3}
a
Object { abc=1, def=2, gij=3.3}
b
Object { abc=1.1, def=2.2, gij=3.3}

Posted in Ext Js, javascript, Web Development | Leave a comment

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.

1
2
$ 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.

1
2
mysqldump -uusername -ppassword --opt --events --all-databases > alldb.sql
mysql -uusername -ppassword < alldb.sql
Posted in Database, Linux, MySQL | Leave a comment

Delete All Symlink in The Current Directory

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

Posted in Linux, Operating System | Leave a comment

Ad Blocker for Android Browser

If you’re anoyed with ads in your android browser, you should try Ad Block:
https://market.android.com/details?id=de.ub0r.android.adBlock&hl=en
With ad block your browser will make a request through a proxy server, which is the Ad Block itself. The Ad Block usually setup to use host 127.0.0.1 also known as the localhost, and port 8080.

In order to access the android default proxy settings you need a shortcut to the settings, which by default its hidden. So you’ll need Any Cut
https://market.android.com/details?id=com.appdroid.anycut&hl=en

The cool thing is no rooting required. This way you can browse faster, safer, and use less bandwidth.

Posted in Android | Leave a comment

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/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
Posted in Linux, Operating System, Ubuntu | Leave a comment

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.

Posted in Linux, Operating System, Ubuntu | Leave a comment

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

Posted in Linux, Operating System, Ubuntu | Leave a comment

How To Know Which Linux Distribution You Are Using?

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

1
2
3
4
5
cat /etc/issue

cat /proc/version

dmesg | head -1
Posted in Linux, Operating System, Ubuntu | Leave a comment

How to Set up an FTP Server in Ubuntu Linux

Login to your Linux shell menu as root

1
2
[root@locahost]# apt-get install vsftpd
[root@locahost]# vim /etc/vsftpd.conf

At VIM, 1st comment out anonymous_enable by adding a # sign at the beginning of the line
# anonymous_enable=YES
2nd remove comment at local enable by removing the # sign
local_enable=YES

1
[root@locahost]# /etc/init.d/vsftpd restart
Posted in Linux, Operating System, Ubuntu | Leave a comment