.bashrc

bashrcとは

bashは「シェルの種類のひとつで、shをパワーアップしたシェル」。
(ログインした後に画面上から)bashを起動したときに読み込まれる設定ファイル
シェルはsh、bash、csh、ksh、tcsh、zshなど

vi ~/.bashrc

# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# User specific aliases and functions

ログイン時に「.bashrc」は読み込まれません。
ログイン時には「.bash_profile」というファイルが読み込まれます。

 .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH
source ~/.bashrc

source ~/.bashrcとすると、.bashrcを読み込む。もちろん、source ~/.bash_profileで、bash_profileを読み込む

centosでgrepする

まず、vagrantにログインして、適当にディレクトリを作ります。

[vagrant@localhost ~]$ mkdir grep
[vagrant@localhost ~]$ cd grep
[vagrant@localhost grep]$ git version
git version 2.2.1

git hubから適当に機械学習のコードをgit cloneしてきます。

[vagrant@localhost grep]$ git clone https://github.com/nyk510/machine-learning.git
Cloning into 'machine-learning'...
remote: Counting objects: 112, done.
Receiving objects: 100% (112/112), 2.49 MiB | 673.00 KiB/s, done.
remote: Total 112 (delta 0), reused 0 (delta 0), pack-reused 112
Resolving deltas: 100% (55/55), done.
[vagrant@localhost grep]$ ls
machine-learning
[vagrant@localhost machine-learning]$ ls
black_scholes.py  figures  gibbs_sampling.py  prml  readme.md  requirements.txt

では、適当にblack_scholes.pyでSIGMAが使われている箇所を抜き出しています。

[vagrant@localhost machine-learning]$ grep SIGMA black_scholes.py
SIGMA = 0.30
d = -(np.math.log(S_0 / K) + ((r - SIGMA * SIGMA / 2.) * T)) / (SIGMA * np.power(T, 0.5))
V_c = S_0 * stats.norm.cdf(-d + SIGMA * np.power(T, .5)) - K * np.exp(-r * T) * stats.norm.cdf(-d)
V_p = K * np.exp(-r * (T - t)) * stats.norm.cdf(d) - S_0 * stats.norm.cdf(d - SIGMA * np.power(T - t, 0.5))
    s_j_t = S_0 * np.exp((r - SIGMA * SIGMA / 2.) * T + SIGMA * rand_n * np.power(T, 0.5))
    s_j_t = S_0 * np.exp((r - SIGMA * SIGMA / 2.) * T + SIGMA * rand_n * np.power(T, 0.5))

行番号をつけたい時は grepに -nをつける

[vagrant@localhost machine-learning]$ grep -n SIGMA black_scholes.py
18:SIGMA = 0.30
22:d = -(np.math.log(S_0 / K) + ((r - SIGMA * SIGMA / 2.) * T)) / (SIGMA * np.po      wer(T, 0.5))
23:V_c = S_0 * stats.norm.cdf(-d + SIGMA * np.power(T, .5)) - K * np.exp(-r * T)       * stats.norm.cdf(-d)
24:V_p = K * np.exp(-r * (T - t)) * stats.norm.cdf(d) - S_0 * stats.norm.cdf(d -       SIGMA * np.power(T - t, 0.5))
29:    s_j_t = S_0 * np.exp((r - SIGMA * SIGMA / 2.) * T + SIGMA * rand_n * np.p      ower(T, 0.5))
35:    s_j_t = S_0 * np.exp((r - SIGMA * SIGMA / 2.) * T + SIGMA * rand_n * np.p      ower(T, 0.5))

ファイル名のみ

[vagrant@localhost machine-learning]$ grep -l SIGMA *
black_scholes.py
grep: figures: ディレクトリです
grep: prml: ディレクトリです

ファイル名と行番号は”-rn”

[vagrant@localhost machine-learning]$ grep -rn import *.py
black_scholes.py:9:import numpy as np
black_scholes.py:10:import matplotlib.pyplot as plt
black_scholes.py:11:import scipy.stats as stats
gibbs_sampling.py:7:import numpy as np
gibbs_sampling.py:8:import matplotlib.pyplot as plt
gibbs_sampling.py:9:import scipy.stats as stats

あーこれこれ、やりたかったの。

vagrantにxpdfを入れる

yumコマンドで入れます。
[pre]
yum install xpdf
[/pre]

