Created
July 9, 2011 12:20
-
-
Save kinopyo/1073555 to your computer and use it in GitHub Desktop.
All source code snippets used in my blog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery -> | |
alert "hello" | |
$ -> | |
alert "hello" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// see fql docs: | |
// http://developers.facebook.com/docs/reference/fql/link_stat/ | |
$url_list = array( | |
'http://example.com/url1.html', | |
'http://example.com/url2.html', | |
); | |
$url_string = '("'. implode('","', $url_list). '")'; | |
// just like sql syntax | |
$fql = "select url, like_count from link_stat where url in".$url_string; | |
// remember to encode it | |
$url = "https://api.facebook.com/method/fql.query?format=json&query=".urlencode($fql); | |
$ch = curl_init($url); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
$data = curl_exec($ch); | |
curl_close($ch); | |
$json = json_decode($data, true); | |
$ret = array(); | |
print_r($json); | |
// result example | |
/* | |
Array | |
( | |
[0] => Array | |
( | |
[url] => http://example.com/url1.html | |
[like_count] => 10 | |
) | |
[1] => Array | |
( | |
[url] => http://example.com/url2.html | |
[like_count] => 20 | |
) | |
) | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# HOW TO CREATE USER AND GROUP, GRANT ROOT PRIVILEGE | |
## PART1 CREATE USER | |
# assume user1,user2,user3 is already created | |
# and they are in wheel group, which can execute as root | |
# and here we're creating a new user named:new_user | |
# create new user | |
useradd new_user | |
# set password for it, in the prompt | |
passwd new_user | |
# you can now login as new_user | |
# but when you run sudo you may get this error | |
new_user is not in the sudoers file. This incident will be reported. | |
# so you need to set the privilege to new_user | |
## PART2 GRANT SUDO PRIVILEGE | |
## 1. BY CHANGE USER GROUP | |
$ visudo | |
############ in visudo ############ | |
## Allows people in group wheel to run all commands | |
# %wheel ALL=(ALL) ALL | |
## Same thing without a password | |
%wheel ALL=(ALL) NOPASSWD: ALL | |
############ in visudo ############ | |
# It means people in wheel group can run all commands, without password | |
# Below we're adding the new user called new_user to the wheel group. | |
# list users and user id, group id, home directory, login shell, etc. | |
$ cat /etc/passwd | |
... | |
wheel:x:10:root,user1,user2,user3 | |
... | |
$ id -a [user name] | |
uid=503(user3) gid=503(init_group) 所属グループ=503(init_group),10(wheel) | |
$ usermod -g init_group -G init_group,wheel new_user | |
$ id -a new_user | |
uid=506(new_user) gid=503(init_group) 所属グループ=503(init_group),10(wheel) | |
## 2. BY ADD USER TO sudoers FILE. | |
$ visudo | |
############ in visudo ############ | |
new_user ALL=(ALL) NOPASSWD: ALL | |
############ in visudo ############ | |
## some handy commands | |
# show user list | |
$ cut -d: -f1 /etc/passwd | |
root | |
bin | |
daemon | |
adm | |
... | |
# TODO | |
# what is the difference between -g(initial_group) and -G(group) | |
# Reference | |
# (ENG) http://superuser.com/questions/120340/user-not-in-the-sudoers-file-this-incident-will-be-reported | |
# (JPN) http://kazmax.zpp.jp/linux_beginner/usermod.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* 指定したプロセス数で並列処理を実行する | |
* | |
* @param array $url_list URLの配列 | |
* @param boolean $url_as_key 結果配列を返すときに、urlをキーにする | |
* @param int $timeout タイムアウト秒数 0だと無制限 | |
* @return array 結果配列 | |
*/ | |
function execute($url_list, $url_as_key = false, $timeout=0) { | |
// set your process number | |
$process = 5; | |
$is_over_process = false; | |
if ($process < count($url_list)) { | |
// chunk url list / process number* | |
$url_chunk = array_chunk($url_list, $process); | |
$is_over_process = true; | |
} | |
$ret = array(); | |
if ($is_over_process && !empty($url_chunk)) { | |
foreach ($url_chunk as $key => $url_list) { | |
echo "chunk start:{$key}\n"; | |
$res = fetch_multi_url($url_list, $url_as_key, $timeout); | |
if (!empty($res)) { | |
$ret = array_merge($ret, $res); | |
} else { | |
continue; | |
} | |
} | |
} else if (!$is_over_process && !empty($url_list)){ | |
$ret = fetch_multi_url($url_list, $url_as_key, $timeout); | |
} else { | |
echo "url invalid::"; | |
} | |
return $ret; | |
} | |
/** | |
* curl_multi_execの並列処理 | |
* ほぼboilerplate | |
* | |
* @param array $url_list URLの配列 | |
* @param boolean $url_as_key 結果配列を返すときに、urlをキーにする | |
* @param int $timeout タイムアウト秒数 0だと無制限 | |
* @return array 結果配列 | |
*/ | |
function fetch_multi_url($url_list, $url_as_key, $timeout) { | |
$mh = curl_multi_init(); | |
foreach ($url_list as $i => $url) { | |
$ch[$i] = curl_init($url); | |
curl_setopt($ch[$i],CURLOPT_RETURNTRANSFER,1); | |
//タイムアウト | |
if ($timeout){ | |
curl_setopt($ch[$i],CURLOPT_TIMEOUT,$timeout); | |
} | |
curl_multi_add_handle($mh,$ch[$i]); | |
} | |
//URLを取得 | |
//すべて取得するまでループ | |
$active = null; | |
do { | |
$mrc = curl_multi_exec($mh,$active); | |
} while ($mrc == CURLM_CALL_MULTI_PERFORM); | |
while ($active and $mrc == CURLM_OK) { | |
if (curl_multi_select($mh) != -1) { | |
do { | |
$mrc = curl_multi_exec($mh,$active); | |
} while ($mrc == CURLM_CALL_MULTI_PERFORM); | |
} | |
} | |
if ($mrc != CURLM_OK) { | |
echo '読み込みエラーが発生しました:'.$mrc; | |
} | |
//ソースコードを取得 | |
$res = array(); | |
foreach ($url_list as $i => $url) { | |
if (($err = curl_error($ch[$i])) == '') { | |
// url_as_keyがtrueの場合、urlをキーとして格納 | |
if ($url_as_key) { | |
$res[$url] = curl_multi_getcontent($ch[$i]); | |
// そうでない場合は、ただ配列に入れる | |
} else { | |
$res[$i] = curl_multi_getcontent($ch[$i]); | |
} | |
} else { | |
echo '取得に失敗しました:'.$url_list[$i].'<br />'; | |
} | |
curl_multi_remove_handle($mh,$ch[$i]); | |
curl_close($ch[$i]); | |
} | |
curl_multi_close($mh); | |
return $res; | |
} | |
// 並列実行したいurl list | |
$url_list = array( | |
"http://www.kinopyo.com/blog/ipad-2-not-charging-when-connected-to-pc-usb", | |
"http://www.kinopyo.com/blog/the-first-app-i-installed-to-ipad2", | |
"http://www.kinopyo.com/blog/chrome-warn-before-quitting-with-command-q-in-mac", | |
"http://www.kinopyo.com/blog/reply-to-all-always-in-gmail", | |
"http://www.kinopyo.com/blog/lion-fullscreen-shortcut-key-conflict-with-evernote-client", | |
"http://www.kinopyo.com/blog/how-to-set-gesture-for-chrome-to-swipe-back-and-forth-in-lion", | |
"http://www.kinopyo.com/blog/3-free-ebooks-for-study-coffeescript" | |
); | |
// start time | |
$start_time = microtime(true); | |
// execute | |
$res = execute($url_list, true); | |
// execute time | |
$time = microtime(true) - $start_time; | |
// play with the result | |
// here I just get the page title | |
$titles = array(); | |
foreach ($res as $url => $html) { | |
preg_match('{<title>(.*)</title>}',$html, $match_title); | |
$titles[$url] = $match_title[1]; | |
} | |
echo "Result:\n"; | |
echo "time:{$time} sec\n"; | |
print_r($titles); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# stop it first if it's running, | |
# or you will get a warning like: | |
# the NTP socket is in use, exiting | |
$ /etc/init.d/ntpd stop | |
# ntp.nict.jp: 日本標準時プロジェクト 公開NTP | |
$ ntpdate ntp.nict.jp | |
19 Aug 15:30:18 ntpdate[27156]: step time server 133.243.238.243 offset -856.532277 sec | |
# start ntp again | |
$ /etc/init.d/ntpd start | |
# confirm | |
$ date | |
2011年 8月 19日 金曜日 15:32:15 JST |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# update rvm | |
$ rvm get head | |
# or older versions such as 1.0.0 | |
$ rvm update --head | |
# you need to run reload to reflect the update | |
$ rvm reload | |
$ rvm -v | |
rvm 1.7.2 by Wayne E. Seguin (wayneeseguin@gmail.com) [https://rvm.beginrescueend.com/] | |
# install ruby 1.9.2.p290 | |
$ rvm install 1.9.2-p290 | |
... | |
$ rvm list | |
ruby-1.9.2-p0 | |
ruby-1.9.2-p180 | |
# you need to replace the p0 based on your environment | |
$ rvm upgrade ruby-1.9.2-p0 ruby-1.9.2-p290 | |
yes | |
yes | |
yes ... | |
Upgrade complete! | |
$ rvm list | |
ruby-1.9.2-p290 [ x86_64 ] | |
$ rvm --default use 1.9.2 | |
$ ruby -v | |
ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin10.8.0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment