crontab job issues with export command

Hi all,

I have sevaral jbos with bunch of export commands like export VARIABLE=value.
They work perfectly when I run manually but when I try to run from crontab it stops right at this export command.

But when I replace these export with these, crontab job works fine.

VARIABLE = value; export VARIABLE.

Is there a way to make crontab job work with the orginal way of exporting ( export VARIABLE=value). I have 100+ like this and don't want to send time modifying all these. :wall: :wall:

Thanks in advance.
mkneni

Well,

export var_name='value' 

is a bash/ksh form. Many commands and systems like cron default you to sh, where you can't say that, but must say:

var_name='value' ; export var_name

If you are in csh, I think it is

setenv name value

or the like. Your crontab line can say

bash bash_script_name

or

ksh -c "commands . . . . "

but the slick thing is to make your script execvp() friendly: execuatable

chmod u+x path_to_script

and with the first line

#!/abs_path/to/interpreter one_optional_argument

so cron's sh execvp()'s it over to the right interpreter. You can make a sed script, for instance, with

#!/bin/sed -f

as the first line, since sed does not expect scripts by default, only under the -f option. For bash,

#!/usr/bin/bash

is all you need, as it expects script names. Don't forget or innovate, as execvp() is not imaginative. Verify the interpreter path with something like "which bash". As man execvp says, if you mess up, you get

sh < your_not_sh_script_file

which can be disappointing in many ways! :smiley:

If the first line of the cron job script is:
#!/bin/sh
export VARIABLE=value
will not work but
VARIABLE=value ; export VARIABLE
will.

If the first line is:
#!/bin/ksh
export VARIABLE=value
will work.

Right, no need to revert to good old Bourne sh, execvp() treats all scripts alike, so use your favorite and enjoy portable skills.

@MKNENI
In Solaris the Shell used by cron is /usr/bin/sh which is one which does not allow the "export variable=value" syntax.
There are two solutions.
1) Prepend a shebang line for your normal shell to every script
2) Invoke your normal shell from the cron command line
e.g.
/usr/bin/ksh /pathtoscript/scriptname

Thank you all. You are the best.

#!/bin/ksh woked great. This saved lot of trouble for me.

;);):wink: