1

I am looking to frequently create 3 background tasks and execute some shell commands from these tasks from my Rails app. I was thinking about using delayed_job for the background tasks but I am unsure as to how to safely execute shell commands from a rails app, can someone tell me how I can do this from a Rails app?

Jeff

jeffci
  • 2,537
  • 6
  • 37
  • 59

1 Answers1

4

There are several ways, all are Rubyisms.

system('cmd','arg1',...)

Or the very Unixy

val = `ls -l`

The latter would run the command and return it's output into the variable val.

RadBrad
  • 7,234
  • 2
  • 24
  • 17
  • I am aware of system() and back ticks but this seems like a bad idea. What I mean is is there anything for Ruby/Rails like http://docs.python.org/library/subprocess.html#module-subprocess? (My background is/was py) Running system commands from a privileged process such as apache seems like a security issue so I was hoping to find something similar or equivalent to the subprocess module from python. – jeffci Jun 04 '12 at 16:47
  • It's a potential security hole, you just have to make sure whatever get's handed to system is validated. I use it extensively, but anything passed to system never comes from user input, so it works for me. Here's a relevant question: http://stackoverflow.com/questions/4650636/forming-sanitary-shell-commands-or-system-calls-in-ruby – RadBrad Jun 04 '12 at 17:54
  • great! none of the data comes from my users as well but am always concerned about ANY security holes. I will investigate this solutions, thanks rad – jeffci Jun 04 '12 at 20:34