tags:

views:

31

answers:

5

How can I store variables in my crontab? I realize it's not shell but say I want to have some constants like a path to my app or something?

Thoughts?

A: 

I'm not much of a unix guy, so I can't say anytihng definite, but this sounds like a good place for a pre-processer step.

#define cmdpath /usr/bin/myfolder/cmd
0,30 8-17 * * 1-5 cmdpath
17 3 * * 1 cmdpath

running that through my c++ compiler's preprocessor gives as:

0,30 8-17 * * 1-5 /usr/bin/myfolder/cmd
17 3 * * 1 /usr/bin/myfolder/cmd

Which seems like just what you wanted.

James Curran
When all you have is a hammer, every problem looks like a nail.
Byron Whitlock
That turns the relatively easy process of editing a crontab with `crontab -e` into a three step process of editing your crontab, invoking cpp, and installing the new crontab with crontab $filename
Matt Kane
@Matt: a) that's what shell scripts are for, and b) but `crontab -e` doesn't do what he wants, so it's not really an answer.
James Curran
+1  A: 

You can add arguments to a PHP call in the crontab:

 php -f /path/to/yourscript.php arg1 arg2 arg3

and then fetch them using the argv variable.

As an alternative, you could call a wrapper script (PHP or a shell script) in your crontab that sets the variables, and then calls the script itself.

Pekka
+2  A: 

You can put environment variables in the crontab. See the man page for crontab(5) for more details.

Richard Fearn
+1  A: 

You can run shell scripts from your crontab. In these shell scripts, you can set variables.

Starkey
+2  A: 

You can do this almost exactly like a shell script.

VARIABLE=value
PATH=/bin:/path/to/doathing
0 0 * * * doathing.sh $VARIABLE

The man page says:

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. The name string may also be placed in quote (single or double, but matching) to preserve leading, trailing or inner blanks.

Matt Kane
+1 Thats the answer
Byron Whitlock