Debian : Setting environment variables for all users

An environment variable like JAVA_HOME can be set in different ways in  linux systems.

#1 . If we set environment variables in .bashrc file found in each users home directory, it will be accessible only for that user.

~/.bashrc   ——> specific to a particular user.

Setting environment variable for all users.
If the environment variable to be available for all users,

#2.   it can be set in /etc/environment

For instance let see how JAVA_HOME can be set,
Open this file in  vim or any editor of your choice, and add following line in it.
This sets JAVA_HOME for default open JDK. Please note that location is specified in quotes,

Then save the file and run the command, to have these these changes in effect immediately.
source /etc/environment
But this is not a recommended method.

#3. Setting via /etc/profile

vim /etc/profile (open the this file in vim )
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
export PATH=$JAVA_HOME/bin:$PATH
Then save the file and run following command.
source /etc/profile

#4. Setting via /etc/profile.d
This can be viewed as an extension to the above step. Having all application specific environment file in /etc/profile makes the maintenance a hassle. As a solution to this, variables will be specified as a separate module inside the directory /etc/profile.d/. These modules are shell scripts and should end with .sh extension, otherwise system is not going to recognize it. You can read further on this from here.

I think this would be clear, with the following example.
In order to set JAVA_HOME bythis approach, first create a file inside /etc/profile.d (The file should end with .sh extension ).

Then open this file using vim or any editor
vim /etc/profile.d/
Then add following lines ( This JAVA_HOME location could be different for others)

export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
export PATH=${JAVA_HOME}/bin:${PATH}

Then save the file and run the following command.
source /etc/profile

You can also consider reading this


Linux – Exclude Certain Files and Folders while Copying

When we copy a certain folder to a remote machine using scp, we might want to exclude some sub folders or files. The following command worked for me in those kind of situations.

scp !(bin) root@target-machine:/opt/your-folder-name/  

In this case, it copies everything except the bin folder from the current working directory to /opt/your-folder-name/ folder in target-machine

Access denied issue with mysqlimport with remote machine

On importing a csv file into a table with same structure that placed on different machine, with mysql import using mysql root user , it is common that we may end up with some access denied error messages.

mysqlimport -h yourhostname –port 3306 -u root -pyourpassword –fields-terminated-by=’;’ –local csv_test tweet.csv

For instance the above stated query which worked in some machines, may raise

mysqlimport: Error: 1045 Access denied for user ‘root’@’ipaddress-of-remote-host’ (using password: YES)

The fix to this issue is as follows :

  1. First check the my.cnf file ( inside /etc/mysql folder ) and look for the line :

bind-address =

If it not commented, comments it out, as :

 #bind-address =

This tells system that, this mysql is accessible from different hosts as well.

2. Next we need to grant permission to the root user.

For that login to the mysql prompt using mysql -u root -p. Then execute the following query,


3. Now restart mysql using the command:

service mysql restart

VirtualBox – Copy Paste Not working (Debian)

Even though you have set bidirectional for the option Shared Clipboard in settings ( under General –>Advanced ) some times it will not work. The following steps fixed this issue for me.

In the guest OS do the following :

  1. update source.list file
  • enable the contrib repositories; e.g. for Debian 8, make sure your /etc/apt/sources.list contains something like
    deb jessie main contrib

2.  Install guest addition software.

apt-get update
apt-get install virtualbox-guest-dkms

RabbitMQ – Status Checking

To see the status of rabbitmq

sudo rabbitmqctl status

To stop the rabbitmq

sudo rabbitmqctl stop

(Try the status command again to see that it’s stopped). To start it again, the recommended method is

sudo invoke-rc.d rabbitmq-server start

Still not getting it up ????

Try restarting your system. It is supposed to work now. 

Celery -Bound Tasks

A task being bound means the first argument to the task will always be the task instance (self).
Bound tasks are needed for retries (using app.Task.retry()), for accessing information about the current task request, and for any additional functionality you add to custom task base classes. Ref

An example to get the tasks ID of current tasks has been added below :

def get_ID(self):
print “hooo”

It is always a good practice to give names to tasks otherwise it will go for automatic  naming which in some situation may lead to tasks unregistered error.


Java – Batch Upload to Database

Usually I/O operations are costly. When I  tried to upload my huge csv file of 1.8 lakhs records one by one,  into a MySQL table , it took almost more than 30 minutes. And obviously it was not an acceptable result. So I had to resort  to batch uploading. On writing data as batches of 1000 records, everything was over in 30-40 seconds.

Lets see the code in detail.

The important thing to remember is that we need to turn off  “auto commit” mode. This means that if this mode is enable, every time record is pushed DB memory, it would automatically get written into tables, nullifying the effects of batch upload. At the same we have to enable it just before writing the records into table once enough number of records are pushed into DB memory or cache, using the commit().

In the the following example, records are read from a csv file named input.csv and its first three fields are written into tables called “batch” in the DB test. MySQL was DB of my choice.

At first auto commit mode is turned off by calling setAutoCommit(false) on DB connection object. Each record will be read and pushed them into DB cache using the addBatch(); 

When the we have 1000 records in the cache ie count variables becomes multiples of 1000, we need to write them into DB . For that we call following method, executeBatch(); Since we have disabled the auto commit mode, we need to enable that as well by calling commit()  on connection object in order to get this data written into the DB.

Gitub link here

public class BatchUpload {

public static void main(String[] args) throws IOException, SQLException {

String line = “”;
String delimiter = “,”;
int count = 1;
Connection dbConn = DBConnector.getDBConnection();
PreparedStatement ps = DBConnector.getPSInstance();
String inputFile = “input.csv”;
BufferedReader br = new BufferedReader(new FileReader(inputFile));
while ((line = br.readLine()) != null) {
String[] entities = line.split(delimiter);
try {


System.out.println(“Records are inserted into DBUSER table!”);

} catch (SQLException e) {




/*** To write the remaining records into DB*/



public class DBConnector {
private static final String DB_DRIVER = “com.mysql.jdbc.DRIVER”;
private static final String DB_CONNECTION = “jdbc:mysql://localhost:3306/test”;
private static final String DB_USER = “root”;
private static final String DB_PASSWORD = “root”;

private static Connection conn;
private static PreparedStatement ps;

public static void createConnection() {

// conn = null;

try {


} catch(ClassNotFoundException cnf){
System.out.println(“Driver could not be loaded: ” + cnf);

conn = DriverManager.getConnection(DB_CONNECTION, DB_USER,DB_PASSWORD);
String query = “INSERT INTO batch”
+ “(userID, username, address) VALUES”
+ “(?,?,?)”;

ps = conn.prepareStatement(query);
catch (SQLException e) {
// TODO Auto-generated catch block


public static Connection getDBConnection() {

return conn;


public static PreparedStatement getPSInstance() {
return ps;