Kungs's Blog

  • Rails
  • Ruby
  • Linux
  • Categories
  • About
  • 22 Sep 2014 » 查看进程是否存在,并杀死

    有时候我们需要定时查看一些进程是否存在,不存在的话,需要重新启动。


    直接查看进程是否存在

    	#!/bin/bash
    
    	D=`date -d "seconds" "+%Y_%m_%d_%H_%M_%S" `
    	echo $D
    	proc_num=`ps ax | grep ruby | grep do_refund_action | wc -l`
    	if [ $proc_num -eq 0 ]; then 
    		echo 'start up do_refund_action'
    		nohup ruby do_refund_action.rb & 
    	else
    		echo "do_refund_action is running ...."
    	fi
    

    通过查看进程id来判断

    	#!/bin/bash
    
    	#根据某个字符串查找匹配到的pid
    	function pidof_str
    	{
    		proc_str=$1
    		proc_num=`ps ax | grep $proc_str | grep -v 'grep ' | awk 'BEGIN{s = "" }{ s=$1 " " s }END{print s}' `
    		echo $proc_num
    	}
    
    	#1:存在, 0:不存在
    	function proc_exist
    	{
    		proc_str=$1
    		pid=$( pidof_str $proc_str )
    		if [ "X" != "X$pid" ]; then
    			echo 1 
    		else
    			echo 0
    		fi
    	}
    
    	proc_str='gateway.rb'
    
    	exist=$( proc_exist $proc_str )
    
    	if [ $exist -eq 1 ]; then
    		echo "$proc_str is running ..."
    		exit 1
    	fi
    
    	nohup ruby test.rb &
    	echo "run $proc_str succ!"
    
  • 06 Jan 2014 » Summary

    新的一年,却没有新的惊喜。


      去年11月份换了份新工作。
      之所以在这个时间点换工作,是因为看不到公司的未来。说到这,估计会有人呵呵一笑:有多少公司看得到未来?也是,谁能说的准未来呢。但至少,得做自己稍微感兴趣的方向吧。于是,我来到了新的公司。
      新公司,新气象,当然,也少不了一些插曲。
      两个月后,我的心却一团乱麻。只依稀记得,当初进来时是对公司业务十分感兴趣。
      有人工作为了混口饭,有人工作为了兴趣。你是哪种?
      不管是混口饭,还是为兴趣,都不容易。
      所以,啰啰嗦嗦半天,啥都没讲咯?

  • 05 Apr 2013 » 重新编译nginx

    许多时候我们需要增加新的模块到nginx,这时需要重新编译nginx。


    Get package sources

    sudo apt-get build-dep nginx         # Install the dependencies
    cd /tmp                              # let's build here: /tmp
    sudo apt-get source nginx 
    

    Add some additional modules

    cd /tmp
    git clone https://github.com/wandenberg/nginx-push-stream-module.git
    
    cd /tmp/nginx-1.2.1/debian/modules     # Go to Nginx "modules" dir
    ln -s /tmp/nginx-push-stream-module    # and add a link to your module dir
    

    现在编辑 “/tmp/nginx-1.2.1/debian/rules” 这个文件,将下面一行加进去:

    ...
    --add-module=$(MODULESDIR)/nginx-push-stream-module \
    ...
    

    Compile the package

    cd /tmp/nginx-1.2.1 && dpkg-buildpackage -uc -b -j2
    
    # -uc  - do not sign the .changes file
    # -b   - binary-only build, no sources
    # -j4  - number of parallel jobs (just in case)
    

    现在安装包出现在上一层目录:

    dpkg-deb -I ../nginx-full_1.2.1-2.2ubuntu0.1_amd64.deb      # Information about the package
    

    安装这个包:

    dpkg -i /tmp/nginx-common_1.2.1-2.2ubuntu0.1_all.deb
    dpkg -i /tmp/nginx-full_1.2.1-2.2ubuntu0.1_amd64.deb
    

    System update may reinstall Nginx and remove your modifications

    当新版本nginx出来的时候,你重新编译的nginx有可能会被覆盖,为了防止覆盖,你需要这样做:

    sudo aptitude hold nginx
    sudo aptitude hold nginx-full
    
  • 30 Jan 2013 » 用ruby实现常见的排序算法

    有许多经典的排序算法,经常不用都快忘了,于是便用ruby实现了一下,加强记忆


    冒泡排序

    class Array
      def bubble_sort
        return self if self.size < 2
    
        1.upto self.size - 1 do |i|
          1.upto self.size - i do |j|
            self[j], self[j - 1] = self[j - 1], self[j] if self[j] > self[j - 1]
          end
        end
      end
      self
    end
    
    1.9.3p327:> a = [1, 3, 53, 64, 5, 7, 98, 34]
    => [1, 3, 53, 64, 5, 7, 98, 34] 
    1.9.3p327:> a.bubble_sort
    => [98, 64, 53, 34, 7, 5, 3, 1] 
    

    快速排序

    class Array
      def quick_sort(a = self)
        return a if a.size <= 1
        middle = a.shift
        left, right = a.partition {|elem| elem > middle}
    
        quick_sort(left) + [middle] + quick_sort(right)
      end
    end
    
  • 02 Dec 2012 » 在Rails中使用polymorphic(多态)

    现在有个场景:两个model:Album 和 Book,这两个model都需要添加一个图片。有两种方法:


    建立直接关联关系

    首先,添加一个Imgae model,做一下设置:

    class Album < ActiveRecord::Base
      has_one :image, :dependent => :destroy
    end
    class Book < ActiveRecord::Base
      has_one :image, :dependent => :destroy
    end
    class Image < ActiveRecord::Base
      belongs_to :album
      belongs_to :book
    end
    

    同时,表albums添加字段:image_id, 表books添加字段:image_id。
    搞定,完全符合要求。
    过了一天,客户说,Person加个头像吧,好吧,只能把上边的步骤重复一遍,不觉的烦么?


    建立polymorphic关联关系

    class Album < AcitveRecord::Base
      has_one :image, :as => imagable, :dependent => :destroy
    end
    class Book < ActiveRecord::Base
      has_one :image, :as => imagable, :dependent => :destroy
    end
    class Image < ActiveRecord::Base
      belongs_to :imagable, :polymorphic => true
    end
    

    此时,imgaes表中需加两个属性: imagable_id, imagable_type 。

    @book = Book.new(params[:book])
    @book.build_image(params[:image])
    @book.save
    @book.image
    

    或者:

    @book = Book.new(params[:book])
    @image = Image.create(params[:image])
    @image.imagbale = @image
    @book.save
    
  • 15 Oct 2012 » Rails内置的STI

    有两个类:

    class Apple < ActiveRecord::Base
      def name
        p self.name
      end
    end
    
    class Fruit < AcitveRecord::Base      
      def name
        p self.name
      end
    end
    

    很明显,这个两个类是继承关系,设计表的时候不太可能建apples和fruits两个表。
    此时,需要应用Rails内置的STI机制来简化设计:

    新建一个表fruits,在表中加一个字段“type”,这样,在model只需这么设计:

    class Fruit < ActiveRecord::Base
      def name
        p self.name
      end
    end
    
    class Apple < Fruit
    end
    
    Apple.new.name
    

    在数据库中只有一个表fruits,如果Apple.create,fruits表新加一条记录,此时,这条记录的“type”值为“Apple”。

  • 01 Oct 2012 » 关于Ruby的self

    Ruby的self在使用时需要多加小心,在不同的地方使用,效果是不一样的。


    Top level context

    1.9.3p362 :001 > p self
    main
     	=> main
    

    这里,在irb里直接打出self,得到默认对象main。


    在类中

    1.9.3p362 :001 > class Person 
    1.9.3p362 :002?>   p self
    1.9.3p362 :003?> end
    Person
    => Person
    

    在‘类方法’中

    1.9.3p362 :001 > class Person
    1.9.3p362 :002?>   def self.method
    1.9.3p362 :003?>     p self
    1.9.3p362 :004?>   end
    1.9.3p362 :005?> end
     => nil 
    1.9.3p362 :006 > Person.method
    Person
     => Person 此时得到的是Person这个Class。需要注意的是,这里self.method不需Person实例化,直接调用。
    

    在类的实例方法中

    1.9.3p362 :001 > class Person
    1.9.3p362 :002?>   def method
    1.9.3p362 :003?>     p self
    1.9.3p362 :004?>   end
    1.9.3p362 :005?> end
     => nil 
    1.9.3p362 :006 > Person.new.method
    #<Person:0x00000001a19658>
     => #<Person:0x00000001a19658> 此时得到的是实例化后的Peroson。