Skip to main content

Logger.debug to output command line results

Rails • Asked by Anand Padia

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 [email protected] --agree-tos --expand"

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

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Starting new HTTPS connection (1):
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.

Hey Anand,

For, 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:

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.

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

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 22,346+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.