1

OS: Amazon Linux

I have a Ruby script that connects to a site, then it searches with an XPath request for a div block where is the stats counter I want to parse.

Then it compares the number from the site with the current value in the database, if the number has increased it sends me an email.

The problem is that, then I run the script from the current directory it works.

  1. The script parses the block of text which contains a value.
  2. I extract the value with Regex like this (/\d/) ...

But when it the script executes by crontab it gets some strange value like ...041704300440043504330438044104420440043804400430432043004304304304304304404370430432043004420435043043504390447043504400435043704320430044804430430...

I don't know how to debug it because, when I run the script manually it works, but fails with strange value when executed by crontab.

The text in the site is russian, encoded with Windows-1251. Maybe there is something wrong with that. I have set # encoding: utf-8, in the .rb file.

4

3 に答える 3

1

これは環境の問題である可能性があり、パスの不良などが含まれる可能性があります。コマンドラインからcrontabで起動した場合の環境とENVを比較できます。

試す:

ruby -rpp -e 'pp ENV' > /tmp/crontab_env.out

crontabから、次に:

ruby -rpp -e 'pp ENV' > /tmp/cmd_env.out

コマンドラインから、次に:

vimdiff /tmp/*env.out

または通常のエディターを使用します。

于 2013-01-18T04:17:14.143 に答える
0

RVMを使用している場合は、通常、インタラクティブシェルでのみ使用できることに注意してください。このトピック専用のRVMマニュアルのセクション全体があります:RVM:Rubyバージョンマネージャー-RVMでのCronの使用

これは、Gemsを含む間違ったRubyバージョンが使用されているという問題である可能性があります。スクリプトでhashbang行を削除し、crontabで次のように呼び出してみてください。

1 0  * * * /usr/local/rvm/bin/ruby-1.9.3-p362 /path/to/script.rb

これにより、適切な環境にRubyバイナリがロードされていることを確認できます。

実際の問題が、非対話型スクリプトでRVMを使用できないことである場合は、さらに一歩進んで、RVMをロードするときにシェルが行うことを実行することもできます。右にスクロールすると、これは大きな線になります。

1 0 * * * /bin/bash -l -c 'source "$HOME/.rvm/scripts/rvm" && rvm use 1.9.3-p362 && ruby /path/to/script.rb
于 2013-01-17T22:07:53.827 に答える
0

cronジョブの問題は、多くの場合、環境が間違っていることが原因で発生します。~/.profileスクリプトはおそらく、インタラクティブシェルを(または同様に)起動するときに設定される環境変数に依存します~/.bashrcが、プログラムがcronによって直接起動されるときは依存しません。

と入力して、環境変数とその現在の値のリストを取得しますenv。単に実行するcronジョブを追加しますenv。出力を比較し、原因が見つかるまでチップを取り除きます。

私はLANG、友達が始めるのに良い場所だと思います。と入力して、言語およびエンコーディング関連の環境変数のリストを取得しますlocale

于 2013-01-17T23:31:15.190 に答える