Linux « Intelligrape Groovy & Grails Blogs

Archive for the ‘ Linux ’ Category

GNOME GUI Integration w.r.t IDEA GUI Launcher

Posted by Kushal Likhi on February 13th, 2011

One of the important part of any Installer is integrating and defining launchers for GUI. This post will demonstrate adding entries in menu bar and panel, using scripts in context of creating a GUI launcher for IDEA IDE.

currently IDEA does not come with any GUI Launcher but this does not mean that we cant have one. How cool it would be if we had an icon on the top panel to launch IDEA in Terminal mode.

IDEA LAUNCHER IN PANEL ->


Similarly an entry is also created in menu: application->programming->Idea
Download: idea_desktop_Integration.tar.gz Check the installation guide below to see how to install

PROBLEM STATEMENT:
A program will be created to do IDEA GNOME GUI Integration, which launches IDEA in TERMINAL such that we can see whats happening in the background, that too with the ease of GUI one click quick launch. It requires following subtasks:

  1. Building Launch Environment for idea.sh: idea.sh is designed to launch in terminal with all PATH variables set for various SDK’s. if we try to launch it in a standalone terminal it will not work as environment is required. In this step we will generate the launch environment
  2. Adding Menu Bar Entry: in this we will add an entry to menu: application->programming->Idea using scripts, automated.
  3. Adding entry to panel: here we will add an entry for launcher in the quickLaunch panel on top of the screen.

Building Launch Environment
whenever terminal is launched it runs the .bashrc file which have the PATH variables to be set.
but we cant execute .bashrc in standalone terminals i.e terminals with no command point as GUI always launches applications in standalone terminals.
so the other option is:- extract all the PATH variables from .bashrc and create your own setup script for the terminal.
this can be achieved by simple commands:-

grep "export" ~/.bashrc |tee ~/.IDEALauncher

this will extract all exported variables and make a file containing all of them in the home folder with the name .IDEALauncher
and we can execute this script to set the path via command:

chmod 777 ~/.IDEALauncher
. ~/.IDEALauncher

Now it has set all the path variables needed for the launch.

the above mentioned task is stored in a file klaunch.sh and is the launcher start point. file is as follows:
Note: UI and echo’s have been removed here to make it more readable, in attached file lot other stuff is there.

# IDEA Launcher Script for gnome desktop integration.
# By Kushal Likhi

#GET settings of enviornment variables and create a shell script for those
grep "export" ~/.bashrc |tee ~/.IDEALauncher

#set enviornment variables
chmod 777 ~/.IDEALauncher
. ~/.IDEALauncher

#launch IDEA
`idea.sh`

Setup Process
adding menu bar entries and panel entries is a part of setup process and is as follows:

“Everything in LINUX is a file”

yes, and using the above saying all we have to do is: create/edit/add some files
FOR Menu Entry we have to create a file: /usr/share/applications/idea.desktop and update cache to be safe
the Script is as follows for the setup file:

#IDEA Desktop integration setup file
# (c) Kushal Likhi
#Ver 1.0

#clear screen for good clean looks
clear

#welcome message
echo "-------------------WELCOME----------------------"
echo ""
echo " SETUP Program for IDEA GNOME Integration"
echo " by: kushal likhi"
echo ""
echo "------------------------------------------------"

#read IDEA installation dir path
echo ""
read -p "Enter IDEA Bin Location, press enter to keep default(/opt/idea/bin/), note: end path with '/':" ideahome

if [ -z "$ideahome"]
then
	ideahome=/opt/idea/bin/
else
	echo "path set!"
fi

echo ""
echo "Path set to: "$ideahome 

#copy files
echo ""
echo "Generating launcher...."
sudo cp -fv klaunch.sh $ideahome
echo ""
echo "copying Icons...."
sudo cp -fv icon.png $ideahome
echo ""

#add menu entries
echo "checking for /usr/share/applications"

if [ -d "/usr/share/applications" ]
then
#found cache file
echo "found"
echo "Adding Entries...."

