2012年10月25日木曜日

WEBアプリケーションサーバの構築(6) crontab

1.Shell Scriptエラー処理 前回のツールを動作させる前提として「img」ディレクトリが必要だが,もし存在しないとうまく動作しない.
そこで,「img」ディレクトリの存在有無をチェックし,もし存在しない場合は,ディレクトリを作成するようにツールを編集する.
get_image2.sh
#!/bin/bash

for user in `ls /home`
do
    id $user > /dev/null 2>&1
    if [ $? -eq 0 ] && [ ! -d /home/$user/public_html ]; then
        mkdir -p /home/$user/public_html
        chown $user. /home/$user/public_html
        chmod 711 /home/$user
        echo $user
    fi
done


2.crontabの利用 逆方向も試してみよう
WEBから受け取った値...天気予報の予報値や,特定サイトの値...を,
シェルスクリプトでDBサーバに自動登録.
さらにcrontabを使って,定期的に自動実行.
登録された値はHTMLファイルで閲覧可能なものを作成.
#goo天気の場合
#元ファイル取得
# wget http://weather.goo.ne.jp/area/4610.rdf
#時刻
# grep lastBuildDate 4610.rdf | sed -e "s/\(.*\)<\/lastBuildDate>/\1/g"
#天気
# grep description 4610.rdf | sed -e "s/\(.*\)<\/description>/\1/g"

#Yahoo天気の場合
#元ファイル取得
 wget http://rss.weather.yahoo.co.jp/rss/days/4410.xml
#時刻
 grep lastBuildDate 4410.xml | sed -e "s/\(.*\)<\/lastBuildDate>/\1/g"
#現在の天気と気温を抽出
 grep -n description 4410.xml | grep ^13 | sed -e "s/\(.*\)<\/description>/\1/" | sed "s/\s//g" | sed "s/^.*-//g" | sed "s/\/.*$//g"

1.SQL文を用意
vi temp.sql
 insert into temp (temp) values (10);

2.1で作成したファイルを用いてMySQLへ値流し込み
mysql -u root -s test < temp.sql

3.temp.sqlの値を自動的に更新するシェルスクリプトを作成
temp.sql
 #!/bin/sh
 echo "insert into temp (temp) values (10);" > temp.sql
 /usr/bin/mysql -u root -s test < temp.sql
 exit 0

chmod u+x temp.sql
./temp.sql

4.temp.sqlの中身を自動的に変更する.
temp.sql
 !/bin/sh
 wget http://rss.weather.yahoo.co.jp/rss/days/4410.xml
 str_temp=`grep -n description 4410.xml | grep ^13 | sed -e "s/\(.*\)<\/description>/\1/" | sed "s/\s//g" | sed "s/^.*-//g" | sed "s/\/.*$//g"`
 echo 'insert into temp (temp) values ("'$str_temp'");' > temp.sql
 /usr/bin/mysql -u root -s test < temp.sql
 rm 4410.xml
 exit 0

5.対象とする地域を複数とする.
ファイル名の下2桁目が可変 ---> 4410.xml/4420.xml/4430.xml ... ---> 44*0.xmlの*が1~9まで変化

0 件のコメント: