Sunday, 7 April 2013

How to checkout other branch exact files in git

Here i am going to explain you, how to checkout other branch particular file in your working tree or current branch. Just assume your current working directory is "future"(git branch name is future).
You have one more branch called "master". Suppose if you want to checkout particular file from master branch into your future branch. Then do the following command it will work out well.

Lets move to your current branch, here "future" using following command

git checkout future

Now you shifted into "future" branch. Then do the following command, which we need exactly for our need(checkout particular file from master branch).

git checkout master -- file_1_name.txt,file_2_name.txt
(Just for examples file_1_name.txt and file_2_name.txt. According to your need you can change into your desired filenames list).

The above command will override your current branch(future branch) files you mentioned above command. So your local changes will be ignored.

Hopefully now you come to know how to checkout other branch files in your current branch!!. 

Monday, 18 March 2013

Most useful linux crontab with examples

Linux crontab is very useful utility program, if you plan to do schedule a routine in background with specific hour/time or day basis. Mainly this program will used by linux sysadmins because they knows how this utility will helps in Maintenance some background scheduler routines in their daily activities. Let's see that in detail in this article.

Linux crontab syntax/format as follows

00-59 00-23    00-31                 01-12                           0-6              (Command)
MINS HOURS DAYOFMONTH MONTH-IDENTIFIER DAY-OF-WEEK CMD

MINS -- Minutes identifier(Range is 00-59)

HOURS -- Hours identifier(Range is 00-23 format)

DAYOFMONTH -- Day of month identifier(Range is 00-31)

MONTH -- Month identifier (Range is 01-12)

DAY OF WEEK -- Day of the week (Range is 0(Sunday)... 6(Saturday))

CMD -- shell script going to execute on specified time on crontab.  

Let's see various time specific examples using crontab

 *) How to run specific task on particular time - Example (March 18th 2013 at 5.30 PM)

   30 17 18 03 * /home/madhan/<shell script.sh>
   Note: Hour format uses 24hrs, so if you want to specific 5.30 AM use 5 in HOUR tab.If    you want to run at 5PM use 17 in HOUR tab.

 *) How to run multiple times on same day(for example i want to run the task at March  18 8am and 10PM)
   00 8,22 18 03 * /home/madhan/<shell script.sh>
   8,22 -- 8AM and 10PM  

 *) How to set every ten minutes will run the task on same day(for example March 18 in  between 11-12 but only for ten minutes interval)
   */10 11-12 18 03 * /home/madhan/<shell script.sh>  

 *) Suppose if you want to run everyday and every month at 2.30PM, just set the following time stamp on cron tab field.
 30 14 * * * /home/madhan/<shell script.sh>

 *) How to run task in between timings for example everyday at 9am-6pm (office       working hours)   00 9-18 * * * /home/madhan/<shell script.sh>  
  9-18 -(9am,10am,12pm,1pm,2pm,3pm,4pm,5pm and 6pm)

 *) How to run the above task only on weekdays(Monday-Friday only) 
    00 9-18 * * 1-5 /home/madhan/<shell script.sh>
    9-18 -(9am,10am,12pm,1pm,2pm,3pm,4pm,5pm and 6pm)
    1-5 (Monday,Tuesday,Wednesday,Thursday and Friday)

 *) How to view cron tab as logged-in user
    crontab -l (Will show you the list)

 *) How to view root user crontab list
    Login as root user and do "crontab -l"

 *) How to view other user crontab list
    crontab -u <username> -l  

 *) How to edit crontab item
    crontab -e  

 *) How to schedule task for every minutes
    * * * * * /home/madhan/<shell script.sh>

 *) Crontab has some special keywords for very frequent stuff just like daily,monthly yearly and reboot once stuff. Lets see
  @yearly equals to 0 0 1 1 * 
  @daily equals to 0 0 * * * 
  @monthly equal to 0 0 01 * *
  @hourly equals to 0 * * * *
  @reboot equals to Run at startup.(It will execute only once the machine got  booted everytime)  

 *) How to disable/redirect crontab mail output Normally crontab will send an output to who schedule the job. so if you dont want that email output just simply make it as empty MAIL variable
   MAIL = ""
  OR
  (You can redirect to log file something like > /tmp/cron_output/output.log) @yearly /home/madhan/script.sh > /tmp/cron_output/output.log)

 *) How to specify path variable in crontab The above examples we are specifying absolute path of the shell script file to avoid that you can define that absolute path once in PATH variable.
 PATH =  /home/madhan After that you can specify just like @yearly <shell script.sh> Thats it.


