April 8, 2020

vim : arrow keys adds characters in editor

I had this problem sometimes on my application server, which can sometimes be old machines. When I tried to use arrow keys in insert mode in vim editor the following characters are being inserted in the editor:


  • for ↓ I get B
  • for ↑ I get A
  • for ← I get D
  • for → I get C
...and it's super annoying.

From my reading, this is how Vi behaves.

However, VIM is the successor to Vi. By default, it set to be in Vi-compatible mode, which includes this behavior for the arrow keys.

To remove the compatible mode, create a file named .vimrc in home directory add this line to the top of the file:

vim  ~/.vimrc
set nocompatible

Save the file and this should fix the problem.

fedora / Linux - Add new monitor resolution

Sometimes when you plug in your new monitor, the best fit resolution for it doesn't come out as one of the option in the Display Setting.

Use these commands to add new resolution to your monitor
gtf 1920 1024 60
xrandr --newmode "1920x1024_60.00"  163.83  1920 2040 2248 2576  1024 1025 1028 1060  -HSync +Vsync
xrandr --addmode DP-1 "1920x1024_60.00"

November 4, 2017

Stop relogin everytime pushing to github repo

Instead, lets permanently authenticating with Git repositories using username and password.
Run following command to enable credential caching:

$ git config credential.helper store

Afterwards, make some changes, commit and try with git push.

No more relogin.

October 20, 2017

Automatically back up redmine installation

# required settings
DB_USERNAME=''
DB_PASSWORD=''
DB_NAME=''
REDMINE_ROOT='' # e.g. /home/peter/rails/redmine.commanigy.com'
BACKUP_ROOT='' # e.g. /home/peter/backups (will be created)

# optional settings
GS_ROOT='gs://redmine-backup'
GS_FILENAME='backup_'`date +%Y%m%d`'.tar.gz'

echo 'Setting up directories'
mkdir $BACKUP_ROOT/redmine/db -p
mkdir $BACKUP_ROOT/redmine/files -p

echo 'Backing up database'
/usr/bin/mysqldump -u $DB_USERNAME --password=$DB_PASSWORD $DB_NAME | gzip > $BACKUP_ROOT/redmine/db/`date +%Y%m%d`.gz

echo 'Backing up attachments'
rsync -a $REDMINE_ROOT/files/ $BACKUP_ROOT/redmine/files/

echo 'Packing into single archive'
tar -czPf $GS_FILENAME $BACKUP_ROOT/redmine/

echo 'Creating bucket on Google Storage (if not already created)'
gsutil mb $GS_ROOT

echo 'Copying backup archive to Google Storage'
gsutil cp $GS_FILENAME $GS_ROOT

echo 'Cleanup by removing archive'
rm $GS_FILENAME

echo 'Backup complete'

October 17, 2017

Removing unread and not starred email in Gmail

Search using the following :

in:inbox AND is:unread AND -in:starred

September 30, 2017

SSH : Remote host identification has changed

So you have recently connected to a remote Server A via ssh.

The Server A's key is then saved into a file in your local PC, typically $HOME/.ssh/known_hosts

It may looks like this :

serverA ecdsa-sha2-nistp256 AAAAE2VjZHNhLXasdasdasdmlzdHAyNTYAAAAIbm
lzddddHAyNTYAAABBBNrIVAhYMcnUasdasdsdoMv7gtf8nMHghgYzVcdddkzbDM79C
81qswhYdd8L9VX+pNjg+asdasdasdasdasdasdxgooCk=

Imagine, suddenly, the remote Server A changed their key.
As a result, the key you have in your local PC, inside known_hosts becomes invalid.

The moment you try to ssh to Server A again, you will be prompted with :

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that the RSA host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
a7:a8:f2:97:94:33:58:b7:9d:bc:e0:a6:6b:f7:0a:29.
Please contact your system administrator.
Add correct host key in /home/ramesh/.ssh/known_hosts to get rid of this message.
Offending key in /home/ramesh/.ssh/known_hosts: 6
Permission denied (publickey,password).
The solution is to edit the file known_hosts and remove the existing key of Server A,
using your preferred text editor i.e. vi or vim or nano or notepad

When you try to ssh to Server A again, the new key of Server A will be saved into 
your local file known_hosts.

September 25, 2017

SSH SCP with Java

Good library to use is Java Secure Channel (JSch) from http://www.jcraft.com/jsch

Java doc : http://epaul.github.io/jsch-documentation/simple.javadoc/

Source code : https://github.com/ePaul/jsch-documentation

September 14, 2017

