Operating System - HP-UX
1833776 Members
1882 Online
110063 Solutions
New Discussion

Script runs fine from command line not as cron job.

 
SOLVED
Go to solution
rmueller58
Valued Contributor

Script runs fine from command line not as cron job.

I have a script that I am running periodically to check how many instants of a database connection our school districts have at any one time throughout a given period.

The purpose is for future planning reasons, I think we are butting heads with a license limit.. When I run it manually I get the output desired:
# dbgo.sh
Arlington User Count
0
Blair User Count
0
Conestoga User Count
0
DCWest User Count
0
ESU User Count
8
ELKHORN User Count
6
FT Calhoun User Count
0
Gretna User Count
1
Millard User Count
21
Papillion LV:User Count
10
Ralston User Count
1
Westside User Count
7

When ran in cron the output shows "0"

Here are the scripts.. I am hoping someone with better eyes might be able to tell me what I am doing wrong.

SCRIPT#1
# cat dbusr
for duser in `/usr/informix/bin/onstat -g ses|/usr/bin/grep -v informix |/usr/bin/awk '{print $2}'`
do
grep $duser /etc/passwd
done

SCRIPT#2 (want to run in cron to build log)
# cat dbgo.sh
/usr/local/bin/dbusr > /tmp/dbusr.out


echo "Arl User Count" > /tmp/dbusr.log
grep '\/arl\/' /tmp/dbusr.out |wc -l >> /tmp/dbusr.log

echo "Blr User Count" >> /tmp/dbusr.log grep '\/blr\/' /tmp/dbusr.out |wc -l >> /tmp/dbusr.log

.......

echo "WST User Count" >> /tmp/dbusr.log grep '\/wst\/' /tmp/dbusr.out |wc -l >> /tmp/dbusr.log

Any insight appreciated

Rex M - ESU#3
7 REPLIES 7
F Verschuren
Esteemed Contributor
Solution

Re: Script runs fine from command line not as cron job.

please ad one line in your script:
first line:
#!/bin/ksh

than it wil work fine.

kind reagrad,

Freek Verschuren
rmueller58
Valued Contributor

Re: Script runs fine from command line not as cron job.

My head has been plugged up lately.. Thanks.. I missed it.
rmueller58
Valued Contributor

Re: Script runs fine from command line not as cron job.

Still missing something as cron.

put in the

#!/usr/bin/sh

Patrick Wallek
Honored Contributor

Re: Script runs fine from command line not as cron job.

You must remember that cron has a VERY LIMITED environment. Your PATH is not the same and very few environment variables will be set.

To see precisely what your environment is, put an 'env' in your script and let it run via cron. Then compare that with the output of 'env' from the command prompt. You will then need to set any needed variables explicitly in your script.
James R. Ferguson
Acclaimed Contributor

Re: Script runs fine from command line not as cron job.

Hi Rex:

Remember, that when a crontask runs, its environment is sparse. This is documented in the 'crontab(1)' manpages.

If you are relying on environmental variables that are build during a login shell (i.e. as defined in a '.profile') then unless you source (read) that profile (or antoher file that defines them), your cron'ed environment lacks them.

Regards!

...JRF...
rmueller58
Valued Contributor

Re: Script runs fine from command line not as cron job.

executing the profile in the script resolved the problem.

Thanks. I need to go home and take some nyquil.
rmueller58
Valued Contributor

Re: Script runs fine from command line not as cron job.

Thanks All.