Ubuntu Cron 定时任务

1. crontab -e

cron 作业的时间表示例
调度计划
小时 月中日 月份 周中日期 说明
0 1 15 1,3,5,7,9,11 * 在 1、3、5、7、9 和 11 月的 15 日的 1 a.m. 运行命令。为了更容易理解,也可以把这个时间表写成 0 1 15 jan,mar,may,jul,sep,nov *。在指定列表时,不要在逗号后面加空格。
0-59/15 * * * * 这个调度计划每 15 分钟运行命令一次。
30 * * * wed,fri 这个时间表只在星期三和星期五每小时的 30 分时执行命令一次。(在列表中可以使用日和月份的名称,但是在范围中不可以)。
0,30 0-5,17-23 * * * 在午夜到 5 a.m. 以及 7 p.m. 到 11 p.m. 之间整点时和 30 分时运行命令。
0 0 1 1 * 在每年 1 月 1 日午夜执行命令一次。
0 0 * * 0 在每个星期日午夜运行命令。这相当于每周一次。
30 0 10,20,30 * 6 因为月中日和周中日受到限制,这个时间表在每星期六和每月的 10、20 和 30 日(二月除外)的 12:30 a.m. 运行命令。

2. example file:
* * * * * /root/hourtest
3. /root/hourtest file:

#!/bin/bash
/usr/local/rvm/gems/ruby-2.1.5/wrappers/ruby /root/kuailv/Crawler/test.rb
4. 重启Cron
service cron restart

read timeout reached (Faraday::TimeoutError)

Problem:


/usr/local/rvm/gems/ruby-2.1.5/gems/excon-0.44.2/lib/excon/socket.rb:291:in `raise_timeout_error’: read timeout reached (Faraday::TimeoutError)
        from /usr/local/rvm/gems/ruby-2.1.5/gems/excon-0.44.2/lib/excon/socket.rb:189:in `rescue in read_nonblock’
        from /usr/local/rvm/gems/ruby-2.1.5/gems/excon-0.44.2/lib/excon/socket.rb:165:in `read_nonblock’
        from /usr/local/rvm/gems/ruby-2.1.5/gems/excon-0.44.2/lib/excon/socket.rb:35:in `read’
        from /usr/local/rvm/gems/ruby-2.1.5/gems/excon-0.44.2/lib/excon/response.rb:133:in `parse’
        from /usr/local/rvm/gems/ruby-2.1.5/gems/excon-0.44.2/lib/excon/middlewares/response_parser.rb:6:in `response_call’
        from /usr/local/rvm/gems/ruby-2.1.5/gems/excon-0.44.2/lib/excon/connection.rb:372:in `response’
        from /usr/local/rvm/gems/ruby-2.1.5/gems/excon-0.44.2/lib/excon/connection.rb:236:in `request’
        from /usr/local/rvm/gems/ruby-2.1.5/gems/faraday-0.9.1/lib/faraday/adapter/excon.rb:54:in `call’
        from /usr/local/rvm/gems/ruby-2.1.5/gems/faraday-0.9.1/lib/faraday/request/url_encoded.rb:15:in `call’
        from /usr/local/rvm/gems/ruby-2.1.5/gems/faraday-0.9.1/lib/faraday/rack_builder.rb:139:in `build_response’
        from /usr/local/rvm/gems/ruby-2.1.5/gems/faraday-0.9.1/lib/faraday/connection.rb:377:in `run_request’
        from /usr/local/rvm/gems/ruby-2.1.5/gems/faraday-0.9.1/lib/faraday/connection.rb:140:in `get’
        from ctrip.rb:66:in `<main>’

Solution:

