Search code examples
ruby-on-railsloggingsshtail

Run an SSH command to stream Rails logs


I would like to create a function that is able to run a remote script via SSH that streams Rails logs. Currently, I have to do this manually:

ssh dev_server
cd /var/log/.../rails_app
tail -f production.log web.log

I would like to run a command like rails_logs, run the above commands and stream my logs. How can I do this?


Solution

  • How about just running the command you want in your initial ssh connection?

    ssh dev_server 'tail -F /var/log/.../rails_app/log/*log'
    
    • Using -F instead of -f will follow a filename rather than an inode. Handy if your logs ever rotate

    If you want to make this easier, you could put your tail command into a remote script which you could call:

    ssh dev_server my_tail_script
    

    You could also alias this locally and just call your alias:

    # put this line into your `.bashrc` or `.profile`:
    alias tl='ssh dev_server my_tail_script'
    
    # Now you can just call your alias:
    tl