whenever
whenever copied to clipboard
Env vars with newlines cause whenever to create invalid crontabs
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?
Just ran in to this issue on my end. For our use case, using Rails Secrets may be a better fit for us.
+1 for this issue,
any multiline BASH_FUNC_ in environment will invalid the crontab output