前提・実現したいこと
リンク先:vdeep.net様と同じ事をしようとしています。毎分ごとにhello.rbを動かしたいのですが動きません。この場合何が原因と考えられるでしょうか。ご教授ください。
該当のソースコード
Hello.rb(vdeep.netより拝借)
Ruby
1require "date" 2 3d = DateTime.now 4File.open('hello-ruby.txt', 'w') do |file| 5 file.puts(d.strftime("%Y%m%d %H%M%S")) 6end
補足情報(FW/ツールのバージョンなど)
conohaVPSにてUbuntu18.04.02を使用
vpsuser@ipaddress:~$ crontab -l # Edit this file to introduce tasks to be run by cron. # # Each task to run has to be defined through a single line # indicating with different fields when the task will be run # and what command to run for the task # # To define the time you can provide concrete values for # minute (m), hour (h), day of month (dom), month (mon), # and day of week (dow) or use '*' in these fields (for 'any').# # Notice that tasks will be started based on the cron's system # daemon's notion of time and timezones. # # Output of the crontab jobs (including errors) is sent through # email to the user the crontab file belongs to (unless redirected). # # For example, you can run a backup of all your user accounts # at 5 a.m every week with: # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/ # # For more information see the manual pages of crontab(5) and cron(8) # # m h dom mon dow command PATH=$PATH:/home/vpsuser/.rbenv/shims * * * * * ruby ~/hello.rb
vpsuser@ipaddress:~$ ls -l -rwx------ 1 vpsuser vpsuser 121 Jul 14 15:36 hello.rb vpsuser@ipaddress:~$ which ruby /home/vpsuser/.rbenv/shims/ruby vpsuser@ipaddress:~$ ruby -v ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux] vpsuser@ipaddress:~$ crontab -l | file - /dev/stdin: ASCII text vpsuser@ipaddress:/etc$ ls cron* crontab cron.d: mdadm popularity-contest cron.daily: apport aptitude dpkg man-db mlocate passwd ubuntu-advantage-tools apt-compat bsdmainutils logrotate mdadm ntp popularity-contest update-notifier-common cron.hourly: cron.monthly: cron.weekly: man-db update-notifier-common
追記
コメントの指摘で、ruby,スクリプト共に絶対パスで指定しましたが、やっぱりうまくいきません。「PATH=$PATH:~」の行をなくしても同様です。
PATH=$PATH:/home/vpsuser/.rbenv/shims:/usr/bin * * * * * /home/vpsuser/.rbenv/shims/ruby /home/vpsuser/hello.rb >> /var/log/hello.log 2>&1
まずは、
rubyも絶対パスで、スクリプトも~の指定ではなく、絶対パスで指定しましょう。
追記いたしました。
> 動きません。
具体的な現象を書いてください。
コメントありがとうございます。hello-ruby.txtという日時を格納したテキストファイルを作成しようとするプログラムを定期的に起動させようとしていますが、何らかの原因でテキストファイルが作成できないという事です。
したいことはプログラムから分かりますが、「具体的な現象」というのは、起こっていないことじゃなくて、起こっていることを書いて欲しかったのです。
私の目からは何も起こっていないように見えます。
エラーメッセージが出ていることを、「動かない」と書く人も多いので。
プログラムが作るはずのファイルが作られていないこと以外は何も確認できていないと言うことですかね。
エラーメッセージが出ていれば、メール設定がちゃんとしていればrootからメールが届いているはずですが、届いていませんか?
ls -l $MAIL
の結果は?
コメントありがとうございます。crontab -eのインストール時にはエラー等はありませんでした。解決前の状況ではログを出力するhello.logが生成できない状況でした。解決後にですが、hello.rbのあるフォルダ(ホームフォルダ)で「ls -l $MAIL」とすると「ls: cannot access '%MAIL': No such file or directory」でした。
> '%MAIL':
タイプミスしていませんか?
すみません。結果が違うので多分タイプミスです。
「vpsuser@ipaddress:~$ ls -l $MAIL」とすると
「ls: cannot access '/var/mail/vpsuser': No such file or directory」でした。
cronをよく知らないので「メール設定」なる存在さえも存じ上げていませんでした。今後ログの出力等では解決できないようであれば探ってみたいと思います。
そういう結果ならメールは来ていませんね。メール設定が出来ていないと思われます。
cronを使うなら、メール設定しておかないと不便です。
ありがとうございます。心得ておきます。
回答2件
あなたの回答
tips
プレビュー