0

I'm wondering if there is an more elegant solution by defining the scope of my shell variables only once? (export QUERY_STRING;)

Here are a couple of my crontasks, notice that I'm currently exporting each shell variable for each task:

2 0 1 * * QUERY_STRING='drink/beer/carlsberg'; export QUERY_STRING; /usr/local/bin/php -e /home/myuserdir/public_html/index.php
4 0 1 * * QUERY_STRING='food/pizza/hawai'; export QUERY_STRING; /usr/local/bin/php -e /home/myuserdir/public_html/index.php
am_
  • 2,378
  • 1
  • 21
  • 28
  • You can write it once, at the top of crontab file. – fedorqui Apr 04 '13 at 12:36
  • fedorqui: I thought cronjob/task had a limited environment, hence the reason to export the shell var for each job/task. I'll try what you suggest though and see what happens. – am_ Apr 04 '13 at 13:16

1 Answers1

2

Use env(1):

env QUERY_STRING=food/pizza/hawai /usr/local/bin/php -e /home/myuserdir/public_html/index.php

Or the short form of export:

export QUERY_STRING=food/pizza/hawai; /usr/local/bin/php -e /home/myuserdir/public_html/index.php
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • Thanks for the suggestions, I used the short form of export earlier - and that didn't work for me as a crontask (however it works when manually called). I expect that env might also have similar issues? – am_ Apr 04 '13 at 13:07
  • @am_ `env` is a separate program while `export` is a shell built-in, so I wouldn't expect similar issues. Try it. – Fred Foo Apr 04 '13 at 14:20
  • That makes sense! thank you, using env works great - and are indeed shorter and more elegant. :) – am_ Apr 04 '13 at 18:04