Skip to main content

Jekyll with environment variable and multiple _config.yml files

· 2 min read
Sandeep Bhardwaj

When we write a new post in jekyll we usually made changes in _config.yml for our local/development environment like :-

Development environment

#url: http://sandeepbhardwaj.github.io
url: http://localhost:4000

google_analytics: #UA-68409070-1
disqus_user: #sandeepbhardwaj

Production environment

url: http://sandeepbhardwaj.github.io
#url: http://localhost:4000

google_analytics: UA-68409070-1
disqus_user: sandeepbhardwaj

But doing this every time is pain. Sometimes we forgot to revert the changes when push our changes to github. So i Google it and find out that there is jekyll.environment variable that we can use.

<% if jekyll.environment == "production" %>
<% include disqus.html %>
<% endif %>

so it means i have to made changes in _layout and _include file. I personally don't like this approach because i dont want to change my existing code.

Below are the two approaches which i liked most.

Approach 1

Creating environment specific _config.yml files

Finally, i find out a best way of doing this and just copy paste the existing _config.yml and rename it to _config-dev.yml and made changes for local environment.

Jekyll provide a best way of providing the config file explicitly using the command line like:-

jekyll serve -w --config _config-dev.yml

Approach 2

Overridding the default values

There is also a alternative approach for overriding the exiting properties with default one if you do not want to add unnecessary config in new _config-dev.yml

jekyll serve -w --config _config.yml,_config-dev.yml

Note :-Make sure in that case just add those config those need to be override only. Example:-

url: http://localhost:4000

google_analytics: #UA-68409070-1
disqus_user: #sandeepbhardwaj

and when i push my site to github it automatically pick the default config file _config.yml and build the site using default one.

Enable minimize button in Google chrome on Elementary OS

· One min read
Sandeep Bhardwaj

By default Google chrome do not have minimize button on Elementary OS and we can't enable it by using Elementary tweak tool. But we can make it enable by modifying the gConfigurations. Just type the below line in terminal and its done.

gconftool-2 --set /apps/metacity/general/button_layout --type string ":minimize:maximize:close"

Renaming files using terminal on ubuntu

· One min read
Sandeep Bhardwaj

Suppose we have lots of lots file in a directory and we need to rename of modify the extension of all the file, then renaming one by one is pain and not a good idea as well.

If we want to rename all the files in directory in one shot then we can use below command.

Example:- Renaming all file with extension html to md.

find -L . -type f -name "*.html" -print0 | while IFS= read -r -d '' File_Name; do
mv -- "$File_Name" "${File_Name%.html}.md"
done

Starting and Stopping the MySQL Server

· One min read

By Default MySQL server is started automatically after installation. Wd can check/verify the status of the MySQL server with the following command:

sudo service mysql status

Stop the MySQL server with the following command:

sudo service mysql stop

To restart the MySQL server, use the following command:

sudo service mysql start

Set Java Home on Ubuntu

· One min read

Setting the environment variable in Ubuntu in really easy, just need to edit the /etc/profile. Just add your java installation directory as below.

Open the /etc/profile file in your favorite editor.

sudo gedit /etc/profile

Add following lines in end

JAVA_HOME=/usr/local/java/jdk1.8.0_60
PATH=$PATH:$JAVA_HOME/bin
export JAVA_HOME
export JRE_HOME
export PATH

Note: For Installing Java on Ubuntu follow the

Install Oracle JDK on Ubuntu

· 2 min read
  1. Download the jdk-8u60-linux-x64.tar.gz from the Oracle site (Download 64bit or 32bit based on your linux environment).

  2. If you have root access then install java system-wide inside /usr/local directory.

First create a directory named java inside /usr/local

sandeep@vivan:~$ sudo mkdir /usr/local/java/

after that change directory

sandeep@vivan:~$ cd /usr/local/java/

Note: If you do not have access to root then install java inside your home directory.

  1. Unpack the tarball and install Java
