Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chruby from cron #219

Closed
jrochkind opened this Issue Oct 21, 2013 · 12 comments

Comments

Projects
None yet
2 participants
@jrochkind
Copy link

jrochkind commented Oct 21, 2013

The wiki page on cron suggests you can just put chruby-exec in the cron file:

30 18 * * *   chruby-exec 1.9.3 -- /path/to/script

https://github.com/postmodern/chruby/wiki/Cron

But when I try this, I get a "/bin/sh: chruby-exec: command not found"

Maybe because the .bashrc or .bash_profile scripts that would load chruby and make the chruby-exec command available aren't run by cron?

So I thought I'd try:

* * * * * /bin/bash -l -c 'chruby-exec 1.9.3 -- /something'

But that still gives me: /bin/bash: chruby-exec: command not found

From an interactive shell as the same user that cron is running on, I can definitely access chruby-exec.

Okay, continuing to investigate and try to answer my own question, I see chruby-exec is not a shell function, but is actually installed at /usr/local/bin/chruby-exec.

So why wouldn't this be in my path on a cronjob, even with /bin/bash -l -c, weird!

I'm having trouble figuring that out. If I run: /bin/bash -l -c 'echo $PATH', I've got /usr/local/bin in there.

But if I make a cronjob:

 * * * * * /bin/bash -l -c 'echo $PATH | mail me'

Then indeed the PATH does not include /usr/local/bin.

I realize this is more a unix issue than a chruby issue, but this is perhaps something people other than me are likely to run into trying to use chruby in a cron? You definitely have more shell scripting knowledge then me, do you have any suggestions or places to investigate? would appreciate any tips.

@jrochkind

This comment has been minimized.

Copy link
Author

jrochkind commented Oct 21, 2013

Okay, I found your suggestion that chruby-exec is really just syntactic sugar:

#24 (comment)

So trying this in a cronjob:

/bin/bash -l -c 'chruby 1.9.3 && something`

That DOES appear to work for me in a cron! I guess I'll use that? Does chruby-exec work for most people on other distros in cron, cause they have /usr/local/bin in their cron path already? Confused!

i wonder if the wiki page should be altered to warn you about $PATH issues, and/or suggest /bin/bash -l -c 'chruby X && Y' as an alternative? I don't know if this will be a general issue, or it's one unique to my environment; I don't understand enough about the issue to feel comfortable editing the wiki page to give advice to others, but appreciate feedback.

@jrochkind

This comment has been minimized.

Copy link
Author

jrochkind commented Oct 21, 2013

oh man, this is so confusing, trying to switch my uses of chruby-exec to just chruby X && caused other issues I still havne't debugged in other areas.

Ah, my other issue is that a ruby wrapper script I had ran some shell commands with ruby system, which runs things under just /bin/sh, where chruby-exec worked, but chruby X would not.

Man. I guess I could change my system() to do system("/bin/bash chruby X && Y") -- is that what's advisable?

Would definitely appreciate some advice based on your shell expertise (better than mine) and current understanding of best way to do all these things, @postmodern.

Now I don't know if I should leave chruby-exec but make sure to explicitly set the PATH in my crontab -- but that might break when you refactor chruby-exec to be a shell function, as you keep suggesting you will soon in various tickets, etc.

@jrochkind

This comment has been minimized.

Copy link
Author

jrochkind commented Oct 21, 2013

Weirdly, even spelling out /usr/local/bin/chruby-exec in a cron doesn't seem to work, and I can't figure out why:

* * * * * /bin/bash -l -c 'echo "$(/usr/local/bin/chruby-exec jruby -- ruby -v)" | mail me -s "new version check from cron with bin bash"'

Get me a:

/bin/sh: chruby: command not found

I can't figure out how /bin/sh even got involved there , to not be able to find 'chruby'. I'm explicitly running it all in a /bin/bash, plus calling /usr/local/bin/chruby-exec, which begins with #!/usr/bin/env bash.

