【问题标题】:Ruby gives “No such file or directory - text.txt (Errno::ENOENT)” errorRuby 给出“没有这样的文件或目录 - text.txt (Errno::ENOENT)”错误
【发布时间】:2013-08-21 23:07:18
【问题描述】:

当我试图运行一个 ruby​​ 应用程序时,它给出了以下错误,

有人能解释一下吗?

ruby logger.rb
/home/swapnasa/Downloads/irclogger-master
logger.rb:14:in `read': No such file or directory - ./tmp/logger.pid (Errno::ENOENT)
        from logger.rb:14:in `<main>'

代码在这里:

#!/usr/bin/env ruby

puts Dir.pwd

$: << File.join(File.dirname(__FILE__), 'lib')

require 'irclogger'
require 'irclogger/cinch_plugin'
require 'redis'

pidfile = File.join(File.dirname(__FILE__), 'tmp', 'logger.pid')

begin
  old_pid = File.read(pidfile).to_i
  Process.kill 0, old_pid

  raise "An existing logger process is running with pid #{old_pid}. Refusing to start"
rescue Errno::ESRCH
end

File.open(pidfile, 'w') do |f|
  f.write Process.pid
end

bot = Cinch::Bot.new do
  configure do |c|
    c.server   = Config['server']
    c.channels = Config['channels']
    c.user     = Config['username']
    c.nick     = Config['nickname']
    c.realname = Config['realname']

    # cinch, oh god why?!
    c.plugins.plugins = [IrcLogger::CinchPlugin]
  end
end

IrcLogger::CinchPlugin.redis = Redis.new(url: Config['redis'])

bot.start

【问题讨论】:

  • 好像您正在尝试打开一个不存在的文件...
  • 可能很傻......我做了什么来解决这个问题,我在 tmp 目录中手动创建了 logger.pid,然后重新启动了应用程序,然后它就可以工作了。
  • 发现了一个错误。现在它已修复。

标签: ruby


【解决方案1】:

似乎源于

pidfile = File.join(File.dirname(__FILE__), 'tmp', 'logger.pid')

试试吧

pidfile = File.join('./tmp', 'logger.pid')

如果“tmp”文件夹不存在,也可以在这里添加一个条件来创建它。当您创建该 pid 文件时,您可以告诉它创建它所在的任何目录。

【讨论】:

    【解决方案2】:

    pdfile = File.join( Dir::pwd,'lib', 'tmp', 'logger.pid' )

    【讨论】:

    • 仍然收到错误 -/home/swapnasa/Downloads/irclogger-master logger.rb:14:in read': No such file or directory - /home/swapnasa/Downloads/irclogger-master/lib/tmp/logger.pid (Errno::ENOENT) from logger.rb:14:in
      '
    【解决方案3】:

    问题很可能是tmp 目录不存在。试试这样的:

    piddir = File.join(File.dirname(__FILE__), 'tmp')
    Dir.mkdir(piddir)
    pidfile = File.join(piddir, 'logger.pid')
    

    【讨论】:

    • 已经存在 tmp 目录 logger.rb:12:in mkdir': File exists - ./tmp (Errno::EEXIST) from logger.rb:12:in
      '
    【解决方案4】:

    如果你添加:

      File.open(pidfile, 'w+') do |f|
        f.write Process.pid
      end
    

    应该会自动为您创建文件。而不是你先创建它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-18
      • 1970-01-01
      • 2023-03-29
      • 2012-02-24
      • 2020-02-22
      • 2015-06-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多