Undo bad commit

Sometimes when you did commit like below, you have forgoten where you are and accidentally committed every local changes under the current folder tree, including some that you don't want to. Urrgh!

Example : svn commit -m 'blabla'

For example, above commit results in revision 101.

You want to get back the repo to what was in 100.

You run :
1. Go to the same folder when you did the commit

2. Merge to the previous revision i.e. 100
svn merge -c -100 .

3. Check what changes this one will take place
svn stat

4. Commit
svn commit -m 'sorry Im so dumb before but I learned now'

Now you have the new revision i.e. 102 is now back to what was in 100

September 5, 2017

MySQL : Conditional If-else when selection returns empty result set

I am still very new to MySQL or SQL in general. So, this is kind of new.

I need to run a select statement and if that select statement return nothing, I would like to run another different select statement :

IF EXISTS (select something from TABLE_A) THEN                                         select something from TABLE_A;                                                 
ELSE
    select something from TABLE_B;
END IF;
More from StackOverflow :

IF NOT EXISTS (SELECT ...)
BEGIN
  INSERT ...
END
And with more control :

IF EXISTS (SELECT ...)
BEGIN
  PRINT 'Do nothing.';
END
ELSE
BEGIN
  INSERT ...
END

August 31, 2017

The APR based Apache Tomcat Native library was not found on the java.library.path

Getting this message when running JasperServer :

The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path

...and SO says :

Tomcat can use the Apache Portable Runtime to provide superior scalability, performance, and better integration with native server technologies. The Apache Portable Runtime is a highly portable library that is at the heart of Apache HTTP Server 2.x. APR has many uses, including access to advanced IO functionality (such as sendfile, epoll and OpenSSL), OS level functionality (random number generation, system status, etc), and native process handling (shared memory, NT pipes and Unix sockets).

The library is bundled into an OS specific dll (tcnative-1.dll) loaded via Java Native Interface (JNI). It allows tomcat to use OS functionalities not provided in the Java Runtime (such as sendfile, epoll, OpenSSL, system status, etc.). Tomcat will run just fine without it, but for some use cases, it will be faster with the native libraries.
If you really want it, download the tcnative-1.dll (or libtcnative.so for Linux) and put it in the bin folder, and add a system property to the launch configuration of the tomcat server in eclipse.
 -Djava.library.path=c:\dev\tomcat\bin

February 16, 2017

User's crontab

Per crontab man page:

Each user can have their own crontab, and though these are files in /var/spool/cron/crontabs, they are not intended to be edited directly.

Files under /var/spool are considered temporary/working and it's always a good practice to back up your cron entries or keep them in a file in your home directory.

I assume you're using crontab -e to create crontab files on the fly. If so, you can get a "copy" of your crontab file by doing crontab -l. Pipe that to a file to get a "backup":

crontab -l > my-crontab

Then you can edit that my-crontab file to add or modify entries, and then "install" it by giving it to crontab:

crontab my-crontab

This does the same syntax checking as crontab -e.

To list all cron jobs from all users in your system:

for user in $(cut -f1 -d: /etc/passwd)
do
  echo $user
  crontab -u $user -l
done

or

for user in $(cut -f1 -d: /etc/passwd)
do
  echo $user
  crontab -l $user 

done

February 15, 2017

Why am I still getting a password prompt with ssh with public key authentication

Following this tutorial : http://www.linuxproblem.org/art_9.html

Why am I still getting a password prompt with ssh with public key authentication?

Make sure the permissions on the ~/.ssh directory and its contents are proper. Create .ssh folder if it's not there yet. Same goes to authorized_keys file.

Your home directory ~, your ~/.ssh directory and the ~/.ssh/authorized_keys file on the remote machine must be writable only by you:
rwx------ and rwxr-xr-x are fine, but rwxrwx--- is no good.
700 or 755, not 775

If ~/.ssh or authorized_keys is a symbolic link, the canonical path (with symbolic links expanded) is checked.

Your ~/.ssh/authorized_keys file (on the remote machine) must be readable (at least 400) but you probably need it to be also writable (600) if you will add any more keys to it.

Your private key file (on the local machine) must be readable and writable only by you: rw-------, i.e. 600.