#NOTE: echo here is used to create files, NOT display on console, '>' creates a file, and '>>' appends to it.
sudo echo "[Desktop Entry]" > /usr/share/applications/idea.desktop
sudo echo "Type=Application" >> /usr/share/applications/idea.desktop
sudo echo "Encoding=UTF-8" >> /usr/share/applications/idea.desktop
#sudo echo "Version 1.0" >> /usr/share/applications/idea.desktop
sudo echo "Name=IDEA IDE" >> /usr/share/applications/idea.desktop
sudo echo "Comment=Launcher for IDEA by Kushal" >> /usr/share/applications/idea.desktop
sudo echo "Categories=Development;" >> /usr/share/applications/idea.desktop
sudo echo "Exec="$ideahome"klaunch.sh" >> /usr/share/applications/idea.desktop
sudo echo "Icon="$ideahome"icon.png" >> /usr/share/applications/idea.desktop
sudo echo "Terminal=true" >> /usr/share/applications/idea.desktop
echo ""
echo "updating /usr/share/applications/desktop.en_US.UTF8.cache"
sudo echo "[idea]" >> /usr/share/applications/desktop.en_US.UTF8.cache
sudo echo "Name=IDEA IDE" >> /usr/share/applications/desktop.en_US.UTF8.cache
sudo echo "Comment=Launcher for IDEA by Kushal" >> /usr/share/applications/desktop.en_US.UTF8.cache
sudo echo "Categories=Development;" >> /usr/share/applications/desktop.en_US.UTF8.cache
sudo echo "Exec="$ideahome"klaunch.sh" >> /usr/share/applications/desktop.en_US.UTF8.cache
sudo echo "Icon="$ideahome"icon.png" >> /usr/share/applications/desktop.en_US.UTF8.cache
sudo echo "Terminal=true" >> /usr/share/applications/desktop.en_US.UTF8.cache
sudo echo "Type=Application" >> /usr/share/applications/desktop.en_US.UTF8.cache
sudo echo "" >> /usr/share/applications/desktop.en_US.UTF8.cache
echo ""
echo "adding pannel entry......"

#/usr/lib/gnome-panel/gnome-panel-add --launcher=/usr/share/applications/idea.desktop
chmod 777 panel

bash ./panel

echo ""
echo "Menu set..... Enjoy!!"
echo ""
echo "------> YOU WILL FIND ENTRY IN (Applications -> Programming) MENU"
echo ""
echo "NOTE: Run command ./panel to add panel entry explicitly. was unable to add panel in sudo mode"
else
# file not found
echo "ERROR! Does not exist! this setup works for GNOME only"
exit 1
fi

now here we have added menu entry.

Adding Panel Entry:
Ubuntu has made this task easy by providing us with a python script to manipulate panels.
so all we have to do is use that script.. :D
so the code is as follows for the panel file:-

echo "adding panel......"
/usr/lib/gnome-panel/gnome-panel-add --launcher=/usr/share/applications/idea.desktop --panel=top_panel_screen0
echo "panel entry added... "

you can also add to the bottom panel with the switch –panel=bottom_panel_screen0 , now the launch button will appear next to show desktop icon at the botton left corner.
we can also align them to left or right, all is documented in the script usage guide.

Installation Guide
installation is very simple, just follow the following steps:

  1. STEP 1: Download the files: idea_desktop_Integration.tar.gz
  2. STEP 2: Extract the files to a directory.
  3. STEP 3: go in the root of the extracted files and issue command
    sudo ./setup
    

    then when asked enter idea path if it is different from the mentioned, if its same as mentioned just press enter.

  4. STEP 4: issue command:
    ./panel
    

    this has to be issued seperate because the ubuntu script for panel does not work in sudo mode

  5. STEP 5: all set Enjoy!! now whenever you have to launch IDEA just click on the icon and it will launch gracefully

~~Kushal Likhi~~
http://www.intelligrape.com

  • Share/Bookmark
Posted in Linux, System

Git – Grep

Posted by Hitesh Bhatia on January 22nd, 2011

