1

We have a rails sidekiq setup to run jobs.

I am trying to make a job as portable as possible by separating the actual script from the sidekiq call.

So sidekiq calls a small stub job that backtick calls the actual script and captures the output.

That part works fine. But sidekiq runs the job as root, rather than as the user who's environment has all the rvm parts, rubies, and gemsets. I'd like to use the existing user rvm rubies and gemsets.

in the calling script (sidekiq job):

output = `source /path/to/dudes/rvm/environment.file && rvm use  \ 
2.0.0-pxxx@default do ruby /path/to/actual/script.rb`

and the called script gets run, but as root and it obviously doesn't work as I intended because my requires are not found.

If I take that same command string and run it as a local user from BASH, who also has no gemsets, it seems to work.

I've tried just backtick calling it like a shell script

output = `/path/to/actual/script.rb`

...and in the called script, various combinations of shebangs.

#!/usr/bin/env ruby
#!/usr/bin/env rvm use everything i found on the internets

Now, I've gotten ruby scripts to run in environments with linux upstart jobs by using bash script wrappers like this: http://techhelplist.com/index.php/tech-tutorials/43-linux-adventures/85-upstart-ruby-job-with-rvm

But I am trying to find a way to do this with no wrappers. Is it possible?

infused
  • 24,000
  • 13
  • 68
  • 78
user145837
  • 111
  • 1

2 Answers2

0

No, it is not possible.

When you use the "backtick" operator you are essentially calling Kernel#system(...). This means the command you execute is run in a subshell which cannot change the environment of the parent process in the ways you want.

See this question for comparison: How to run code after ruby Kernel.exec

Also consider reading more about the UNIX process model.

Community
  • 1
  • 1
maerics
  • 151,642
  • 46
  • 269
  • 291
  • But in this case, am I not trying to change the environment of the child process? I want to basically copy or mimic the environment from the parent to the child. I probably didn't make that part clear. Even if I have to make the child process run some stuff to manually load the environment, that is ok – user145837 Sep 26 '13 at 14:38
0

According to Upstart Intro, Cookbook and Best Practises, point 4.2.2, you can set user jobs in $HOME/.init/.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mpapis
  • 52,729
  • 14
  • 121
  • 158