Home

Spoon which file been touched by opensnoop

opensnoop tracks file opens. As a process issues a file open, details such as UID, PID and pathname are printed out.

The returned file descriptor is printed, a value of -1 indicates an error. This can be useful for troubleshooting to determine if appliacions are attempting to open files that do not exist.

Since this uses DTrace, only users with root privileges can run this command.


What's the meaning of '>/dev/null 2>&1'?

看到syslog里有这个 (nagios) CMD (/dev/shm/.b/cutitas >/dev/null 2>&1) 突然发现忘记 >/dev/null 2>&1 什么意思了,温习一下。

  1. 在linux里的默认file descripter

0 = stdin 1 = stdout 2 = stderr

  1. > 代表重定向
  > file # clear file content
  1. /dev/null 空设备文件,用来丢弃输出结果的。

  2. 2> 表示stderr错误重定向

  3. & 表示‘等同于’的意思。 2>&1表示2的结果等同于1

其实 > /dev/null 2> &1 == 1> /dev/null 2> &1

所以这个命令 echo "xxx" > /dev/null 2>&1 就是抛弃所有的输出结果,反正也看不到,要看去日志吧,如果你记了日志的话。

值得注意的地方:

command > file 2>filecommand > file 2>&1 区别?

虽然同时输出到同一个文件,但是file会被打开两次, 这样stdout和stderr会互相覆盖, 这样写相当使用了FD1和FD2两个同时去抢占file的管道。 而command >file 2>&1 这条命令就将stdout直接送向file, stderr 继承了FD1管道后,再被送往file,此时,file 只被打开了一次,也只使用了一个管道FD1,它包括了stdout和stderr的内容。

从IO效率上,前一条命令的效率要比后面一条的命令效率要低,所以在编写shell脚本的时候,较多的时候我们会command > file 2>&1 这样的写法。


ActiveRecord's composed_of method

I’m rare to use AR’s composed_of method, but always writing a method to override the default attributes to make conversion for database attributes.

Let’s have look at this simple exmpale:

class Website
  composed_of :domain,
    # Specify custom class name for association. default is Domain here.
    :class_name => 'WebsiteDomain',
    # mapping entity attr to value obj
    :mapping => [%w(domain_ip ip)],
    # will be called when init a value obj, all mapped attrs will be passed into the block
    :constructor => Proc.new { |sitename| WebsiteDomain.new(sitename) },
    # this will be called when a new value is assigned to the value object
    :converter => Proc.new { |domain_ip| domain_ip.is_a?(Integer) ? WebsiteDomain.new(domain_ip) : WebsiteDomain.new }
end

Parslet a very cool Parsing Expression Grammar parser

Recently I made a japanese language learning tool, which is very cool :) . In this tool we need to create a mark language parser to parse like this [kanji|furigana], My colleague zete, use his very professional regexp skill to make a regexp to parse this text.

WORD_PARSER = /
  (?<kanji>   [\p{Han}\p{Hiragana}\p{Katakana}]+    ){0}
  (?<furi>    \p{Hiragana}+                         ){0}
  (?<oword>   [^\]]+                                ){0}
  (?<invalid> .                                     ){0}
  (?<word>    \[ \g<sp> \g<word_content> \g<sp> \]  ){0}

  (?<word_content> \g<kanji> \g<sp> \g<right> | \g<oword> ){0}
  (?<right>   \| \g<sp> \g<furi>                    ){0}
  (?<sp>      [\t\ ]*                               ){0}

  \g<word> | \g<invalid>
/ux

Look at above stunning regular expression. I never toughed this level of complexity of regexp. But later on when I was looking at http header parser, then found a good tool, called parslet. It just do the exactly same thing, and even could do more complicated things like parsing a language.

require 'parslet'
include Parslet

furidown = "[中国|ちゅうごく][雲南|うんなん][省|しょう][の][昆|こん][明|あきら][市|し][在住|ざいじゅう][の][27][歳|さい][の][アメリカ][人|じん][。]"

# Constructs a parser using a Parser Expression Grammar
class Furidown < Parslet::Parser
  rule(:space)      { match('\s').repeat(1) }
  rule(:space?)     { space.maybe }
  rule(:lbracket)   { str('[') >> space? }
  rule(:rbracket)   { str(']') >> space? }
  rule(:split)      { str("|") >> space? }
  rule(:kanji) { match('[^\]\|]').repeat.as(:kanji) }
  rule(:furigana) {split >> match('[^\]\[]').repeat.as(:furigana)}
  rule(:word) {lbracket >> kanji >> furigana.maybe >> rbracket}
  rule(:words) { word.repeat }
  root(:words)
end

parser = Furidown.new
p parser.parse(furidown)

# the result like
=> [{:kanji=>"中国"@1, :furigana=>"ちゅうごく"@8}, {:kanji=>"雲南"@25, :furigana=>"うんなん"@32}, {:kanji=>"省"@46, :furigana=>"しょう"@50}, {:kanji=>"の"@61}, {:kanji=>"昆"@66, :furigana=>"こん"@70}, {:kanji=>"明"@78, :furigana=>"あきら"@82}, {:kanji=>"市"@93, :furigana=>"し"@97}, {:kanji=>"在住"@102, :furigana=>"ざいじゅう"@109}, {:kanji=>"の"@126}, {:kanji=>"27"@131}, {:kanji=>"歳"@135, :furigana=>"さい"@139}, {:kanji=>"の"@147}, {:kanji=>"アメリカ"@152}, {:kanji=>"人"@166, :furigana=>"じん"@170}, {:kanji=>"。"@178}]

Fantastic!!!

Reference: DSL doc Examples Get started Parslet intro - very good intro to parse erb


HTML5 methods and properties audio and video element

Properties:

  • paused (get)
  • ended (get)
  • seeking (get)
  • duration (get)
  • playbackRate
  • defaultPlaybackRate
  • seekable
  • played
  • muted (get/set)
  • volume (get/set)
  • currentTime (get/set)
  • src (get/set)

Methods:

  • play()
  • pause()
  • load()

Events

  • loadeddata
  • progress
  • timeupdate
  • seeked
  • canplay
  • play
  • playing
  • pause
  • loadedmetadata
  • ended
  • volumechange