Operating System - HP-UX
1831097 Members
2516 Online
110019 Solutions
New Discussion

Re: works on command line but does not work in cron

 
SOLVED
Go to solution
amonamon
Regular Advisor

works on command line but does not work in cron

Hello..

I made one script which code I will post...script works fine from CL but in cron it has problems with second nawk. Script:

nawk -F"|" '
{ s=substr($104,2,18)}
{b[s] ++s}
END { for (i in b) print i, b[i] } ' $1 > /path/to/file/TranId_www$2


q=$(cat /path/to/file/TranId_www$2 | wc -l)
echo $q > /path/to/file/zawww


nawk -F"|" -v x=$2 -v w=$(/usr/bin/cat /path/to/file/zawww) '
{u=u+1; l=l+$66}
($66 != 0)&&($110 == 1)&&($111 == 0) { a=a+1; s=s+$66}
END { print x"|"u"|"l"|"w"|"s } ' $1 >> statistic_www


Also note that I got TranId_www$2 after script exec. in cron but statistic_www can not be appended..what might be an issue?

Pleaseeeee can anyone figure out what is problem and why does second nawk make problems??

THANKS in advance
25 REPLIES 25
James R. Ferguson
Acclaimed Contributor

Re: works on command line but does not work in cron

Hi:

Unless you have established your own PATH in your script, 'cron' has only provided a minimal one, notably:

PATH=/usr/bin:/usr/sbin:.

Make sure that you have what you think you do!

Regards!

...JRF...
Pete Randall
Outstanding Contributor

Re: works on command line but does not work in cron

As a follow-up to James' comments, the first thing that I see is "nawk", without a full path name. You have to either define a PATH or provide full path names for everything!


Pete

Pete
Peter Godron
Honored Contributor

Re: works on command line but does not work in cron

Hi,
cat and echo may be missing their paths.
do you have a correct:
/path/to/file/zawww
file ?

You could run the script in debug mode (include "set -x" as the start of the script)
to track the path of code.


DCE
Honored Contributor

Re: works on command line but does not work in cron


Cron runs with only a limited shell - be sure to fully the path to all commands.

In fact you may need to explicitly set some more environment variables that are not set by cron limited shell.

This is sometimes bypassed by using

su - -c "some command" as the line in cron in order to invoke a user's shell before the command
amonamon
Regular Advisor

Re: works on command line but does not work in cron

well my path is OK..
it is not problem with path becouse I have my other script similar almost like this one:

nawk -F"|" -v x=$2 '
{u=u+1; l=l+$66}
($66 != 0)&&($110 == 1)&&($111 == 0) { a=a+1; s=s+$66}
#$66 != 0 { a=a+1; s=s+$66}
END { print x"|"u"|"l"|"a"|"s } ' $1 >> statistic_www

and this works from my cron..so as U can see it can recognise nawk and other things..but in script from my first post first nawk is executed brcouse I can see TranId_www$2 but second is not becouse file does not append..

any better ideas?? thanks for reply
Bill Hassell
Honored Contributor

Re: works on command line but does not work in cron

All scripts, expecially in cron, should have the following code:

#!/usr/bin/sh
export PATH=/usr/bin

However, in your code, you have not specified a location for the file "statistic_www". It will need a fullpath to ensure it goes where you want it. cron runs your script on your behalf but never performs a login so none of your /etc/profile and .profile will be run. And to further troubleshoot the problem, add the command set -x at the beginning of the script.


Bill Hassell, sysadmin
john korterman
Honored Contributor

Re: works on command line but does not work in cron

Hi amonamon,

have you looked for statistic_www
in the cron user's home directory?
Does the mail to the cron user give any clue?

regards,
John K.
it would be nice if you always got a second chance
hari prasad_4
Advisor

Re: works on command line but does not work in cron

hi,

I guess you have not defined which shell you are using, try defining in the first line of the script.

regards
hari
amonamon
Regular Advisor

Re: works on command line but does not work in cron

OK please...how is then possible if I am having problems with path to be able to ececute script that I published 3 posts before???
as U can see it is almost the same script no export PATH, no specificatio on shell, no path to statistic_www...and this script works just fine in cron! but script that I published in first post can not be completely executed in cron..

any clue??
thanks I hope that someone will give me a good hint which I have not tried yet.
hari prasad_4
Advisor

Re: works on command line but does not work in cron

hi,
what i mean to say is,an example:
if you are using sh then define it as #!/bin/sh


rgds,
Yang Qin_1
Honored Contributor

Re: works on command line but does not work in cron

two simple cheap suggestions:
1. Can you check your /var/adm/cron/log and see if you have error there?

2. Can you add "set -x" in your cronjob either at the top or before the second nawk ... and check /var/adm/cron/log after cronjon run
Stefan Schulz
Honored Contributor