Still in my early days of using Git. Recently just to make sure that I have merged two branches  and have pushed changes to QA server successfully,I often used command “find | xargs grep” .But to deal with this kind of situations Git provides “grep” command. Just as name suggests this commands searches for regex pattern passed to it.

 git grep "regex" 

Example – To find usage of word “collections”  in each file.

 git grep --ignore-case "collections"

output :

ReadMe.txt:groovy Collections
ReadMe.txt:nice collections

Now this just described which of project files have word ‘list’ in it and how many times.One can also use -c option to count occurrences  of word “collections”

git grep --ignore-case -c "collections"

Output:

ReadMe.txt:2
Now a question  why I am not using command (and favoring “git grep”)
find | xargs grep -y "collections"
which will easily do the same for me and would perhaps list some extra  files (which are not exactly part of my app). Now here come the answer with “git grep” I can also search in previous versions of my project.
Example
git grep --ignore-case  "collections" ea145f1
output :
 ea145f1:ReadMe.txt:groovy Collections
It searches for word “collections” in my previous commits . (As second line in file ReadMe.txt was added after commit numbered ea145f1) .Simply awesome.And this is  not it.  To explore more visit. git grep manual page or the git community book which is maintained by Scott Chacon
_________________________________

Hitesh Bhatia

Mail,LinkedIn,Facebook,Twitter

_________________________________

  • Share/Bookmark
Tags: , ,
Posted in Linux, System

Mysql Dump multipurpose script.

Posted by Hitesh Bhatia on January 12th, 2011

Taking MYSQL dump is usual requirement in my project . And Since some of the tables in project are huge so often I have to take dumps of selected tables and often have to ignore some tables.

since we had to do it frequently in our project so I created a script  which does following for me.

  1. Takes database dump
  2. Takes dump of specified tables.
  3. Takes dump of all the tables other than specified.

Inside the script I have added variables like username and password which are required by Mysql. (So if you are using this ,you will need to change them.)

USER="username"
PASSWORD="password"
DATABASE="$1"
DUMPS="Dumps"
DUMPDIR="$DATABASE$DUMPS"
FILENAME="latest.sql"
cd;mkdir "$DUMPDIR"