Hopefully you can enjoy this article!!!

Sunday, 17 March 2013

Scheduler using linux crontab

Suppose if you want to do some scheduler kind of tasks in ubuntu/linux platforms, crontab utility is very useful for that. Here i am going to explain you how to use this crontab for scheduler stuff.

* How to view what are the cron tasks available for you, just do the following command in your terminal/console.

madhan@ubuntu:~$ crontab -l (It will list of what are the scheduler tasks available for current user)

output of the above command is just like this

madhan@ubuntu:~$ crontab -l
*/5 12 17 03 0 bash /home/madhan/my_first_linux_shell_script > /tmp/mdn_cron1.log

detail about the above crontab command

*/5  ---> every 5 mins
12   ---> 12pm
17   --->  Day of the month
03   --->  Month identifier
0     --->  Day of the week( Normally 0(sunday)-6(saturday) )

"bash /home/madhan/my_first_linux_shell_script" ---> (command going to execute on exact time what we mentioned above. "my_first_linux_shell_script" is shell script file which has real commands going to run on scheduled time.)

>  /tmp/mdn_cron1.log (means output redirection to that log file, if anything fails we can just look into that file for further investigation ).

Ok Let's see How to add your tasks into crontab.

1) Let's create a shell script (I am ruby on rails developer, so i am just writing one small shell script which will run one rake task according to my project, so my shell script contains commands related RoR).

#! bin/bash
# This is my first linux shell script
echo "WoW shell script is working fine"
cd /home/madhan/abl/CyncAbl-R3
bundle exec rake cync:load_dilution_history

Just save this file in the extension of ".sh"(But recent ubuntu distributions its not required to save the file with extension of ".sh". So i saved the file without .sh extension)

2) Lets create one text file for your entire cron items.
  
*/5 12 17 03 0 bash /home/madhan/my_first_linux_shell_script > /tmp/mdn_cron1.log

<cron item 2>(as your wish if you have more that one scheduler)
<cron item 3>(as your wish if you have more that one scheduler)
<cron item4>(as your wish if you have more that one scheduler)

Just save the file with the extension of just ".txt". I save this file "my_cron_tab.txt"

3) Let's add that txt file into cron tab utility. The following command will use

 crontab /home/madhan/my_cron_tab.txt

You are almost done!!!

Lets see whether it will display correct cron item or not using this command

crontab -l (Will list out all scheduler task items available in "my_cron_tab.txt" file)

How to edit crontab, just do the following command
crontab -e

NOTE: if you stuck with executing the scheduler crontab, always you can view the log file in  /var/log/syslog. It has some entries, from there you can investigate further. In short just type it in your console "tail /var/log/syslog" it will show few last lines on that file).

Hopefully you can get little bit clear picture about linux crontab stuff.

Sunday, 17 February 2013

group concat usage in MySQL

In this article, i am going to explaining usage of group_concat in MySQL with few example tables. Hope you people enjoy this concept, probably one who learn MySQL concepts. I am introduction two tables one is called "employee" table and "Department" table.

Structure and Data about employee table.


 Id
name
Department_id
1
Madhan
1
2
nisha
2
3
janani
1
4
ajay
1
5
poovitha
2
6
ayyasamy
1
7
Jack
3

Structure and Data about department table.


 Id
name
1
Accounts
2
IT
3
Product

Suppose if you want to query how many employees each and every department, you can use this following MySQL query.

SELECT department.name AS department_name,COUNT(*) AS department_total_employee FROM department
JOIN employee ON employee.department_id = department.id
GROUP BY employee.department_id

 Result of the above query



Department_name
Department_total_employee
Accounts
4
IT
2
Product
1

Just think about, now you got it how many employees available in each and every department.
If you want the detail, what the people belongs to which department in aggregate manner, you come know the real usage of GROUP_CONCAT method.

