Ask A Question

Notifications

You’re not receiving notifications from this thread.

Logger.debug to output command line results

Anand Padia asked in Rails

I have an application where we have multiple domains mapped to same server and we need to create letsencrypt certificate for each of these domains. Along with this we need to create nginx files to accomany them..

I have successfully worked out a strategy to create nginx files dynamically from the ui based off user's input however when it comes to checking logs it is not that clear.. BTW we are using sidekiq with rails..

I am looking to see if there is a way to show the out of the command in rails logs..

Code for the custom class/service created..

command_to_run = "certbot certonly --non-interactive --webroot -w /home/deploy/.letsencrypt" + domain_info + " --email myemail@email.com --agree-tos --expand"
system(command_to_run)

if I run this command on terminal the output is ...

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Starting new HTTPS connection (1): acme-v01.api.letsencrypt.org
Cert not yet due for renewal
Keeping the existing certificate
so on....

how can I show this output in logs to check if the command ran properly and certificates were generated appropriately.

Reply

Hey Anand,

For Hatchbox.io, when I do things like setup LetsEncrypt, etc. I use the exit code of the commands to determine success or fail. You probably want to do the same thing here rather than looking at the logs.

There's a special variable Ruby sets called $? after you run a system command that you can check for the exit code of the command you just ran. If it's 0 then the command was successful, non-zero means it failed.

system also seems to return nil, true or false based on the exit status according to the docs: http://ruby-doc.org/core-2.0.0/Kernel.html#method-i-system

system("unknown command")     #=> nil
system("echo foo")            #=> true
system("echo foo | grep bar") #=> false

So that means you could say success = system(command) and check for truthiness to see if it was successful.

Reply

Thanks Chris, I was able to get required result on logs.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 85,376+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.