7

I have a Sinatra app which I run on my local machine using ruby app.rb. While deploying it on a remote machine via ssh, how do I run it in background and redirect stdout and stderr to a log file?

On a restart, I want to preserve the previous logs so that newer messages are appended to the existing log file, instead of truncating it.

What's the recommended way of running my web application as a daemon?

I've tried nohup ruby app.rb &, but that seems to be missing stderr and the log statements seem to be out of order in some cases.

Peter Mortensen
  • 2,319
  • 5
  • 23
  • 24
letronje
  • 429
  • 1
  • 6
  • 16

2 Answers2

9

Under bash, try:

nohup ruby app.rb >> /log/file 2>&1 &
MadHatter
  • 78,442
  • 20
  • 178
  • 229
  • "2>&1" what does that mean ? – letronje Dec 20 '10 at 06:27
  • 3
    "Send STDERR to the same place you're sending STDOUT" – MadHatter Dec 20 '10 at 06:56
  • thnx a ton for the cmd :) – letronje Dec 20 '10 at 12:13
  • And what does the end `&` mean? – Nakilon Oct 10 '13 at 16:57
  • "*Run in the background*", though please note that SF isn't a forum, and this might have been better asked as a separate question on http://unix.stackexchange.com (after some research, because I'm fairly sure this question will have been asked there before). – MadHatter Oct 10 '13 at 19:55
  • It's perfectly fine to ask follow-up questions; visitors can understand more that way. – shevy May 01 '19 at 14:57
  • @shevy a followup question from the OP is reasonable, and I'd answered it. But the other question came **three years later**, from a third party. It would have been better asked as a separate question (and in any case, as I think I said, it's a pretty common one). – MadHatter May 01 '19 at 20:11
3

screen -L -dmS somename ruby app.rb

This will start a screen process with the name of 'somename', with all output from the program being logged to screenlog.0 in the current working directory.

If you ever want to get back the application's console for some reason, you can do screen -r somename.

Greg
  • 167
  • 4
devicenull
  • 5,572
  • 1
  • 25
  • 31