[ java ] random value

Random Distibution

Concept : Sum of (V[0] to V[i-1]) <= N <= Sum of (V[0] to V[i])

V : List of values

N : random number

when V[i] is returned


Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive)

int randomValue = java.util.Random.nextInt(int sumWeight)/sourcecode]



for (T rand : rands)
runningTotal += rand.weight;

if (randomValue < runningTotal)
return rand;


shell script

run multiple command at once : with &&

mkdir testfolder.zip &amp;amp;amp;amp;amp;amp;&amp;amp;amp;amp;amp;amp; touch test.zip

NOTE : if the first command fail, the rest will not be executed

touch with last modified date

2015/09/01 at 00:00

touch -t 201509010000 test_old.swf

rm filename is case sensitive

test file will not be deleted when run

rm -f Test

Check file exists

if [ -e &amp;amp;quot;test&amp;amp;quot; ]
echo &amp;amp;quot;file exists&amp;amp;quot;
echo &amp;amp;quot;file does not exists&amp;amp;quot;

How to check file exists on the remote ssh



[ ubuntu ] Jenkin


wget -q -O - http://pkg.jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add -

add the following entry in your /etc/apt/sources.list

deb http://pkg.jenkins-ci.org/debian binary/
sudo apt-get update
sudo apt-get install jenkins

older version of jenkins

apt-get install jenkins=1.532-1.1

or get deb from http://pkg.jenkins-ci.org/debian-stable/


  • Deamon is at /etc/init.d/jenkins
  • ‘jenkins’ user is created to run this service
  • log file is at /var/log/jenkins/jenkins.log
  • configuration parameters for the launch e.g JENKINS_HOME is at /etc/default/jenkins

By default, Jenkins listen on port 8080. However, this can be changed.




File Parameter


Need “File location”, that is the file name that will be placed when upload.

Otherwise, we will get “The parameter is incorrect. FATAL: command execution failed”


Create new shell script job

New Job > Build a free-style software project

Build > Execute shell


Shell executable on windows Setup

Go to Manage Jenkins > Configure System

Shell executable : C:\Program Files\Git\bin\sh.exe


May need full path when execute windows program



Otherwise, need Setting  Jenkins path on windows

Manage Jenkins > Configure System


ref : http://stackoverflow.com/questions/36473479/how-to-set-the-path-environment-variable-in-jenkins-configuration-on-windows


Execute Git Command on Shell

Example : git pull, git checkout



Build > Build Now

NOTE : this will call http://YOURHOST:8080/job/PROJECTNAME/build


Console output

Build history > console output


NOTE : built success : build history bullet is blue

built failure : build history bullet is red


Multiple steps can be added



Activate ant script

Where is Jenkins Workspaces ?

Manage Jenkins > Configure System > click `Advance` button

Home directory : /var/lib/jenkins

Workspace Root Directory : ${ITEM_ROOTDIR}/workspace

Build Record Root Directory : ${ITEM_ROOTDIR}/builds

The workspace path can be founded from the console error when we try to run the task

My workspace is `/var/lib/jenkins/jobs/[job name]/workspace/`

${WORKSPACE} variable path



Setup Slave 

setup node

NOTE : no need to install Jenkins on the slave machine

  1. Manage Jenkins > Manage Nodes

New node

suggestion : slave name should contains no space


2. Manage Jenkins > Manage Nodes

click at the jenkins slave


run the command on the client

java -jar slave.jar -jnlpUrl


Restrict where this project can be run

bash -c cd “/var/lib/jenkins-slave” && java -jar slave.jar


ref : https://wiki.jenkins-ci.org/display/JENKINS/Step+by+step+guide+to+set+up+master+and+slave+machines






Update Jenkins

I just purge and install from deb file from http://pkg.jenkins-ci.org/debian-stable/

NOTE : need to purge both jenkins and jenkins-common

Download jenkins.war from





java -jar jenkins.war

ref : https://wiki.jenkins-ci.org/display/JENKINS/Automated+Upgrade

If we would like to run as a deamon, replace file at


ref : https://wiki.jenkins-ci.org/display/JENKINS/Installing+Jenkins+as+a+Unix+daemon

, https://wiki.jenkins-ci.org/display/JENKINS/Installing+Jenkins+on+Ubuntu

, https://wiki.jenkins-ci.org/display/JENKINS/Building+a+software+project

ลอง JSHint บน gedit

sudo npm install jshint -g


Tools > Manage External Tools

Shortcut Key: ‘Alt+Super+J’


คือ มันไม่คิดจะเตือนอะไรนอกจาก semi colon หรอ

ref : http://jeffmcmahan.info/blog/how-to-use-jshint-with-gedit-on-linux/



$ rsync -avz --delete-after /home/pong/rsyncsource /home/pong/rsyncdest


a : Recursively transfer all files from the directory hone/pong/rsyncsource on the machine foo into the /home/pong/rsyncdest/rsyncsource directory

The files are transferred in “archive” mode, which ensures that symbolic links, devices, attributes, permissions, ownerships etc are preserved in the transfer.

v : verbose
z : compression will be used to reduce the size of data portions of the transfer.

–delete-after : delete the files that got deleted on rsyncsource on rsyncdest after transfer ( ref : http://askubuntu.com/questions/476041/how-do-i-make-rsync-delete-files-that-have-been-deleted-from-the-source-folder )

–rsh=”ssh …” : choose an alternative remote shell program to use for communication between the local and remote copies of rsync. Typically, rsync is configured to use ssh by default. However, if you would like to change an ssh encryption or change port ( ssh -p 2234 ), or change an identity file ( ssh -i /path/to/id_rsa ).

Rsync with last modified date

  • Last modified date is one of the factor that rsync use to decide to copy, if we use cp before rsync and we would like to preserve the last modified date, use the –preserve flag.

cp –preserve

Therefore, I use -c ( checksum ) instead.

NOTE : -c ( checksum ) won’t works with -a ( that alias contains -t that is checking last modified date.





default calalena access log format


%h %l %u %t &quot;%r&quot; %s %b

Example :

0:0:0:0:0:0:0:1 – – [03/Nov/2015:14:35:05 +0700] “GET /favicon.ico HTTP/1.1” 200 21630

%h Remote host name (or IP address if enableLookups for the connector is false)
%l Remote logical username from identd (always returns ‘-‘)
%u Remote user that was authenticated
%t Date and time, in Common Log Format format
%r First line of the request
%s HTTP status code of the response
%b Bytes sent, excluding HTTP headers, or ‘-‘ if no bytes were sent

ref : https://tomcat.apache.org/tomcat-7.0-doc/api/org/apache/catalina/valves/AccessLogValve.html