sandeep@vivan:/usr/local/java$ sudo tar zxvf ~download/jdk-8u60-linux-x64.tar.gz

The Java files are installed in a directory /usr/local/java/jdk1.8.0_60/

  1. Install the new Java source in system:
sudo update-alternatives --install /usr/bin/javac javac /usr/local/java/jdk1.8.0_60/bin/javac 1
sudo update-alternatives --install /usr/bin/java java /usr/local/java/jdk1.8.0_60/bin/java 1
sudo update-alternatives --install /usr/bin/javaws javaws /usr/local/java/jdk1.8.0_60/bin/javaws 1
  1. Choose default Java:
sudo update-alternatives --config javac
sudo update-alternatives --config java
sudo update-alternatives --config javaws
  1. Check the Java version:
java -version
Output
java version "1.8.0_60"
Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)
  1. Verify the symlinks all point to the new Java location:
ls -la /etc/alternatives/java*
Output
lrwxrwxrwx 1 root root 36 Sep 24 19:27 /etc/alternatives/java -> /usr/local/java/jdk1.8.0_60/bin/java
lrwxrwxrwx 1 root root 37 Sep 24 15:00 /etc/alternatives/javac -> /usr/local/java/jdk1.8.0_60/bin/javac
lrwxrwxrwx 1 root root 38 Sep 24 15:06 /etc/alternatives/javaws -> /usr/local/java/jdk1.8.0_60/bin/javaws

Note: For Setting Java HOME on Ubuntu follow the

Enable laptop touch-pad on Elementary OS Freya

· One min read

Yesterday, i installed fresh copy of Elementary OS Freya on my Samsung laptop. But after installing i faced a weired problem that my touch-pad of laptop not working but every thing just working fine with external mouse.

So, i search on Google and find out so many solution but only the below one works for me.


sudo add-apt-repository ppa:hanipouspilot/focaltech-dkms
sudo apt-get update
sudo apt-get install focaltech-dkms
sudo modprobe -r psmouse
sudo modprobe psmouse

I hope this will work for you as well if you are also facing the same issue.

To make it permanent create a file with below command you can use your favorite editor.

sudo gedit /etc/modprobe.d/psmouse.conf

add this line to file options psmouse proto=imps and save the changes.

Execute N number of threads one after another in cyclic way

· 2 min read

This is a famous interview question how to execute 3 or n threads one after another and performing a job like :-

Thread T1 prints then Threads T2 print and then .... Thread Tn
then again Thread T1 prints then Threads T2 print and then .... Thread Tn and so on....

T1 -> T2 -> ... -> Tn; and then again T1 -> T2 -> ... -> Tn


CyclicExecutionOfThreads.java

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CyclicExecutionOfThreads
{

public static void main(String args[])
{

int totalNumOfThreads = 10;
PrintJob printJob = new PrintJob(totalNumOfThreads);

/*
* MyRunnable runnable = new MyRunnable(printJob, 1); Thread t1 = new
* Thread(runnable);
*
* MyRunnable runnable2 = new MyRunnable(printJob, 2); Thread t2 = new
* Thread(runnable2);
*
* MyRunnable runnable3 = new MyRunnable(printJob, 3); Thread t3 = new
* Thread(runnable3);
*
* t1.start(); t2.start(); t3.start();
*/

// OR
ExecutorService executorService = Executors.newFixedThreadPool(totalNumOfThreads);
Set<Runnable> runnables = new HashSet<Runnable>();

for (int i = 1; i <= totalNumOfThreads; i++)
{
MyRunnable command = new MyRunnable(printJob, i);
runnables.add(command);
executorService.execute(command);
}

executorService.shutdown();
}
}

