Search code examples
rubycapistranocapistrano3

File.read() fails in my Capistrano task (file *does* exist)


My Capistrano task is failing with

No such file or directory @ rb_sysopen - /home/blog/pids/grantb.blog.staging.pid`

Here's proof that the file does exist, from my command prompt:

[blog@grantb current]$ ls /home/blog/pids/grantb.blog.staging.pid
/home/blog/pids/grantb.blog.staging.pid

[blog@grantb current]$ irb
2.1.0 :001 > File.read("/home/blog/pids/grantb.blog.staging.pid").to_i
 => 936

Here's the partial task:

desc 'Restart application'
task :restart do
  on roles(:app), in: :sequence, wait: 5 do
    pid_file = fetch(:pid_file)

    puts "PID_FILE name: #{pid_file}"
      # outputs 'PID_FILE name: /home/blog/pids/grantb.blog.staging.pid'
    execute "/bin/ls #{pid_file}"  
      # outputs '/home/blog/pids/grantb.blog.staging.pid'
    execute "/bin/ls #{pid_file}"  
      # outputs '936'

    pid = File.read(pid_file).to_i
    # Exception while executing on host x.x.x.x: No such file or directory @ rb_sysopen - /home/blog/pids/grantb.blog.staging.pid
    # WHAT THE HELL!

Can anyone tell me where I'm going wrong?


Solution

  • Figured it out.

    File.read() is being performed on my local machine, not on the remote one. Ugh.

    What I needed to do was something like this:

    within release_path do
      execute "cat #{pid_file}"
    end
    

    My actual task is:

    desc 'Restart application'
    task :restart do
      on roles(:app), in: :sequence, wait: 5 do
        pid_file = fetch(:pid_file)
    
        within release_path do
          execute "((ls #{pid_file} && ps -p `cat #{pid_file}`) && kill -9 `cat #{pid_file}`) || true"
          execute "(ls #{pid_file} && /bin/rm #{pid_file}) || true"
    
          # RESTART COMMAND GOES HERE
        end
      end
    end