Re: works on command line but does not work in cron

Hi,

did you get the zawww file? I think like others that this is a problem of the PATH variable. So if you dont get the zawww file, then try to use full path for cat and echo commands or set the PATH in your script to suite your needs.

Hope this helps

Regards Stefan
No Mouse found. System halted. Press Mousebutton to continue.
Steve Post
Trusted Contributor

Re: works on command line but does not work in cron

You KNOW it's the environment. You just haven't found out how.

1. add this to the top of you little program.
"/usr/bin/env > /tmp/my_env.txt"
2. run it by hand.
3. mv /tmp/my_env.txt /tmp/my_env.byhand.
4. run it by cron.
5. mv /tmp/my_env.txt /tmp/my_env.bycron.
6. cd /tmp
7. diff /tmp/my_env.byhand /tmp/my_env.bycron

8. look at the differences in the environment between running the job by hand and running the job by cron.
amonamon
Regular Advisor

Re: works on command line but does not work in cron

OK let me find what will be resoult of this command..

I will reply soon..I hope that is an issue
amonamon
Regular Advisor

Re: works on command line but does not work in cron

#cat my_env.crontab¸

HOME=/
LOGNAME=root
PATH=/usr/sbin:/usr/bin
SHELL=/usr/bin/sh
TZ=MET

# cat my_env.manual

PMTAG=inetd
PWD=/export/home/roberto
ISTATE=enabled
TZ=MET
LC_MESSAGES=C
HZ=100
HOSTNAME=x5sel6
LD_LIBRARY_PATH=/usr/dt/lib:/usr/openwin/lib:/opt/SMAW/lib:/opt/Navisphere/lib
OMNI_HOME=/export/home/omni
PERL5LIB=:/opt/SMAW/SMAWsuf
NLSPATH=:/export/home/omni/locale/%L/%N.cat:/export/home/omni/locale/%L/%N
CLASSPATH=/export/home/omni/classes/Signalware.jar:
MANPATH=/usr/share/man:/opt/SMAW/man:/usr/dt/man:/usr/openwin/man:/usr/share/man:/export/home/omni/man:/usr/local/man:/opt/nsr/man:/opt/Navisphere/man
ENV=/.kshrc
_DVFS_RECONFIG=YES
LC_TIME=en_US.ISO8859-1
MACHTYPE=sparc-sun-solaris
XFILESEARCHPATH=/usr/dt/%T/%N%S:/usr/openwin/lib/%T/%N%S:
MAIL=/var/mail/root
EDITOR=vi
LANG=C
LC_NUMERIC=en_US.ISO8859-1
LOGNAME=root
SHLVL=2
LC_CTYPE=en_US.ISO8859-1
_=/usr/bin/env
SHELL=/bin/bash
HOSTTYPE=sparc
OSTYPE=solaris
HOME=/
TERM=vt100
PATH=/usr/local/sbin:/usr/local/bin:/export/home/omni/java/bin:/export/home/omni/bin:/usr/dt/bin:/usr/openwin/bin:/usr/sbin:/usr/bin:/opt/SMAW/bin:/opt/SMAW/sbin:/opt/nsr:/opt/FJSVhwr/sbin:/opt/SMAW/SMAWsuf/bin:/opt/Navisphere/bin
LC_MONETARY=en_US.ISO8859-1
SHM=0
LC_COLLATE=en_US.ISO8859-1


so U were right it is not a same..but what should I do now in my case..

please looking forward of hearing from U
Stefan Schulz
Honored Contributor

Re: works on command line but does not work in cron

Hi,

as mentioned somewhere above do a export PATH=.... at the beginning of your script.

The PATH variable should contain all necessary paths to the commands and tools you use in your script (typically /usr/bin:/usr/sbin).

That should do the trick.

Kind Regards

Stefan
No Mouse found. System halted. Press Mousebutton to continue.
amonamon
Regular Advisor

Re: works on command line but does not work in cron

well-- stefan thank U very much..but I am new to solaris..as U can notice..and I am little bit scared of this path export...

if I put export PATH=...at the beginning of my script does it mean that path variables will be changed only during script execution in cron or forever???
If forever is the case than, I could create some other possible troubles..

what do U think?
Steve Post
Trusted Contributor
Solution

Re: works on command line but does not work in cron

Ok this should work.
I took your local env data and removed anything that looked specific to your login. I.e TERM, LOGIN, PWD....

You have two choices for this text.
1. Put text in your program you are running. Put the text just below the #!/bin/??? that is on the top line.

2a. Make a file like mycron.env and put this stuff in in.
2b. Put the following line in your program you are running. Put it just below the #!/bin/??? that is on the top line.
the line:
. /xxxx-the-full-path-xxxxx/mycron.env


