why #!/usr/bin/env ruby doesn't work in crontab?
If you installed ruby via rvm, ruby probably isn't in /usr/bin. Depending on where rvm is installed:
bash -c "source /usr/local/lib/rvm" && rails runner foo.bar
You probably added a source */rvm to your bashrc that is the correct rvm loading script.
Cron job can't get it to run, What syntax to use for the crontab?
Cron jobs don't load the user's environment. Try adding RAILS_ENV=production before your command within crontab, or whichever environment you need.
Example:
RAILS_ENV=production
*/3 * * * * /your/command/here
OR, if you want to make sure you have your user's full environment, execute the command within a login shell:
*/3 * * * * bash --login -c '/your/command/here'
Why crontab can't use ruby?
In crontab, type full pathes:
* * * * * echo $(/usr/share/rvm/rubies/ruby-2.3.3/bin/ruby -v) >> 123.rb
* * * * * echo "123" >> 123.rb
should work.
Or, add $PATH
variable:
In console:
echo $PATH
Copy value, in crontab file add:
export $PATH="<copied pathes>:/usr/share/rvm/rubies/ruby-2.3.3/bin/"
* * * * * echo $(ruby -v) >> 123.rb
* * * * * echo "123" >> 123.rb
What is the difference between #!/usr/bin/env bash and #!/usr/bin/bash?
Running a command through /usr/bin/env
has the benefit of looking for whatever the default version of the program is in your current environment.
This way, you don't have to look for it in a specific place on the system, as those paths may be in different locations on different systems. As long as it's in your path, it will find it.
One downside is that you will be unable to pass more than one argument (e.g. you will be unable to write /usr/bin/env awk -f
) if you wish to support Linux, as POSIX is vague on how the line is to be interpreted, and Linux interprets everything after the first space to denote a single argument. You can use /usr/bin/env -S
on some versions of env
to get around this, but then the script will become even less portable and break on fairly recent systems (e.g. even Ubuntu 16.04 if not later).
Another downside is that since you aren't calling an explicit executable, it's got the potential for mistakes, and on multiuser systems security problems (if someone managed to get their executable called bash
in your path, for example).
#!/usr/bin/env bash #lends you some flexibility on different systems
#!/usr/bin/bash #gives you explicit control on a given system of what executable is called
In some situations, the first may be preferred (like running python scripts with multiple versions of python, without having to rework the executable line). But in situations where security is the focus, the latter would be preferred, as it limits code injection possibilities.
Cannot install NodeJs: /usr/bin/env: node: No such file or directory
Doing a symlink solves the issue:
ln -s /usr/bin/nodejs /usr/bin/node
(My thanks and +1 vote to bodokaiser's answer).
RoR: $bundle install = /usr/bin/env: ruby: not a directory
Solved:
src: https://askubuntu.com/questions/182418/how-to-get-usr-bin-env-ruby-to-point-to-the-correct-ruby-environment
Adding:
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # This loads RVM
to:
.bashrc
THEN to fix MySQL installation errors I used the second solution (67 votes) on this question:
MySQL Install: ERROR: Failed to build gem native extension
I hope this helps someone else in the future!
Related Topics
Setting a Global Within a Proc
Ruby 'Gets' That Works Over Multiple Lines
How to Use Headless Chrome with Capybara and Selenium
How to Convert a Ruby Bigdecimal to a 2-Decimal Place String
Undefined Method 'Instance' for Capistrano::Configuration:Class
Call Next on Ruby Loop from External Method
Capybara Trouble Filling in Js Modal
Why Does 'Puts(Nil or 4)' Fail in Ruby
Ruby on Rails: Converting "Somewordhere" to "Some Word Here"
How to Replace the Characters in a String
Why Is the << Operation on an Array in Ruby Not Atomic
Ruby: How to Call Function Before It Is Defined
Accessing a Has_One Associations' Attributes
Scraping a Site That Requires Login Username and Password on Two Separate Pages