[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 553
  • Last Modified:

setting up ksh script as a cron job

I have a shell script which works fine which writes the log to a file whenever there is an error or failure. it works fine if I run the script from console. But if I try to set up the script as the cron job. It doesn't write anything to the log file. please advise.
0
saibsk
Asked:
saibsk
  • 8
  • 5
  • 3
  • +2
3 Solutions
 
zwseemmCommented:
sounds like the cron service doesn't have access to the folder or log file.
0
 
saibskAuthor Commented:
How can I make the cron access the log file?
0
 
TintinCommented:
Please not show us your script.  It probably uses relative paths or makes use of environment variables.  Remember that cronjobs run in a minimal environment, so if you haven't setup the required environment in the script, then it may not run correctly.
0
[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

 
omarfaridCommented:
I agree with Tintin. You need to use full path names to the files you use or commands. Also, if the script uses env variables then they should be set in the script.

e.g.

if you use command like

ls -l $HOME >> $HOME/my.log

in your script then you need to change it to

HOME=/export/home/username
export HOME
/usr/bin/ls -l $HOME >> $HOME/my.log 2>&1


0
 
saibskAuthor Commented:
I set up the cron job something like this

35 10 1 2 5  cd /export/home/user/; ./qa_envs_0129.ksh 2> ./qa_envs_0129.err 1>&1

#!/bin/ksh

HOME=/export/home/user
export HOME

if [ -f $HOME/client_report.out ]
then
rm $HOME/client_report.out
fi


echo "I am here"
clientInst=`awk '{print $1}' $HOME/client_instances.txt`
echo "$clientInst\n"

for client in $clientInst
do

results=`/opt/syb1253/OCS-12_5/bin/isql -w200 -Sclient -Uuser -Ppasswd <<EOF
set nocount on
go
select ip_address from client_info where environment = 'NON-PROD' and dbName = "$client"
go
EOF`

----
----

when I check the qa_envs_0129.err file it doesn't even print the statement "I am here". Actually it doesn't echo anything to the err log file. Please advise.
0
 
ahoffmannCommented:
> when I check the qa_envs_0129.err file it doesn't even print the statement "I am here".
for sure, cause it countains output of STDERR only, while echo goes to STDOUT
you probably want to have:
  ./qa_envs_0129.ksh 2> ./qa_envs_0129.err 1>&2
0
 
saibskAuthor Commented:
It is now writing to the err log. Thanks. For the isql statement it says
CT-LIBRARY error:
        ct_connect(): directory service layer: internal directory control layer error: Requested server name not found

But when I type the isql statement directly on the console it works fine. Please advise.
0
 
ahoffmannCommented:
> But when I type the isql statement directly on the console it works ..
I guess you're missing some environment variables available to your console user.
0
 
saibskAuthor Commented:
how do I check the diiference
0
 
saibskAuthor Commented:
echo whoami from the script which i scheduled the script as cron job. It prints nothing. How do i fix this? When I run the script from console it prints my username.
0
 
TintinCommented:
I'd define your cronjob as:

35 10 1 2 5  /export/home/user/qa_envs_0129.ksh >/export/home/user/qa_envs_0129.err 2>&1

and your script

#!/bin/ksh
cd /export/home/user
rm -f client_report.out

echo "I am here"
clientInst=`awk '{print $1}' client_instances.txt`
echo "$clientInst\n"

for client in $clientInst
do

results=`/opt/syb1253/OCS-12_5/bin/isql -w200 -Sclient -Uuser -Ppasswd <<EOF
set nocount on
go
select ip_address from client_info where environment = 'NON-PROD' and dbName = "$client"
go
EOF`
0
 
saibskAuthor Commented:
still I am getting the same error. Please advise.
0
 
TintinCommented:
I wasn't suggesting my version would correct the error, I was just pointing out it was a more concise way of writing the script and cronjob.

To determine which environment setup you need for isql to run correctly.  From the command line do

env >/tmp/env1

and from the cronjob do

env >/tmp/env2

then do

diff /tmp/env1 /tmp/env2

and look for any obvious differences in environment variables related to isql.
0
 
saibskAuthor Commented:
The PATH is not set up for isql. How do I update these changes for the cron job?
0
 
TintinCommented:
In your script put

export PATH=$PATH:/path/to/dir/where/isql/lives
0
 
saibskAuthor Commented:
I did the above. But there many other environment variables missing like USER, SYBASE I think I need them to run this cron job.

The env is set up something like this:

 SYBASE=/opt/syb1253
 USER=wasuser

so do I write something like this in my script

export SYBASE=$SYBASE:/opt/syb1253.

Please advise.
0
 
omarfaridCommented:
Hi,

You may source your .profile at the beginning of the script:

e.g.

. /path/to/myhomedir/.profile

Please note the dot at the beginning of the line above.



0
 
TintinCommented:
You should just need

export SYBASE=/opt/syb1253
0
 
ahoffmannCommented:
> how do I check the diiference
(your environment) use the
  set
command in your shell. Check which variables probably belong to isql (PATH already mentioned), then set them in your script called by cron (see the example of the already existing HOME variable there).

@Tintin
> export PATH=$PATH:/path/to/dir/where/isql/lives
take care! this does not work in Bourne shell (sh) as used by cron (not talking about crapy systems using a faked sh like some Linux do):
Better use:
  PATH=$PATH:/path/to/dir/where/isql/lives; export PATH
which works in any sh and friends, since centuries ;-)
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 8
  • 5
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now