依存性関連をインストールしました:
desktop-file-utils.x86_64 0:0.15-9.el6
lcms-libs.x86_64 0:1.19-1.el6
libXmu.x86_64 0:1.1.1-2.el6
libXp.x86_64 0:1.0.2-2.1.el6
libpaper.x86_64 0:1.1.23-6.1.el6
openjpeg-libs.x86_64 0:1.3-16.el6_8
openmotif.x86_64 0:2.3.3-9.el6
poppler.x86_64 0:0.12.4-12.el6_9
poppler-data.noarch 0:0.4.0-1.el6
poppler-utils.x86_64 0:0.12.4-12.el6_9
urw-fonts.noarch 0:2.4-11.el6
xdg-utils.noarch 0:1.0.2-17.20091016cvs.el6
xorg-x11-fonts-ISO8859-1-100dpi.noarch 0:7.2-11.el6
xorg-x11-fonts-ISO8859-1-75dpi.noarch 0:7.2-11.el6

完了しました!

etcのディレクトリの xpdfrcを編集します。

# Choose a text encoding for copy-and-paste and for pdftotext output.
# The Latin1, ASCII7, and UTF-8 encodings are built into Xpdf. Other
# encodings are available in the language support packages.

textEncoding UTF-8

コマンドラインから、pdftotext origin.pdf create.txt でtxtファイルを作成する。

[vagrant@localhost translator]$ pdftotext boj.pdf boj.txt

うおおおおおおおおおおおおおおおおおお
テキストファイルになってる!!!!!!!!!!!
ヘッダーの「日本銀行」とフッターの「以上」が改行されているが、他は問題なさそう。すげー

xpdfをazureのvmに入れられるのかわからんが、まずはvagrantで構築していきましょう。
pdftotextはcrontabでやるにしても、コマンドラインではなく、phpファイルから実行できるんだろうか?

cronでgunzipを実行する

crontabでgunzipと書くだけです。

20 * * * * root cd /home/vagrant/cookie/cron; gunzip GeoLite2-Country.mmdb.gz

ほお~

sudo cat /var/log/cron
Apr 26 22:20:02 localhost CROND[17108]: (root) CMD (cd /home/vagrant/cookie/cron; gunzip GeoLite2-Country.mmdb.gz)

無償版は毎月3日に更新されるそう(真偽は不明です)なので、日本との時差を考慮して、毎月5日の1時15分にwgetして、1時20分にgunzipするよう設定します。

15 1 5 * * root cd /home/vagrant/cookie/cron; /usr/bin/wget "http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.mmdb.gz"
15 1 5 * * root cd /home/vagrant/cookie/cron; /usr/bin/wget "http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz"
20 1 5 * * root cd /home/vagrant/cookie/cron; gunzip GeoLite2-Country.mmdb.gz
20 1 5 * * root cd /home/vagrant/cookie/cron; gunzip GeoLite2-City.mmdb.gz

geolite側はこれでOK
次はmongodbに入れる前に、phpでipから、continent, country, cityを取得して配列にpushするところをやりたいと思います。

cronでwgetを実行する

wget:ノンインタラクティブなダウンローダー

wgetがどこにあるか確認します。

/usr/bin/wget

bin:Binary codeのこと、実行可能プログラム置き場
/bin:FHSによって定められたシングルユーザを含めたどのユーザでも使えるコマンド
/usr/bin:ユーザーが一般的に使用するコマンド

続いて、crontabを編集します。

sudo vi /etc/crontab

任意のディレクトリ(ここでは/home/vagrant/cookie/cron)に、wgetでdownloadしたいとすると、まず、cdで移動する必要があります。そのため、以下のように書きます。
root cd で移動して、/usr/bin/wgetで、”http://hogehoge”をダウンロード
※ここでは、テスト用に毎時55分にwgetする

55 * * * * root cd /home/vagrant/cookie/cron; /usr/bin/wget "http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.mmdb.gz"
/etc/rc.d/init.d/crond start

編集後、crond start
/etc/rc.d/init.d/crond start

結果
->取れてる!!!

sudo cat /var/log/cron でログも確認

Apr 26 21:55:01 localhost CROND[17001]: (root) CMD (cd /home/vagrant/cookie/cron; /usr/bin/wget "http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.mmdb.gz")

OK牧場!

2000万ファイルの作成について

東京都の電話番号の全てのパターンをつくりたいとする

03-XXXX-YYYY

市外局番:03
市内局番:3100~6999(2264件)
加入者番号:0000~9999(9999件)

仮に全て、ファイルを作ったとすると、2264万ファイル作成することになる。