Also, if SELinux is set to enforcing, you may need to run restorecon -R -v ~/.ssh (see e.g. Ubuntu bug 965663 and Debian bug report #658675; this is patched in CentOS 6).

January 25, 2017

svn diff with meld

How to use svn diff with meld

1. First, download meld from here and set the path to /bin folder of meld to $PATH variables.

2. Create a script of below content with name meld-diff.sh

#!/bin/bash
meld "$6" "$7" 

exit 0

3. Copy the location, lets say its /home/you/meld-diff.sh

4. Edit subversion configuration file. Found in /home/you/.subversion/config
Search for "diff-cmd", uncomment it and set it to path of our script we created just now. Save it.

For example : diff-cmd = /home/you/meld-diff.sh

5. Now if you run "svn diff", it should redirect to meld automatically.

January 19, 2017

Restart, Start and Stop Tomcat using Bash

$CATALINA_PID is the Environment Variable that has path of the file which should contains the pid of the catalina/tomcat startup java process.

Typically, this will be under /bin folder in your Tomcat installation.

For example, you could have this in your ~/.bashrc
CATALINA_BASE="/opt/tomcat8"
export CATALINA_BASE
CATALINA_PID="$CATALINA_BASE/bin/catalina.pid"
export CATALINA_PID

By having this $CATALINA_PID environment variable set up, we can easily get the pid of the Tomcat process and control it as we want.

I created two scripts
(1) check-tomcat : To check if Tomcat is running, by reading the Tomcat PID file
(2) restart-tomcat : To kill the tomcat, clear the Tomcat's work folder and restart Tomcat


check-tomcat
============
#!/bin/bash
#title           : check-tomcat
#description     : This script will check if Tomcat is running and return the pid
#author : Raf
#note : Requires $CATALINA_BASE/bin/catalina.pid to be created and $CATALINA_PID to be set.

clear 

if [ -f "$CATALINA_PID" ]; then
read kpid < "$CATALINA_PID"
    
    if ps --pid $kpid 2>&1 1>/dev/null; then
        echo "Tomcat is already running at ${kpid}"
    else
        echo "$CATALINA_PID found, but Tomcat is not running"
    fi
fi


restart-tomcat
==============
#!/bin/bash
#title           : restart-tomcat
#description     : This script will first clear the work folder and then check if Tomcat is running, kill it, and restart the tomcat again
#author : Raf
#note : Requires $CATALINA_BASE/bin/catalina.pid to be created and $CATALINA_PID to be set.


echo "Clear Tomcat log...."
rm -fv /opt/apache-tomcat-8.0.39/logs/catalina.out 
touch /opt/apache-tomcat-8.0.39/logs/catalina.out

echo "Clear Tomcat work folder...."
rm -rfv /opt/apache-tomcat-8.0.39/work/Catalina/

if [ -f "$CATALINA_PID" ]; then
read kpid < "$CATALINA_PID"
    
if ps --pid $kpid 2>&1 1
/opt/apache-tomcat-8.0.39/bin/catalina.sh start;

read kpid < "$CATALINA_PID"
echo "Tomcat is started at ${kpid}"
else
    echo "Killing Tomcat...."
kill -kill $kpid

/opt/apache-tomcat-8.0.39/bin/catalina.sh start;

read kpid < "$CATALINA_PID"
echo "Tomcat is started at ${kpid}"
fi
fi

Intro to Tomcat's management in Linux

1. In Linux, Tomcat can be typically installed at a few possible location such as
/usr/share/tomcat8
/var/lib/tomcat8/
/opt/tomcat8

Note : At KWT server, Tomcat is installed at /var/lib/tomcat8/


2. Tomcat has a few Environment Variables

$CATALINA_HOME : where Tomcat is installed
$CATALINA_BASE : same as CATALINA_HOME
$CATALINA_PID : location of pid file for Tomcat running process (will discuss later)


3. To start Tomcat, usually we can run : "$CATALINA_HOME/bin/catalina.sh start"


4. Once Tomcat is running, the pid of the Tomcat process is writen into a file which is declared in $CATALINA_PID.
Typically value for $CATALINA_PID is $CATALINA_BASE\bin\catalina.pid

When you open this file, (run "less $CATALINA_BASE\bin\catalina.pid"), you will see the pid of the Tomcat process


5. You stop Tomcat process by running : "kill -kill 1239" (replace 1239 with the pid of the process)
This is a generic Linux's way of killing any process.


6. Typically, to stop Tomcat, you run "$CATALINA_HOME/bin/shutdown.sh start"


NOTE : To add more : Logging, folder structure, manager, host manager, multiple instance, users, server setting

January 18, 2017

Habits of highly productive people.

The best daily routines involve taking care of yourself physically, emotionally and mentally before the day starts.

Aristotle said, "We are what we do repeatedly, therefore, excellence is not an act, but a habit."

What are those habits?

The habits that affect productivity the most are directly related to the reasons you ordinarily complain about as justifications for being lazy or unproductive. "I'm tired, I'm cold, I can't stop thinking about my ex, I didn't sleep enough last night, I didn't eat enough, my stomach hurts, I just don't feel right today," etc.

The habits themselves are the habits related to your welfare, your physical, mental and emotional well-being. If physically you're feeling good and have the requisite energy for productivity, emotionally you're stable and able to withstand the stress of having a lot to do, and mentally you aren't bogged down by identifying with every passing thought, then you have set yourself up for maximum productivity.

The habits that ensure your physical, mental and emotional welfare are:

1. Get enough sleep

Being tired not only slows down your mental and physical capacity. The first step in eliminating that excuse is making sure you get enough sleep.

How to do it:

(a) Have a set, no negotiation lights-out time.

(b) Have a set time to put stop messing around with the t.v., your phone, computer, etc. Social media and t.v. can easily distract you and keep you up for an extra hour or two.

(c) Read before you sleep, not only do you grow through reading but it helps lull you just enough to get you ready for bed.


2. Wake up early

You don't have to immediately start waking up at 4:00 a.m but you can gradually start pushing back the time you wake up (like 15 minutes a week), and over time you can be a really early riser. The reason waking up early is so integral to success is that there are no distractions whatsoever in the morning time and you can 8-10 hours of work done in 4-5.

How to implement:

(a) Sleep Early

(b) Have the following items done before going to bed : All your bags packed and ready to go for the next day. The clothes you want to wear the next day should be decided. What you want to eat for breakfast.

(c) Have a reason to wake up - you need to know exactly what your plan is. An idea of the first few tasks you want to knock out in the morning or rather, what the first few hours of your morning is going to consist of.

(3) Exercise: Looking good and feeling more confident about your physical attraction is just one incidental benefit to exercising. Your body rewards you with happiness, LITERALLY, in the form of endorphins.


3. Eating healthy  

Looking good and feeling more confident about your physical attraction is just one incidental benefit to eating healthier. To operate at maximum productive capacity, you need to have maximum energy levels. Pay attention to how your body responds to the different foods you eat and you'll quickly see the importance of healthy eating and drinking water. Your body PAYS YOU with the currency of energy for treating it the right way!

How to implement:

(a) Cut junk food - chips, soda, fast-food, etc. Needs to just go and not be an option.

(b) Have bread and other carbs earlier during the day for energy, and try to stay awake from carbs after 3 p.m

(c) WATER, LOTS and LOTS of water - as much as you can have.

(d) Force yourself to eat vegetables and fruits

4. Meditating or pray 

All day long your mind is going at 100 miles an hour and we're not even aware of it. We don't think about our thoughts as events that require energy, but every thought you have requires a certain amount of energy, and the thoughts that actually stick and force you to actively think, DRAIN YOU. Meditation helps patch up the leaking of energy caused the neuroticism of the mind.

How to implement:

(a) Full body stretch for 2-3 minutes

(b) Sit comfortably in a chair, or cross-legged on a pillow on the floor. If you're on the floor try and sit on the edge of pillow so you're leaning forward towards your knees as opposed to back, well, on your lower back.

(c) Close your eyes, try to follow your breath.


5. Stay organized 

When we know we have a million things to do with no plan of attack, we get overwhelmed and we become negative about the fact that we have so much to do, so little time, not knowing where to start. You need to have a plan of attack that breaks your big goals into smaller steps, and be strict on yourself about completing small steps. Progress adds up and you feel accomplished with every step you take.

How to implement:

(a) Have a journal/piece of paper, something to plan on.

(b) Break down the big goals/assignments/projects that you're doing into 5-10 little steps that are necessary in the process.

(c) Plan those steps into feasible deadlines, should be flexible enough that you don't have to stress AT ALL about getting them done.

The software engineer most likely to be sought after

The software engineer most likely to be sought after is ;


  • Utterly reliable. They get things done, even if it means working hard. They notify ahead of time if they're going to miss a deliverable.
  • Thorough. Things they do only need to be done once, which is to say, it's unlikely anyone else could have done it better.
  • Productive. They get things done quickly and competently.
  • Innovative. They can think of new ways to solve problems old and new.
  • Adaptable. They learn new technologies easily, and can integrate it with their existing knowledge to leverage both for gain. Also known as 'fungible.'
  • Optimistic. When things are looking crappy, they're the ones who convince everyone that this too will pass, and probably in a good way.
  • Personable. People like working with them. Despite being good, they don't make other people feel bad.

It's not about your primary programming language or technology. It's not about being smarter than everyone else. It's not about being the world's best programmer. It's about being good, being reliable, and being someone who other people want to work with.

Managers want reliable people in bad times, and people who don't cause trouble. Peers want someone who buoys them and makes the team stronger, and helps their team mates and the company succeed.

If by some miracle a position is open, hiring managers want to hire people like this because they're good for business, they're good for team morale, and they're good for the company. And they find them by remembering who THEY like working with, or asking their team for recommendations of people they like working with

January 17, 2017

Intro to Java Server Pages (JSP)

What is JSP?

JavaServer Pages (JSP) is a technology for developing web pages by inserting java code in HTML pages using special JSP tags, most of which start with <% and end with %>.

JSP can be used as part of the front end (client side) as well as the back end (server side). That means:

  • JSP is written inside HTML file (as a client side code) to perform some logic 
  • JSP can be used just like Java Servlet (as a server side code) - create connection to database, perform query, perform validation logic and so on.

Why JSP? Why not just use Java Servlet?

JSP vs. Pure Servlets :

It is more convenient to write (and to modify!) regular HTML file than to have plenty of println statements in Java Servlets that generate the HTML response.

However, JSP pages can also be used in combination with Java Servlets that handle the business logic and this is usually the code. Java Servlet is usually required to perform more powerful business logic.


JSP Processing:
  • Like normal page, your browser sends an HTTP request to the web server.
  • The web server recognizes that the HTTP request is for a JSP page and forwards it to a JSP engine. This is done by using the file page which ends with .jsp instead of .html i.e. index.jsp instead of index.html
  • The JSP engine converts the JSP page into a Java Servlet. All JSP elements are converted to Java code inside this generated Java Servlet.
  • The generated Java Servlet is then compiled into an executable class.
  • Servlet container loads the servlet class and executes it.
  • During execution, the servlet produces an output in HTML format inside an HTTP Response.
  • The web server forwards the HTTP response (in HTML) to your browser.
  • Finally web browser generates the HTML page inside the HTTP response.
All the above mentioned steps can be shown below in the following diagram


So in a way, a JSP page is really just another way to write a Java Servlet without having to be a Java programming wizard. Except for the translation phase, a JSP page is handled exactly like a regular Java Servlet

Learn JSP Syntax. Proceed to https://www.tutorialspoint.com/jsp/jsp_syntax.htm

Try doing this simple project :
http://www.javaknowledge.info/login-and-registration-example-in-jsp-with-session/

SSH & Linux Services management


* Connect to remote server via ssh
ssh username@server-ip:directory-location-on-server
i.e. ssh ifa@144.83.29.255:/var/lib/tomcat8/webapps 

(or in Windows, use BitviseSSH client - graphical tool)

* List all service
service --status-all

* List specified service
service --status-all | grep tomcat

* Start a service
service {service-name} start 
i.e. service tomcat8 start   

* Stop a service
service {service-name} stop
i.e. service tomcat8 stop

* Check service status
service {service-name} status
i.e. service tomcat8 status

January 13, 2017

Designing the database. First, focus on User Experience.

Don't stress on the design of the database too much.

Sometimes this may be hard. With internal Line-Of-Business (LOB) applications, the prevailing view of the business is often times that the DATA is the primary asset, where as the software is somewhat expendable. That is, for businesses, data is everything.

In my opinion, this is wrong.

In reality the asset is the company's ability to INTERACT with data. To view it, to manipulate it, and to make decisions based on it.

This means that even though they may place a high value on the data, what they are actually valuing is the software that you are writing. This means I would focus most of your effort on building an effective user experience, rather than on "designing the perfect database". The database is really just a tool that enables you to deliver on a user experience.

The key feature of relational data models is data and access path independance. You can add columns, change keys, introduce or remove indexes, etc, while having zero impact (or close to zero) on the applications that use it.

This makes the database structure extremely pliable. Easy to change.

Trying to design the database to "be flexible for the future" or to "optimize performance" is mostly a wasted effort. Changing the structure of the database will have a relatively small impact on your system.

You also can't predict how the database will scale until you run into the scenarios where you need it to scale. Your best bet is to wait until you hit performance issues. and then address them specifically.

Making changes to the user experience of your app, however, is usually more expensive. UI work is time consuming, and usually takes a while to get right.

So, my recommendation is :

  • Just quickly produce a crappy DB design 
  • React to the actual performance scenarios you encounter 
  • Focus your efforts on user experience, not on the database