class MyRunnable implements Runnable
{

PrintJob printJob;
int threadNum;

public MyRunnable(PrintJob job, int threadNum)
{
this.printJob = job;
this.threadNum = threadNum;
}

@Override
public void run()
{
while (true)
{
synchronized (printJob)
{
if (threadNum == printJob.counter)
{
printJob.printStuff();

if (printJob.counter != printJob.totalNumOfThreads)
{
printJob.counter++;
} else
{

System.out.println();
// reset the counter
printJob.resetCounter();
}

printJob.notifyAll();

} else
{
try
{
printJob.wait();
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}

}
}
}

class PrintJob
{
int counter = 1;
int totalNumOfThreads;

PrintJob(int totalNumOfThreads)
{
this.totalNumOfThreads = totalNumOfThreads;
}

public void printStuff()
{
System.out.println("Thread " + Thread.currentThread().getName() + " is printing");

try
{
Thread.sleep(1000);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}

public void resetCounter()
{
this.counter = 1;
}
}
Output
Thread pool-1-thread-1 is printing
Thread pool-1-thread-2 is printing
Thread pool-1-thread-3 is printing

Thread pool-1-thread-1 is printing
Thread pool-1-thread-2 is printing
Thread pool-1-thread-3 is printing

Thread pool-1-thread-1 is printing
Thread pool-1-thread-2 is printing
Thread pool-1-thread-3 is printing

Thread pool-1-thread-1 is printing
Thread pool-1-thread-2 is printing
Thread pool-1-thread-3 is printing

DelayQueueExample Example

· 2 min read
DelayQueueExample.java
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

public class DelayQueueExample
{

public static void main(String[] args)
{

BlockingQueue<DelayedElement> blockingQueue = new DelayQueue<DelayedElement>();

try
{
blockingQueue.put(new DelayedElement(4000, "Message with delay 4s"));
blockingQueue.put(new DelayedElement(2000, "Message with delay 2s"));
blockingQueue.put(new DelayedElement(9000, "Message with delay 9s"));
} catch (InterruptedException ie)
{
}

while (!blockingQueue.isEmpty())
{
try
{
System.out.println(">>" + blockingQueue.take());
} catch (InterruptedException ie)
{
}
}
}
}

class DelayedElement implements Delayed
{

private long duration = 0;
private String message;

public DelayedElement(long duration, String name)
{
this.duration = System.currentTimeMillis() + duration;
this.message = name;
}

@Override
public int compareTo(Delayed o)
{

return (int) (this.duration - ((DelayedElement) o).getDuration());
}

/*
* Expiration occurs when an element's getDelay(TimeUnit unit) method
* returns a value less than or equal to zero.
*/
@Override
public long getDelay(TimeUnit unit)
{
long diff = duration - System.currentTimeMillis();
return unit.convert(diff, TimeUnit.MILLISECONDS);
}

public long getDuration()
{
return duration;
}

public void setDuration(long duration)
{
this.duration = duration;
}

public String getMessage()
{
return message;
}

public void setMessage(String message)
{
this.message = message;
}

@Override
public String toString()
{
return "DelayedElement [duration=" + duration + ", message=" + message + "]";
}
}
Output
>>DelayedElement [duration=1436431881725, message=Message with delay 2s]
>>DelayedElement [duration=1436431883725, message=Message with delay 4s]
>>DelayedElement [duration=1436431888725, message=Message with delay 9s]

ExecutorService InvokeAll Example

· One min read
ExecutorInvokeAllExample.java
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class ExecutorInvokeAllExample
{

public static void main(String[] args) throws InterruptedException, ExecutionException
{
ExecutorService executorService = Executors.newSingleThreadExecutor();

Set<Callable<String>> callables = new HashSet<Callable<String>>();

callables.add(new CallableTask("Hello 1"));
callables.add(new CallableTask("Hello 2"));
callables.add(new CallableTask("Hello 3"));

List<Future<String>> futures = executorService.invokeAll(callables);

for (Future<String> future : futures)
{
System.out.println("future.get = " + future.get());
}
executorService.shutdown();
}
}

class CallableTask implements Callable<String>
{

String message;

public CallableTask(String message)
{
this.message = message;
}

@Override
public String call() throws Exception
{
return message;
}
}