「Linuxの場合、一つのディレクトリの配下に10000個から15000個程度のファイルを作成することが出来るが、実際には5000個もファイルを作成するとディスクI/Oの性能が落ち、ファイル読み書きのパフォーマンスが落ちる」、とのこと。

vagrant・centosからsendmailでメールを送信する

まず完成版から

vagrantのコマンドラインで以下のように打ちます

[vagrant@localhost postfix]$ sendmail myaccount@gmail.com
Subject: test
test content
.

自分のgmailアカウント
vagrantからのメールが届いてますね。

ここまで来るのに5~6時間かかりました。。。

まず、postfixをインストール後、

sudo yum -y install cyrus-sasl-md5 cyrus-sasl-plain

そして、/etc/postfix/main.cfファイルの編集

# 116行目 inet_interfaces = localhostを編集
inet_interfaces = all

# 119行目 inet_protocols = allを編集
inet_protocols = ipv4

# 318行目 以下追加
relayhost = [smtp.gmail.com]:587
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_tls_security_options = noanonymous
smtp_sasl_mechanism_filter = plain
smtp_sasl_security_options = noanonymous
smtp_tls_CApath = /etc/pki/tls/certs/ca-bundle.crt

因みに、main.cfの「318行目」は末尾ではありませんのでご注意を。

続いて、16桁のGmailのアプリパスワードを生成します。
https://secuirty.google.com/settings/security/apppasswords

その後、/etc/postfix/に、sasl_passwdファイルをvimで作成します。パスワードはつくったアプリパスワードを入力。

[smtp.gmail.com]:587 example@gmail.com:app_password(16桁)

ここで、passwordにgmailの普通のパスワードを入れると、authentification errorになるのでご注意。

その後、postfixのディレクトリからコマンドラインで

postmap sasl_passwd

として、sasl_passwd.dbを作成します。

そして

sudo service postfix restart

また、gmailの設定で、安全性の低いアプリ(失礼な!)の設定を変更する必要があります。

これでsendmailが使えるようになります。あて先は、gmail以外でもOKです。送信元がvagrantになってました。カッコいい!

[vagrant@localhost postfix]$ sendmail myaccount@gmail.com
Subject: test
test content
.

受信されてないようでしたらログを確認。

sudo cat /var/log/maillog

smtp結構複雑ですね。
別のvagrant環境立ち上げて試すなど、迷走しまくってました。。。

sudo yum -y install postfix

postfixを入れます。

  更新                    : 2:postfix-2.6.6-8.el6.x86_64                    1/2
  整理中                  : 2:postfix-2.6.6-6.el6_7.1.x86_64                2/2
  Verifying               : 2:postfix-2.6.6-8.el6.x86_64                    1/2
  Verifying               : 2:postfix-2.6.6-6.el6_7.1.x86_64                2/2

更新:
  postfix.x86_64 2:2.6.6-8.el6

完了しました!
[vagrant@localhost /]$ sudo alternatives --config mta

2 プログラムがあり 'mta' を提供します。

  選択       コマンド
-----------------------------------------------
   1           /usr/sbin/sendmail.postfix
*+ 2           /usr/sbin/sendmail.sendmail

Enter を押して現在の選択 [+] を保持するか、選択番号を入力します:1
[vagrant@localhost /]$ sudo chkconfig --add postfix
[vagrant@localhost /]$ sudo chkconfig postfix on
[vagrant@localhost /]$ chkconfig --list postfix
postfix         0:off   1:off   2:on    3:on    4:on    5:on    6:off

vagrantでのsendmail

SMTPサーバーはメールを相手のメールサーバーまで届けるサーバー

PHPで以下のように書いて

$to = "hoge@gmail.com";
$subject = "TEST MAIL";
$message = "Hello!\r\nThis is TEST MAIL.";
$headers = "From: hoge@hoge.com";
 
mail($to, $subject, $message, $headers);

echo "finish!";

以下のようにエラーが出たので

sendmail: fatal: parameter inet_interfaces: no local interface found for ::1

/etc/hostsを修正し

[vagrant@localhost mail]$ vi /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
#::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

それでも全然finishしないので、SMTPサーバーを調べていたら、
>Postfix 2.10.1 ゲストOS上で稼働するMTA(SMTP)サーバー
>Dovecot 2.2.10 ゲストOS上で稼働するPOP3,IMAPサーバー
なるほど、Postfixをインストールしないといけないのね。