Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect the "puts" command output to a log file

Tags:

ruby

daemon

I am working on creating a daemon in Ruby using the daemons gem. I want to add output from the daemon into a log file. I am wondering what is the easiest way to redirect puts from the console to a log file.

like image 509
Josh Moore Avatar asked Oct 22 '08 04:10

Josh Moore


People also ask

How do I redirect an output to a file?

To redirect the output of a command to a file, type the command, specify the > or the >> operator, and then provide the path to a file you want to the output redirected to. For example, the ls command lists the files and folders in the current directory.

How do I redirect a log file in Linux?

In Linux, for redirecting output to a file, utilize the ”>” and ”>>” redirection operators or the top command. Redirection allows you to save or redirect the output of a command in another file on your system. You can use it to save the outputs and use them later for different purposes.

What would you use to redirect the output of a command to another command?

Redirection is done using either the ">" (greater-than symbol), or using the "|" (pipe) operator which sends the standard output of one command to another command as standard input.

Which character is used to redirect output into an existing file in Linux?

Just as the output of a command can be redirected to a file, so can the input of a command be redirected from a file. As the greater-than character > is used for output redirection, the less-than character < is used to redirect the input of a command.


1 Answers

If you need to capture both STDERR and STDOUT and don't want to resort to logging, following is a simple solution adapted from this post:

$stdout.reopen("my.log", "w")
$stdout.sync = true
$stderr.reopen($stdout)
like image 162
Eric Walker Avatar answered Sep 24 '22 14:09

Eric Walker