Then try running the stuff. Besides the PATH needing to go in, I also see PERL5LIB is set, as well as a pile of other stuff.

Here is your mycron.env.
Steve

the text:
PMTAG=inetd
ISTATE=enabled
TZ=MET
LC_MESSAGES=C
HZ=100
HOSTNAME=x5sel6
LD_LIBRARY_PATH=/usr/dt/lib:/usr/openwin/lib:/opt/SMAW/lib:/opt/Navisphere/lib
OMNI_HOME=/export/home/omni
PERL5LIB=:/opt/SMAW/SMAWsuf
NLSPATH=:/export/home/omni/locale/%L/%N.cat:/export/home/omni/locale/%L/%N
CLASSPATH=/export/home/omni/classes/Signalware.jar:
MANPATH=/usr/share/man:/opt/SMAW/man:/usr/dt/man:/usr/openwin/man:/usr/share
/man:/export/home/omni/man:/usr/local/man:/opt/nsr/man:/opt/Navisphere/man
ENV=/.kshrc
_DVFS_RECONFIG=YES
LC_TIME=en_US.ISO8859-1
MACHTYPE=sparc-sun-solaris
XFILESEARCHPATH=/usr/dt/%T/%N%S:/usr/openwin/lib/%T/%N%S:
LANG=C
LC_NUMERIC=en_US.ISO8859-1
LC_CTYPE=en_US.ISO8859-1
SHELL=/bin/bash
HOSTTYPE=sparc
OSTYPE=solaris
PATH=/usr/local/sbin:/usr/local/bin:/export/home/omni/java/bin:/export/home/omni/bin:
/usr/dt/bin:/usr/openwin/bin:/usr/sbin:/usr/bin:/opt/SMAW/bin:/opt/SMAW/sbin:/opt/nsr:/o
pt/FJSVhwr/sbin:/opt/SMAW/SMAWsuf/bin:/opt/Navisphere/bin
LC_MONETARY=en_US.ISO8859-1
SHM=0
LC_COLLATE=en_US.ISO8859-1
Steve Post
Trusted Contributor

Re: works on command line but does not work in cron

sorry about that. I'm typing while you're typing too. The PATH variable and the others exist only for the job because they are either in the file (like I said for option1), or sourced to the file (like I said for option2).
Once the job is done, the variables are cleared. You already painfully know this. Most of your variables are NOT loaded when you run your script as a cronjob.
Stefan Schulz
Honored Contributor

Re: works on command line but does not work in cron

Hi,

as this cron job runs in a seperate shell the export PATH is only valid for this shell as long as your script is running. With the end of your script the shell and with it the export PATH dies.

No other process on your system will recognize the different setting on the path variable.

Hope this helps

Regards Stefan
No Mouse found. System halted. Press Mousebutton to continue.
amonamon
Regular Advisor

Re: works on command line but does not work in cron

so basicly it is 100% safe to do export PATH..and I should not have any other problems with it??

Or I missunderstood you
Steve Post
Trusted Contributor

Re: works on command line but does not work in cron

I was being polite and waiting for Stefan.
Oh well. Run "man export". You'll see that "export PATH" means the PATH will stay as you proceed further INTO the shell.
Uh...that sounds a little goofy.

You have 3 files: f1.sh, f2.sh and f3.sh

contents f1.sh
MYVAR="this is a test"
MYVAR2="this is a test2"
export MYVAR2
echo "in f1 MYVAR is $MYVAR MYVAR2 is $MYVAR2"
sh ./f2.sh

contents of f2.sh
echo "in f2 MYVAR is $MYVAR MYVAR2 is $MYVAR2"
sh ./f3.sh

contents of f3.sh
echo "in f3 MYVAR is $MYVAR MYVAR2 is $MYVAR2"


Now go ahead make the 3 files and run them.
Now pretend that MYVAR is really PATH. You should get your own answer this way.

Go ahead and change MYVAR and/or MYVAR2 to PATH and see what happens. You'll see that nothing nasty happens at all.

steve
amonamon
Regular Advisor

Re: works on command line but does not work in cron

Thanks a lot for this answer I hope that this solves my initial problem that I posted in post #1..

Thanks, :)
Stefan Schulz
Honored Contributor

Re: works on command line but does not work in cron

Hi again,

hope your problem will be solved with adding the PATH variable.

@Steve: Thanks for waiting and also thanks for taking over. I was away from keybord at that time and couldn't answer.

Kind regards

Stefan

BTW: how about assigning points to the other replies? Everybody posting here tried to help you, and you might want to say "thank you" by assigning points. (Please no points for this reply).
No Mouse found. System halted. Press Mousebutton to continue.