if [ $# -eq 0 ]; then
	echo "Usage : <databasename> <tablename*>  # if table name(s) is specified only that/those table will be dumped"
	echo "Altername Usage : <databasename> -k <table(s)dToBeIgnored>"
	exit
fi

if [ $# -eq 1 ];then
echo "Dumping $DATABASE to  $DUMPDIR/$FILENAME"
mysqldump --user="$USER" --password="$PASSWORD" "$DATABASE" > ~/"$DUMPDIR"/"$FILENAME"
fi

COMMAND="mysqldump --user=$USER --password=$PASSWORD $DATABASE"
if [ $# -gt 1 -a $2 != "-i" ];
	then
     for i in $*
	 do
		 if [ $i != "$DATABASE" -a $i != "-i" ];then
			 $COMMAND $i > ~/"$DUMPDIR"/"$i.sql";
		 fi
	 done
fi

if [ $# -gt 2 -a $2 == "-i" ];
	then
	COMMAND2=" "
	for i in $*
	do
		if [ $i != "$DATABASE" -a $i != "-i" ];then
		COMMAND2="$COMMAND2 --ignore-table=$DATABASE.$i"
		fi
	done
	$COMMAND $COMMAND2 >  ~/"$DUMPDIR"/"$DATABASE"-"Ignored.sql"
fi

Now what this can be used in three ways -
(Note – I have named this script sqlDump and all of following examples use this name )

  1. ./sqlDump.sh databasename
    This will take a complete dump of all my tables in specified database and store it in file named latest.sql
  2. ./sqlDump.sh databasename table1 table2 … tablenN
    This will take dump of all tables specified.And will store them in file named – table1.sql , table2.sql … tableN.sql
  3. ./sqlDump.sh databasename -i table1 table2 .. tableN
    This will take dump off all the tables in database ignoring specified tables. And will store it in file named <databaseName>-Ignored.sql.

If file is executed without any arguments it will just describe its Usage
Note – All of these tables will be stored in directory named <databasename>Dumps in home directory.

(more…)

  • Share/Bookmark
Posted in Grails, Linux

Linux Trick for screen collaboration or shell sharing

Posted by Tarun Pareek on November 14th, 2010

In recent week, i got to know about really good way of collaborating the screen in a linux.
 
Let us take a scenario, Suppose User having name ‘B’ on the 5th floor of the company is having a problem/difficulty related to code execution, unable to figure out what to do?, and then he call the fellow guy who can help him out, but he is on 1st floor his name is ‘Lazy A’.
 
Lazy A guy is really lazy, so here come handy screen collaboration trick to help him out and save him from walking 4 Floors to reach 5th one (:phew!:)
 
Lazy A support guy open its terminal and perform the following tricks consisting of 4 steps:
 
1. ‘Lazy A’ remote login into the User B system through following command:

lazyA# ssh B

2. After that Lazy A logged in as super user B, on B machine

B# su - B

3. Now Lazy A is on the B machine logged in as super user B, and ‘Lazy A’ runs the following command,

B# screen -S sos

4. Then Lazy A guy ask B to run following command on his/her system :

B# screen -x sos

 
Note that same alias ’sos’ is being used by both in last 2 commands, and both guy are logged in as same user.
 
Actually the last two command cause both user session joined together in linux shell and now both users can type on there shell and see what other is typing or doing to fix the issue, and this is how lazy A help out the B without walking to 5th floors.
 
After providing help, ‘Lazy A’ terminate the session by performing ctrl-C and then ctrl-D command on its terminal. Thats the way they get detach from each other and get back to there respective work.
 
And code happily ever after :)
 
Example to Test command on your machine:
 
To test the ’screen -S’ and ’screen -x’ command you can open two separate terminal or shell on your linux system, then run following command on your 1st terminal or shell
 
Terminal 1:

# su - tarun
tarun# screen -S mytest

And run another command on Terminal 2:

tarun# screen -x mytest

Then type on any screen you will see what you are doing on one is appearing on both the shells. To terminate session use ctrl-C and then ctrl-D. :)
 
Regards,
Tarun Pareek
LinkedIn

  • Share/Bookmark
Posted in Linux, System

Startup Script Using XdoTool

Posted by Hitesh Bhatia on November 13th, 2010

(Xdotool is a tool that simulates user key movements.)


Usually when we start to work , we usually require some apps to be running. In my case I have to run a grails app, sql , subversion and IDE in different tabs of same terminal.


Normally it would take almost 1-2 minutes if I do it manually.So I created a shell script using Xdotool .And it does all my work in almost 2 seconds. And All the tabs are opened in same terminal).Best part with xdotool is since it simulates key all your aliases work.


And it works perfectly on Ubuntu 10.10. (Tried and Tested)

Here is a snippet of script that I created.

xdotool key alt+t space;
xdotool type "etc";
xdotool key Return;
xdotool key ctrl+t alt+t space;
xdotool type "grails";
xdotool key Return ;
.
.
.
xdotool key ctrl+t alt+t space;
xdotool type "sql";
xdotool key Return;
xdotool type "mysql";
xdotool key Return

This opens new tab of same terminal.

1)xdotool key alt+t space;
The “key” keyword” tells xdotool that next inputs are treated as keys. So it opens “set title” pop up.(As Alt+t and space would do)


2)xdotool type “etc”;
xdotool key Return;

The “type” keyword tells that the next inputs are treated as text.And then enter key is simulated
Hence naming my tab “etc”


3)xdotool key ctrl+t alt+t space;
Opens new tab (I made ctrl+t shortcut for opening new tab in same terminal) and opens “set title” pop up


4)xdotool type “grails”;
And the tab is named as grails. And So on..


This helps me save time. hope it helps you too.

_________________________________
Hitesh Bhatia

Mail

LinkedIn,Facebook,Twitter

_________________________________

  • Share/Bookmark
Posted in Linux, System

Datamatrix for unique identification of Documents on Server Side with libdmtx

Posted by Vivek Krishna on October 22nd, 2010

