Ruby给出“没有这样的文件或目录 - text.txt(Errno :: ENOENT)”错误

时间:2013-08-20 16:15:32

标签: ruby

当我尝试运行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

4 个答案:

答案 0 :(得分:0)

似乎源于

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

尝试

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

还在此处添加条件以创建'tmp'文件夹(如果它不存在)。当你创建那个pid文件时,你可以告诉它创建它所在的任何目录。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

如果你添加:

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

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