Rails: Rails 6.0.3.4

Ruby: ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux]

Nginx: nginx version: nginx/1.18.0 (Ubuntu)

Phusion Passenger: 5.3.2

In my controller, I want to run a system command. It works locally but it doesn’t work remotely.

Code

I create a markdown-file and store it inside my public folder.


dir = Rails.root.join('public', 'pancritic')
Dir.mkdir(dir) unless Dir.exist?(dir)
File.open(dir.join("pancritic.md"), 'w+') do |file|
  file.write(@inputs[:text])
end

After storing the file in /public/pancritic, I want to run a system command:

`pancritic -s -o #{dir}/pancritic.pdf #{dir}/pancritic.md`

This code converts the markdown-file to a pdf. The output is stored in the same folder.

But in production (on Digital Ocean), the command does not create the pdf.

Does the system command work on Digital Ocean?

When I ssh as a non-root user into digital ocean, cd into public/pancritic and enter

pancritic -s -o pancritic.pdf pancritic.md 

the pdf gets created. I assume that there is nothing wrong with the command itself.

What could be the reason why the command does not work in Production?

What I tried so far

I’ve tried other ways of entering system commands. They all work locally, but none of them work in production:

system("pancritic -s -o #{dir}/pancritic.pdf #{dir}/pancritic.md")

Open3.popen3("pancritic -s -o #{dir}/pancritic.pdf {dir}/pancritic.md") do |stdin, stdout, stderr, wait_thr|
   ...
    end

%x[pancritic -s -o #{dir}/pancritic.pdf {dir}/pancritic.md]

Full Code

  def pancritic_editor
    if params[:inputs].present?
      @inputs = OpenStruct.new(params[:inputs])
    else
      @inputs = OpenStruct.new()
    end

    start_markdown ='
# Introduction
Text
'

@inputs[:text] = @inputs[:text] ? @inputs[:text] : start_markdown

dir = Rails.root.join('public', 'pancritic')
Dir.mkdir(dir) unless Dir.exist?(dir)
File.open(dir.join("pancritic.md"), 'w+') do |file|
  file.write(@inputs[:text])
end

`pancritic -s -o #{dir}/pancritic.pdf #{dir}/pancritic.md`

until File.exist?("#{dir}/pancritic.pdf")
  sleep 1
end

File.open("#{dir}/pancritic.pdf") do |file|
  @stuff = Stuff.create(filename: "pancritic_editor #{DateTime.now}")
  @stuff.file.attach(io: file, filename: "basic-markdown-editor-#{Date.today.to_s}.pdf")
end

File.delete("#{dir}/pancritic.md")
File.delete("#{dir}/pancritic.pdf")
  end

1 comment
  • Maybe I installed the programs incorrectly. Here are the results of the which command:

    which pancritic: /home/myusername/.local/bin/pancritic

    which pandoc: /usr/bin/pandoc

    which pdflatex: /home/myusername/.local/bin/texlive/bin/x86_64-linux/pdflatex

Submit an answer

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!