ruby 脚本 cron 不工作
ruby script cron not working
我在 ~/scri.rb
中有一个 ruby 脚本
File.open('~/newfile.txt', 'a+') do |f|
f << "hi..\n"
end
我有一个这样的 cron 选项卡
* * * * * bash -lc 'ruby ~/scri.rb' >> /var/log/syslog
当我在 /var/log/syslog 中查看日志时,我看到了这样的条目
Sep 8 14:49:01 user1acer CRON[26063]: (user1) CMD (bash -lc 'ruby ~/scri.rb' >> /var/log/syslog)
Sep 8 14:50:01 user1acer CRON[27502]: (user1) CMD (bash -lc 'ruby ~/scri.rb' >> /var/log/syslog)
Sep 8 14:51:01 user1acer CRON[29006]: (user1) CMD (bash -lc 'ruby ~/scri.rb' >> /var/log/syslog)
Sep 8 14:52:01 user1acer CRON[30425]: (user1) CMD (bash -lc 'ruby ~/scri.rb' >> /var/log/syslog)
Sep 8 14:53:01 user1acer CRON[31846]: (user1) CMD (bash -lc 'ruby ~/scri.rb' >> /var/log/syslog)
但是当我检查 newfile.txt 文件时,我什么也没看到。
我是不是遗漏了什么?
实际上,这与 cron 无关。 ~
是shell的一个特征,Ruby不知道是什么。 Ruby 中只有 一些 方法处理 ~
,例如File::expand_path
。因此,Ruby 实际上是在尝试打开当前工作目录中名为 ~
的目录中名为 newfile.txt
的文件。自己尝试一下:在你的主目录下创建一个名为 ~
的空目录,果然,1 分钟后,你应该会发现一个名为 newfile.txt
的新文件,其中的内容为 hi..
.
有几种方法可以解决这个问题,我将由您决定使用哪一种:
File.open(File.expand_path('~/newfile.txt'), 'a') do |f| end
File.open(File.join(Dir.home, 'newfile.txt'), 'a') do |f| end
[注意:如果只想追加到文件末尾,不移动也不读取,a
就够了,不需要a+
.]
请参阅 File::expand_path
的文档(粗体 强调我的):
expand_path(file_name [, dir_string] )
→ abs_file_name
Converts a pathname to an absolute pathname. Relative paths are referenced from the current working directory of the process unless dir_string
is given, in which case it will be used as the starting point. The given pathname may start with a “~”, which expands to the process owner’s home directory (the environment variable HOME
must be set correctly). “~user” expands to the named user’s home directory.
File.open('~/newfile.txt', 'a+') do |f|
f << "hi..\n"
end
我有一个这样的 cron 选项卡
* * * * * bash -lc 'ruby ~/scri.rb' >> /var/log/syslog
当我在 /var/log/syslog 中查看日志时,我看到了这样的条目
Sep 8 14:49:01 user1acer CRON[26063]: (user1) CMD (bash -lc 'ruby ~/scri.rb' >> /var/log/syslog)
Sep 8 14:50:01 user1acer CRON[27502]: (user1) CMD (bash -lc 'ruby ~/scri.rb' >> /var/log/syslog)
Sep 8 14:51:01 user1acer CRON[29006]: (user1) CMD (bash -lc 'ruby ~/scri.rb' >> /var/log/syslog)
Sep 8 14:52:01 user1acer CRON[30425]: (user1) CMD (bash -lc 'ruby ~/scri.rb' >> /var/log/syslog)
Sep 8 14:53:01 user1acer CRON[31846]: (user1) CMD (bash -lc 'ruby ~/scri.rb' >> /var/log/syslog)
但是当我检查 newfile.txt 文件时,我什么也没看到。
我是不是遗漏了什么?
实际上,这与 cron 无关。 ~
是shell的一个特征,Ruby不知道是什么。 Ruby 中只有 一些 方法处理 ~
,例如File::expand_path
。因此,Ruby 实际上是在尝试打开当前工作目录中名为 ~
的目录中名为 newfile.txt
的文件。自己尝试一下:在你的主目录下创建一个名为 ~
的空目录,果然,1 分钟后,你应该会发现一个名为 newfile.txt
的新文件,其中的内容为 hi..
.
有几种方法可以解决这个问题,我将由您决定使用哪一种:
File.open(File.expand_path('~/newfile.txt'), 'a') do |f| end
File.open(File.join(Dir.home, 'newfile.txt'), 'a') do |f| end
[注意:如果只想追加到文件末尾,不移动也不读取,a
就够了,不需要a+
.]
请参阅 File::expand_path
的文档(粗体 强调我的):
expand_path(file_name [, dir_string] )
→abs_file_name
Converts a pathname to an absolute pathname. Relative paths are referenced from the current working directory of the process unless
dir_string
is given, in which case it will be used as the starting point. The given pathname may start with a “~”, which expands to the process owner’s home directory (the environment variableHOME
must be set correctly). “~user” expands to the named user’s home directory.