1

I have a minimum example of my problem, which is trying to install a crontab that looks like this:

FOO="foo\"bar\"bin" 0 0 1 * * ls 

by running crontab crontab (I have it in a file name crontab). It fails with this error:

% crontab crontab "crontab":0: bad minute crontab: errors in crontab file, can't install 

It's the escaped quote, \", causing the problem.

How do I escape quotes in crontab's environment variables.

To add a bit more context, I'm not just writing variables in a manually written crontab. This crontab is generated by whenever (a Ruby gem), it has about 70 entries in them, and it picks up all environment variables and puts them as variables in the crontab so all credentials are available to the scripts. The problem is that recently I added a complex one that has quotes in it, and that broke this system.

In case you are curious, the Ruby code that generates the variables is:

ENV.each do |key, value| env key.to_sym, value.inspect end 

3 Answers 3

2

The good practice for cron is:

  1. Create script to run the commands
  2. Set variables in script. Also is possible to use external file with variables: source /path/to/file/with/variables
  3. Use full path to commands /usr/bin/ls
  4. Do not rely on your user home, path, etc

And here is a example how you can create one shell script which run ruby program in different environments.

In cron you have records like (example):

0 1 * * * /path/script 1 0 2 * * * /path/script 2 

The script itself is something like:

#!/bin/bash a=$1 case "$a" in 1) source /path/file1 /path/to/ruby <parameters> ;; 2) source /path/file2 /path/to/ruby <parameters> ;; <snip> *) # do something if parameter is not enumerated ;; esac 

and file1 contain something like (example)

RUBYVAR1_1=1 RUBYVAR1_2=2 

and file2

RUBYVAR2_1=12 RUBYVAR2_2=22 
4
  • The 70 or so commands that I have on my crontab generated by whenever are Ruby scripts. If you mean wrapping them with 70 bash scripts, that would be quite cumbersome, but also, by that point, they lost access to the environment variables with the credentials which is where the variables are coming from (automatically injected by whenever). Commented Aug 30, 2023 at 7:03
  • @PabloFernandez, I show you in the answer a way to move variables in side file which is included in bash scripts. And instead of generating 70 bash files why you do not run your ruby script in cron? If you do not how just ask Commented Aug 30, 2023 at 11:00
  • Reading your answer, I don't see how the values of the envvars get transported from the server, where they are defined (they are secrets), to these bash scripts. This is achieved by the Ruby snippet I put in the original question (ENV.each...) that picks them from the environment where whenever runs them and puts them in the crontab. Commented Sep 5, 2023 at 20:27
  • 1
    @PabloFernandez, this is something new. You get answers to CURRENT question. If you want to change the explanations and environment please create new question. Commented Sep 5, 2023 at 20:31
1

man 5 crontab

An active line in a crontab will be either an environment setting or a cron command. An environment setting is of the form,

 name = value 

where the spaces around the equal-sign (=) are optional, and any subsequent non-leading spaces in value will be part of the value assigned to name. The value string may be placed in quotes (single or double, but matching) to preserve leading or trailing blanks.

So enclosing your string containing double quotes with single quotes should work

Tested with a crontab:

var = 'this"contains"quotes' * * * * * /usr/bin/env >> /tmp/env 

Which looks like it will properly store your double quotes in the var.

cat /tmp/env HOME=/home/HBruijn LANG=C.UTF-8 SHELL=/bin/sh var=this"contains"quotes 

But also I agree with the previous answer and sentiment; cron is not a proper scripting environment, it is just an ancient job scheduler with many limitations and you should offload anything that resembles complexity and include that in the job itself.

1
  • I agree in that it's ancient and we should offload it. We aren't scripting it. We just have a bunch of programs (it doesn't matter whether they are ruby scripts, C compiled binaries, or shell scripts) that need access to all the environment variables in the server when cron triggers them. I can't wrap the envvars in single quotes, or double quotes because they may have one, or the other or both (this is automatically generated by the ENV.each... line in my question, I'm not manually writing this list anywhere). Commented Sep 5, 2023 at 20:29
0

As far as I could find, crontab variables just cannot have quotes and they also can't be more than 999 characters long. The answer to this question is that it's simply not possible.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.