conn = Faraday.new(:url => “http://you.ctrip.com”) do |faraday|
  faraday.request  :url_encoded             # form-encode POST params
#   faraday.response :logger                  # log requests to STDOUT
  faraday.adapter  :excon   # make requests with Net::HTTP
  faraday.options[:timeout] = 200
  faraday.options[:open_timeout] = 200
end

/usr/lib/ruby/2.1.0/net/http/response.rb:357:in finish’: buffer error (Zlib::BufError)

Problem:


/usr/lib/ruby/2.1.0/net/http/response.rb:357:in finish': buffer error (Zlib::BufError) from /usr/lib/ruby/2.1.0/net/http/response.rb:357:infinish’
from /usr/lib/ruby/2.1.0/net/http/response.rb:262:in ensure in inflater' from /usr/lib/ruby/2.1.0/net/http/response.rb:262:ininflater’
from /usr/lib/ruby/2.1.0/net/http/response.rb:274:in read_body_0' from /usr/lib/ruby/2.1.0/net/http/response.rb:201:inread_body’
from /usr/lib/ruby/2.1.0/net/http/response.rb:226:in body' from /usr/lib/ruby/2.1.0/net/http/response.rb:163:inreading_body’
from /usr/lib/ruby/2.1.0/net/http.rb:1414:in block in transport_request' from /usr/lib/ruby/2.1.0/net/http.rb:1405:incatch’
from /usr/lib/ruby/2.1.0/net/http.rb:1405:in transport_request' from /usr/lib/ruby/2.1.0/net/http.rb:1378:inrequest’
from /usr/lib/ruby/2.1.0/net/http.rb:1371:in block in request' from /usr/lib/ruby/2.1.0/net/http.rb:853:instart’

Solution:

require ‘faraday’
require ‘excon’
conn = Faraday.new(:url => “http://you.ctrip.com”) do |faraday|
  faraday.request  :url_encoded             # form-encode POST params
   faraday.response :logger                  # log requests to STDOUT
  faraday.adapter  :excon   # make requests with Net::HTTP
end
response = conn.get “/CommunitySite/Activity/Home/IndexList?page=”+$getPageTimes.to_s+”&sorttab=eventstab_publish”  

Ruby + MYSQL 基本操作指南 (一)

1. 获得MYSQL版本信息。


#!/usr/bin/ruby

require 'mysql'


begin
    con = Mysql.new 'localhost', 'user12', ‘34klq’ # mysql用户名及密码
    puts con.get_server_info
    rs = con.query 'SELECT VERSION()'
    puts rs.fetch_row    
    
rescue Mysql::Error => e
    puts e.errno
    puts e.error
    
ensure
    con.close if con
end
2. 罗列所有databases,使用 list_dbs

#!/usr/bin/ruby

require 'mysql'

begin
    con = Mysql.new 'localhost', 'user12', '34klq*'

    con.list_dbs.each do |db|
        puts db
    end
    
rescue Mysql::Error => e
    puts e.errno
    puts e.error
    
ensure
    con.close if con
end

3. 创建一个表格,并存储一些数据进去。

#!/usr/bin/ruby

require 'mysql'

begin
    
    con = Mysql.new 'localhost', 'user12', '34klq*', 'mydb'
   
    con.query("CREATE TABLE IF NOT EXISTS \
        Writers(Id INT PRIMARY KEY AUTO_INCREMENT, Name VARCHAR(25))")
    con.query("INSERT INTO Writers(Name) VALUES('Jack London')")
    con.query("INSERT INTO Writers(Name) VALUES('Honore de Balzac')")
    con.query("INSERT INTO Writers(Name) VALUES('Lion Feuchtwanger')")
    con.query("INSERT INTO Writers(Name) VALUES('Emile Zola')")
    con.query("INSERT INTO Writers(Name) VALUES('Truman Capote')")   
    
rescue Mysql::Error => e
    puts e.errno
    puts e.error
    
ensure
    con.close if con
end
4. 逐行读取数据。

#!/usr/bin/ruby

require "mysql"

begin
    
    con = Mysql.new 'localhost', 'user12', '34klq*', 'mydb'

    rs = con.query("SELECT * FROM Writers")
    n_rows = rs.num_rows
    
    puts "There are #{n_rows} rows in the result set"
    
    n_rows.times do
        puts rs.fetch_row.join("\s")
    end
 
rescue Mysql::Error => e
    puts e.errno
    puts e.error
    
ensure
    con.close if con
end
    rs.each do |row|
        puts row.join("\s")
    end
        
    rs.each_hash do |row|
       puts row['Id'] + " " + row['Name']
    end      













使用Ngrok 进行微信公众平台本地测试

1. Download Ngrok.

https://ngrok.com/
2. 获得auth token.


3. 执行 ngrok

$ ./ngrok -authtoken ltCc2DrvTdRZg+adcC/A 3000

4. 执行后返回结果。

ngrok                                                                                                                                                                   (Ctrl+C to quit)

                                                                                                                                                                                        

Tunnel Status                 online                                                                                                                                                    

Version                       1.7/1.7                                                                                                                                                   

Forwarding                    http://5dadfc45.ngrok.com -> 127.0.0.1:3000                                                                                                               

Forwarding                    https://5dadfc45.ngrok.com -> 127.0.0.1:3000                                                                                                              

Web Interface                 127.0.0.1:4040                                                                                                                                            

# Conn                        14                                                                                                                                                        

Avg Conn Time                 273.70ms