What?

Man, this stuff is confusing. I liked chruby's simplicity over even rbenv... but I had cron jobs working using rbenv, and can't seem to figure out how to get them working with chruby.

@postmodern

This comment has been minimized.

Copy link
Owner

postmodern commented Oct 21, 2013

So cron isn't a shell and just executes your commands in a really stripped down env. You'll want to set SHELL and PATH above your commands.

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
MAILTO=root
@jrochkind

This comment has been minimized.

Copy link
Author

jrochkind commented Oct 21, 2013

Sadly, it doens't look like you can do the "PATH=$PATH:/usr/local/bin" trick, so i can't just add /usr/local/bin on to what's already there, need to duplicate what's already there, which requires figuring it out somehow. Phew.

Anyhow, would it make sense to add this tip to the wiki page?

And is chruby-exec still your recommended method for cronjobs at the moment? Various comments in issues show that you're thinking has gone back and forth on this sometimes.

@postmodern

This comment has been minimized.

Copy link
Owner

postmodern commented Oct 21, 2013

You'll only need to ensure PATH contains where chruby-exec is installed. Once chruby-exec runs, it will spawn $SHELL -l -c "chruby ... && command ...". Of course you'll have to ensure that chruby is added to the shell's configuration. While chruby-exec is more concise, it makes the assumption that SHELL is set and chruby is loaded by the shell.

@postmodern

This comment has been minimized.

Copy link
Owner

postmodern commented Oct 21, 2013

Also, chruby must be configured globally (/etc/profile.d/chruby.sh) if you use it in the global crontab.

@postmodern

This comment has been minimized.

Copy link
Owner

postmodern commented Oct 21, 2013

If you want to run the cron job under a specific user, try running crontab -e.

@jrochkind

This comment has been minimized.

Copy link
Author

jrochkind commented Oct 21, 2013

I am not using a global crontab, I don't think. I am using a partiuclar account's crontab.

That particular account does have chruby enabled, in it's .bashrc. (and has it's .bash_profile explicitly sourcing .bashrc for good measure). I do not use a global /etc/profile.d/chruby.sh though.

Nonetheless, I'm still getting /bin/sh: chruby: command not found errors. Which is odd. chruby doesn't even feature in my cron line, just chruby-exec. So this error must be coming from chruby-exec's own attempt to call chruby.

Hmm, I see, because maybe it's calling chruby-exec when $SHELL is still sh, unless I make sure to include SHELL=/bin/bash at the top of the crontab.

What's actually going on is even more confusing and complicated than this, I've tried to simplify. But okay, I'll have to keep troubleshooting.

Thanks, your advice is helping. But I would strongly encourage you including more of these tips in the wiki page on chruby on cron. I'm going on 6 hours of working on this even though I'm not a complete idiot when it comes to unix and shells, this stuff gets awfully complicated. It maybe could have saved me a few of those hours to have a few more hints in the wiki page.

(I am trying to change a system that previously used rbenv, in a fairly complicated way, to use chruby. After another 6 hours or so of trying, I'll prob give up and go back to rbenv)

@jrochkind

This comment has been minimized.

Copy link
Author

jrochkind commented Oct 21, 2013

Yes, of course I am using crontab -e to edit a crontab file for a specific user, yes, that's what I'm doing.

@postmodern

This comment has been minimized.

Copy link
Owner

postmodern commented Oct 21, 2013

This works for me:

SHELL=/bin/bash
HOME=/home/hal
PATH=/bin:/usr/bin:/usr/local/bin

* * * * * chruby 1.9.3 -- ruby -v > $HOME/cron_output.log

Updated the Cron Wiki page with this.

@postmodern postmodern closed this Oct 21, 2013

@jrochkind

This comment has been minimized.

Copy link
Author

jrochkind commented Oct 21, 2013

Yep, setting both SHELL and PATH in the env is what it took. thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.