Search code examples
shellcroncron-task

Cronjobs: Is there a way to set scope of shell variables only once for all cronjobs?


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

Solution

  • 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