I had earlier written a post on how to use QR Code to take care of identification of printed documents and updating the DB based on the QR Code detection. However, we had to abandon the use of QR Codes in the document because the library wasn’t performing reliable, even with QR Codes from image files, which were direct screenshots of the PDF. Add to this, the fact that ZXing was also consuming a lot of memory. We believe that it was just not meant for use in a web application, even though it is a low traffic application which caters to a very specific niche.

We decided to try out DataMatrix. However,  there was a dearth of Free java libraries to render/detect them and the cost of the paid libraries put us off, as well as the lack of clarity on the respective product sites about whether they can be used from inside a web application.

We found that we could use a free native library named libdmtx, which works on top of Imagemagick to take care of our requirements. There was a java wrapper around it, but we couldn’t get it working even though we tried the steps mentioned on their page. So after a lot of thought and the consideration that we were not going to migrate to a non Unix platform for any of the environments, we decided to use the library to create images and detect them using the libdmtx-utils.

The service code reads something like this.


def createAndReturnBytes(String text) {
File tmpFile = new File(System.getProperty('java.io.tmpdir') + File.separator + 'tempStringHolder.txt') //Temporary file to hold the string to be encoded. Using 'echo' in the didn't work
tmpFile.write(text)
def p = "dmtxwrite ${tmpFile.absolutePath} -o ${filename}".execute() //Execute the process
p.waitFor() //Wait for the process to complete before proceeding further
byte[] contents = new File(filename).bytes //Retrieve the bytes from the image file
new File(filename).delete()
return contents //Return the bytes for the image

}

 String detectDataMatrix(File file){
 def p = "dmtxread -N1 ${file.absolutePath}".execute().text //-N1 flag to break execution after the first datamatrix on the file has been detected
 return p
 }

This has been working very reliably for us. To enhance the images further, we may use ImageMagick to crop the area, where we think the datamatrix is and zoom on it before sending the file to the datamatrix service for detection.

Hope this helps someone

Vivek

http://in.linkedin.com/in/svivekkrishna

  • Share/Bookmark
Posted in Grails, Groovy, Linux

Setting Expires HTTP header on server response through Apache

Posted by Tarun Pareek on September 13th, 2010

In recent poc, i have set the expires HTTP header on server response for one of the project, such that it will again set when access by user for the specified period, if header is expired already.

The module that control it is “mod_expires.c”. This module is not enabled by default.

You need to enable it by following command (You need to be sudo user):

a2enmod {module name} //here module name is "expires"

Then, open your site configuration file available at apache2/sites-available/{your site}

Append following xml entries in your site configuration file :

<IfModule mod_expires.c>
      <FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
          ExpiresActive On
          ExpiresDefault "access plus 2 days"
      </FilesMatch>
</IfModule>

ExpiresDefault Syntax :

ExpiresDefault "{base} [plus] {num  type}*"

Note : Here base is : ‘access’ and the type is : ‘years’, ‘months’, ‘weeks’, ‘days’, ‘hours’, ‘minutes’, ’seconds’

You can also use “ExpiresByType” for setting different expires for different file type instead of “ExpiresDefault”.

Hope this code will help :)


Regards,
Tarun Pareek
Intelligrape Software

http://in.linkedin.com/in/tarunpareek

  • Share/Bookmark
Posted in Linux, System

Linux – Managing applications running on system start-up

Posted by Himanshu Seth on July 14th, 2010

Rcconf is a tool that we recently discovered. This tool allows you to manage your start-up applications easily.

We found this tool when me and my colleague Aman were trying to identify the processes that we never use but they do eat up a lot of our system resources. We found this link very useful: http://www.debianadmin.com/manage-linux-init-or-startup-scripts.html.

The other way to manage system start-up applications is as the page suggests, by using update-rc.d, but rcconf provides an interface which is easy to understand and use.

To install rcconf, run the following command on your command line:

sudo apt-get install rcconf

To run rcconf, you’ll need sudo privileges:

sudo rcconf

The application looks like :

* means that application is a part of startup
Use spacebar to uncheck the application