So What is GROUP_CONCAT?
" It is used to concatenate multiple column values into single string". If you won't use this, you have to lookup multiple rows and then concatenate them.

Note:
* In side group_concat method, you can override default seperator comma using the following command. Example group_concat(col.name SEPARATOR '--')

* You can change the order by also example
 group_concat(col.name order by col.name DESC)
The following query will give you the result of each and every department employee count as well as what are the employees on the particular department.

SELECT department.name AS department_name,COUNT(*) AS department_total_employee,GROUP_CONCAT(employee.emp_name) AS employees_under_department FROM department
JOIN employee ON employee.department_id = department.id
GROUP BY employee.department_id

  Result of the above query


Department_name
Department_total_employee
Employees_under_department
Accounts
4
Ayyasamy,janani,ajay,Madhan
IT
2
Poovitha,nisha
Product
1
jack

Thursday, 14 February 2013

How to implement bulk insert in Rails

Yeah we can implement using "ACTIVERECORD-IMPORT" library, in rails 3.x (It will not support for rails 2.x versions).

Just look at the following code snippets

Class User < ActiveRecord::Base
  ######## schema information about user ##########
    login
    user_name
    email
    address
    city
    state
    country
    zipcode
  ######## schema information ended here #########
  has_many :posts
  has_many :comments
end
Suppose if you want to store/save 5-10 users into database table, normally you can do the following way

test_name= "user_name"
10.each do |number|
  User.create!(:user_name => test_name + number,:login_name => "login"+number,..etc fields)
end
The above code works fine without any issues. Just think about if you want to save 1000 or 2000 records, that time the above code snippets will eat huge time to save the records. So avoiding/handle that huge time difference Ruby on Rails, We can use the library called "activerecord-import".

Now we can insert 1000/2000 records at one shot using activerecord-import library. Here is the small code snippet implementation for your reference

users = []
test_name= "user_name"
1000.each do |number|
  users << User.new(:user_name => test_name + number,:login_name => "login"+number,..etc fields)
end
User.import users

Thats it. In single shot your entire 1000 records inserted into your database.

Note: This library only will work on Rails 3.x versions only not 2.x versions, so keep it this point before your implementation.


Thursday, 7 February 2013

How to clone a git repository without history

Suppose if you have one git repository, if you are planning to share that repository to your working partner but it has some sensitive data/files. That time better create a new branch and edit those sensitive files and then get a clone of the repository without history then give to your parter. Using that cloned repository without history, your partner could not able to push that repository to anywhere else. But still you can get work done from your partner as "PATCHES".

Using the following command you can create a shallow repository with truncated/limited history.

git clone --depth 1 url_of_your_remote_repository

Limitation of shallow repository

* you can't clone or fetch from the shallow repository(That means you can't make a new repository using this shallow copy)

* you could not able to view whole history using git log command.

* Its workable solution, your partner can make some changes and make it as "patches" and send it to you partner if you wants your partner changes. Then apply that patches in your current working tree/ whatever branches you want to apply.

Saturday, 2 February 2013

How to install gems group specific in your rails application

In Ruby on Rails application Gemfile, while you defining gems we can make that gem in a particular group. For example some gems needs to be loaded only on test environment so on that time we can mention those kind of gems into one specified groups. so your gemfile should like this

gem 'aws-s3'
gem 'paperclip'
group :test do
  gem 'rspec'
  gem 'waitr'
  gem 'faker'
end

If you are planning to define just one gem into a particular group only you can use without blocks like this

gem 'rest-client', :group => :development
gem 'cucuber-rails', :groups => [:development,:test]  (cucuber-rails gems comes under both group)

Similarly you can make it :development,:default(group name) or give a meaningful names whatever you want as group name.

Finally you have to tell while running the "bundle install" command just ignore what are the groups to be ignored using --without.

bundle install --without development
bundle install --without development test (ignore both groups and install others)
The above command ignore whatever gems we mentioned in the development group. So it will install rest of the default group(if you are not mention any group  those gems comes under defaults group)gems and test group gems.

Note:
bundle install --without development (ignore development group gems)
bundle install (still bundle remembers --without development so result is still ignore development groups it will not install all gems)

bundle install --without nothing (just clearing cache, now all the gems to be loaded into the ruby loadpath)