RVM isnt setting environment with cron

后端 未结 3 658
青春惊慌失措
青春惊慌失措 2021-01-15 02:43

I\'m having a rough time executing script/runner with a cron and RVM. I believe the issues lie with the rvm environment not being set before the runner is executed.

3条回答
  •  日久生厌
    2021-01-15 03:24

    You don't need to write a second cron runner (following that logic, you might as well write a third cron runner runner). Please keep things simple. All you need to do is configure your cron job to launch a bash shell, and make that bash shell load your environment.

    The shebang line in your script should not refer directly to a ruby executable, but to rvm's ruby:

    #!/usr/bin/env ruby
    

    This instructs the script to load the environment and run ruby as we would on the command line with rvm loaded.

    On many UNIX derived systems, crontabs can have a configuration section before the actual lines that define the jobs to be run. If this is the case, you would then specify:

    SHELL=/path/to/bash  
    

    This will ensure that the cron job will be spawned from bash. Still, your environment is missing, so to instruct bash to load your environment, you will want to add to the configuration section the following:

    BASH_ENV=/path/to/environment (typically .bash_profile or .bashrc) 
    

    HOME is automatically derived from the /etc/passwd line of the crontab owner, but you can override it.

    HOME=/path/to/home
    

    After this, a cron job might look like this:

    15 14 1 * *     $HOME/rvm_script.rb
    

    What if your crontab doesn't support the configuration section. Well, you will have to give all the environment directives in one line, with the job itself. For example,

    15 14 1 * * export BASH_ENV=/path/to/environment && /full/path/to/bash -c '/full/path/to/rvm_script.rb'
    

    Full blog post on the subject

提交回复
热议问题