Save the settings and restart your linux box. I removed unused applications like postgres, tomcat, monit, bluetooth etc.

And my system does feel a bit faster :)

Regards
~~Himanshu Seth~~

http://www.IntelliGrape.com

  • Share/Bookmark
Posted in Linux, System

How to Set-up shared folder/repository between two or more users on Linux

Posted by Himanshu Seth on June 14th, 2010

We had a case where we wanted two different applications (run by different users) to be able to read and write from the same file system.
This is how we solved this problem:

  1. Create a group which these users will belong to : groupadd <GroupName>
  2. Edit user1 and user2 to be a member of this group: usermod -a -G <GroupName> user1 & usermod -a -G <GroupName> user2
  3. Create a shared directory. In our case, it had to be the document root for an Apache site. Thus we chose the location <Shared_Folder>
  4. Now we need to change the group of this folder : chgrp -R <GroupName> <Shared_Folder>
  5. We’ll also need to grant the group write access on this folder : chmod g+w <Shared_Folder>
  6. Now we’ll need to set the GroupID flag on this folder. For a directory, the set-groupID flag means that all files created inside that directory will inherit the group of the directory. Without this flag, a file takes on the primary group of the user creating the file. This property is important to people trying to maintain a directory as group accessible. The subdirectories also inherit the set-groupID property. (http://www.dartmouth.edu/~rc/help/faq/permissions.html)
  7. Now in your .bashrc / .bash_profile, set the umask as 002. Setting this umask ensures that all the newly created files by this user will have the permission “rw-rw-r”. Thus giving the group write permission.

Now when either of the users create any file in the <Shared_Folder>, all the users of this group will have the read/write permissions on that file. Not only this, these permissions will be on the subfolders and the files with-in that folder as well.

But, if any of these users create a file outside the <Shared_Folder>, the primary group of that file/folder will be the same as the primary group of that user. Thus files/folder only in the <Shared_Folder> are shared between these users.

This is just one of the many great abilities that Linux provides.

Hope this saves you some time.

Your feedback and suggestions are welcome.

Regards
~~Himanshu Seth~~

http://www.IntelliGrape.com

  • Share/Bookmark
Posted in Linux, System

Changing Location of MySql DataStore

Posted by Vivek Krishna on June 7th, 2010

On one of our projects, we had to store the MySql Data at a location different from the default location. This was needed because the server was on an instance-store Amazon instance which meant that the data would get lost if the instance had to be rebooted for some reason. After going through the tutorials and articles on AWS, we came across this excellent piece on how to setup MySql Data store on an EBS volume.

Later on, I tried out the steps on the local machine and see if we could move the MySql data store to some other location on our system. As it turned out, we could and here are the steps I followed.

Stop mysql server running on your system with the command

sudo /etc/init.d/mysql stop

Now, create folders named etc, lib and log at any location where you wish to save your mysql data. Let us keep them inside a folder called “/mysqldata”

mkdir etc lib log

Now, back-up your mysql data by copying the contents of /etc/mysql, /var/lib/mysql and /var/log/mysql to another location. Then, move the folders /etc/mysql, /var/lib/mysql and /var/log/mysql to the “/mysqldata”

mv /etc/mysql /mysqldata/etc
mv /var/lib/mysql /mysqldata/lib
mv /var/log/mysql /mysqldata/log

Let us now create new folders /etc/mysql, /var/lib/mysql, /var/log/mysql

mkdir /etc/mysql
mkdir /var/lib/mysql
mkdir /var/log/mysql

Now these new directories need to be mapped with the ones we moved to “/mysqldata”
This is done by setting the mount point for each of these directories to the ones which we created.
To do so, appending the following entries to “/etc/fstab”

mkdir /etc/mysql
/mysqldata/etc/mysql /etc/mysql none bind
/mysqldata/lib/mysql /var/lib/mysql none bind
/mysqldata/log/mysql /var/log/mysql none bind

With this done, let us mount all the entries by issuing the command

mount -a

MySql data store is now at another location.

Hope this helps

  • Share/Bookmark
Tags: , ,
Posted in Database, Linux