whenever icon indicating copy to clipboard operation
whenever copied to clipboard

Env vars with newlines cause whenever to create invalid crontabs

Open kkuchta opened this issue 7 years ago • 2 comments

The title pretty much says it all. If you specify an environment variable with a newline in it (eg env('FOO', "bar\nbaz"), the resulting crontab is invalid. It puts the later lines of the environment variable on their own lines. Since baz is an invalid crontab, this produces

➜  echo "env('FOO', \"bar\\\nbaz\")" > schedule.rb
➜  whenever -f schedule.rb
FOO=bar
baz

## [message] Above is your schedule file converted to cron syntax; your crontab file was not updated.
## [message] Run `whenever --help' for more options.

Apparently this is a limitation of crontab's variables. Putting quotes around the multiline variable contents doesn't help.

My current workaround is to write the env vars to a temp file and then source it:

environment_variables = { FOO: "bar\nbaz" }
env_script_string = environment_variables.map do |key, value|
  "export #{key}=\"#{value}\"\n"
end
env_script = Tempfile.new('env_script')
env_script << env_script_string

set :job_template, "bash -l -c 'source #{env_script.path}; :job'"

Is there any better way to handle this with Whenever itself?

kkuchta avatar Dec 19 '17 01:12 kkuchta

Just ran in to this issue on my end. For our use case, using Rails Secrets may be a better fit for us.

derosm2 avatar Sep 28 '18 15:09 derosm2

+1 for this issue, any multiline BASH_FUNC_ in environment will invalid the crontab output

prunelsigma avatar Aug 25 '22 01:08 prunelsigma