LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-25-2012, 07:18 AM   #1
mukheja.nitin
LQ Newbie
 
Registered: Sep 2012
Posts: 5

Rep: Reputation: Disabled
Unable to shell script thru crontab but the same runs manually


# cat zz.sh
cd /home/db2inst1/sqllib/db2dump
file_size=$(du -k db2diag.log |awk '{print $1}')
if [ $file_size -gt 2 ]
then
. /home/db2inst1/sqllib/db2profile
db2diag -A
mv db2diag.log_2012* /OPT/IBM/DB2/CMSTEMP/db2diag/
chown db2inst1:dbadmin1 db2diag.log
else
echo "No need to move diag.log as size is less"
fi
# pwd
/
# ls -al zz.sh
-rwxrwxrwx 1 root system 300 Sep 25 17:39 zz.sh
# whoami
root
# crontab -l
# @(#)08 1.15.1.3 src/bos/usr/sbin/cron/root, cmdcntl, bos610 2/11/94 17:19:47
# IBM_PROLOG_BEGIN_TAG
# This is an automatically generated prolog.
#
# bos610 src/bos/usr/sbin/cron/root 1.15.1.3
#
# Licensed Materials - Property of IBM
#
# COPYRIGHT International Business Machines Corp. 1989,1994
# All Rights Reserved
#
# US Government Users Restricted Rights - Use, duplication or
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#
# IBM_PROLOG_END_TAG
#
# COMPONENT_NAME: (CMDCNTL) commands needed for basic system needs
#
# FUNCTIONS:
#
# ORIGINS: 27
#
# (C) COPYRIGHT International Business Machines Corp. 1989,1994
# All Rights Reserved
# Licensed Materials - Property of IBM
#
# US Government Users Restricted Rights - Use, duplication or
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#
#0 3 * * * /usr/sbin/skulker
#45 2 * * 0 /usr/lib/spell/compress
#45 23 * * * ulimit 5000; /usr/lib/smdemon.cleanu > /dev/null
0 11 * * * /usr/bin/errclear -d S,O 30
0 12 * * * /usr/bin/errclear -d H 90
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /usr/sbin/dumpctrl -k >/dev/null 2>/dev/null
0 15 * * * /usr/lib/ras/dumpcheck >/dev/null 2>&1
55 23 * * * /var/perf/pm/bin/pmcfg >/dev/null 2>&1 #Enable PM Data Collection
59 23 * * * /var/perf/pm/bin/pmcfg -T >/dev/null 2>&1 #Enable PM Data Transmission
00 02 * * 1 /usr/ibm/common/acsi/bin/collectbatch.sh > fsout.log 2>&1
* * * * * /zz.sh > z.out 2>&1

Above is my script and path from where i am running.
kindly help in the same...
 
Old 09-25-2012, 07:55 AM   #2
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
I think you need to add the header
Code:
#!/bin/bash
or
Code:
#!/bin/sh
 
Old 09-25-2012, 08:06 AM   #3
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
When you run things at command line they inherit your environment as set by things such as /etc/profile, $HOME/.bashrc, $HOME/.profile and/or $HOME/.bash_profile. There are many environment variables that can impact a script. Chief among these is PATH. If you run "echo $PATH" at command line you'll see what yours is currently set to. There are other environment variables that might have an impact if you relied on them.

When you run things in background such as a cron job or init script there is typically a minimal environment setup. PATH is apt to have a lot fewer directories in it by default.

Therefore it is required that you make the script so it either has the environment variables needed or doesn't rely on environment variables. Also you should specify the shell you intend to use in the script itself.

In your script you're invoking environment variables by sourcing the db2profile file but you're doing it AFTER you try to use some commands. Also I don't know what is in your db2profile so even if you sourced it before using commands it might not have the PATH to the commands you're using.

So for example you need to know where the commands you're using are. You can find out by typing "which <command>" e.g. "which du" might tell you that du is /usr/bin/du and "which awk" might tell you awk is /bin/awk. You should do the which for other commands you're using as well such as "mv" and "chown". You then should insure the PATH variable within the script itself contains the directories with these commands.

So for the first two commands your script should be modified as follows:

Code:
#!/bin/bash
PATH=$PATH:/bin:/usr/bin
cd /home/db2inst1/sqllib/db2dump
file_size=$(du -k db2diag.log |awk '{print $1}')
if [ $file_size -gt 2 ]
then
. /home/db2inst1/sqllib/db2profile
db2diag -A
mv db2diag.log_2012* /OPT/IBM/DB2/CMSTEMP/db2diag/
chown db2inst1:dbadmin1 db2diag.log
else
echo "No need to move diag.log as size is less"
fi
Notice the first line tells it to run the rest of the script using the bash shell. If you wanted to run it as ksh you'd put in the path to that after the "#!". This is called the interpreter line and you should get in the habit of putting it in all scripts. (You can even put in the path for perl at the top of a perl script.)

The second line adds /bin and /usr/bin to whatever is already set for PATH variable. (If nothing is set it simply adds these two directories. Notice that each element in PATH is separated by a colon. Remember to do the which for other commands you're using to be sure they're in your PATH as well.

An alternative to adding PATH would be to put in fully qualified directory name each time you invoke a command e.g.

Code:
file_size=$(/usr/bin/du -k db2diag.log |/bin/awk '{print $1}')
That's easy enough for a small script like yours but in much larger ones it would get cumbersome rather quickly.
 
1 members found this post helpful.
Old 09-25-2012, 10:10 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
In addition to that well-written post: 'du' can be too coarse at times and then you can 'stat -c %s /path/to/file', you don't want to move a file that's in use by a process and to check it is or not use the exit value of 'fuser /path/to/file >/dev/null 2>&1' and if you disable skulker then ensure you've got another automated process cleaning out directories holding temporary files.
 
Old 09-26-2012, 02:17 AM   #5
mukheja.nitin
LQ Newbie
 
Registered: Sep 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thanks for the wonderful explanation ...
i have made some changes as per the suggestions...but still i am facing the problem.
---------------------------------------------------
PHP Code:
#!/usr/bin/ksh
. /home/db2inst1/.profile
PATH
=$PATH:/bin:/usr/bin:/home/db2inst1/sqllib/bin:/home/db2inst1/sqllib
cd 
/home/db2inst1/sqllib/db2dump
file_size
=$(du -k db2diag.log |awk '{print $1}')
if [ 
$file_size -gt 2 ]
then
db2level
pwd
db2diag 
-A
mv db2diag
.log_2012* /OPT/IBM/DB2/CMSTEMP/db2diag
chown db2inst1
:dbadmin1 db2diag.log
db2 get dbm cfg 
grep -i mem
else
echo 
"No need to move diag.log as size is less"
fi 
--------------------------------------------------------
in above script db2level and db2 get dbm cfg | grep -i mem works fine but the command
db2diag -A is not working ..... this command works when i run the script manually
PHP Code:
db2diag -A
db2diag
Moving "/home/db2inst1/sqllib/db2dump/db2diag.log"
         
to     "/home/db2inst1/sqllib/db2dump/db2diag.log_2012-09-26-12.36.05" 
-----------------------------------------------------------------------------------
PHP Code:
which db2diag
/home/db2inst1/sqllib/bin/db2diag
which db2level
/home/db2inst1/sqllib/bin/db2level 
May be i am again missing something...!!
 
Old 09-26-2012, 02:43 AM   #6
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Try to temporarily change the line to this first then see the log after running.
Code:
# db2diag -A
{
    which db2diag
    db2diag -A
} >/somewhere/db2diag.log 2>&1
 
Old 09-26-2012, 04:21 AM   #7
mukheja.nitin
LQ Newbie
 
Registered: Sep 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
Red face

Adding the given code to script generates another log file db2diag.log with entry :
# cat db2diag.log
/home/db2inst1/sqllib/bin/db2diag

The command 'which db2diag' gets executed but the command 'db2diag -A' is still not executing ...

Actually 'db2diag -A' = 'db2diag -archive path' ......... and it does not require any db authorization...so it should work
 
Old 09-26-2012, 04:35 AM   #8
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
I wonder, but perhaps you have to run it as a different user. Does 'db2diag -A' work if you are root? Also, '. /etc/profile' at the beginning of the file might be an option.
 
Old 09-26-2012, 05:40 AM   #9
mukheja.nitin
LQ Newbie
 
Registered: Sep 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
Red face

after adding . /etc/profile still the issue remains.
Also if i give db2diag -t it runs and gives below output
# cat z.out
db2diag: Expecting a time interval for the "-t" option.

but as i give the timestamp also the output file is empty....
db2diag -t 2012-09-26

passing the arguments to script thru crontab is not working but when script runs manually it works fine.
The root user actually can not run the db2diag command but as the . /home/db2inst1/profile is loaded i can execute the db2 commands thru root also except thru crontab ....
 
Old 09-26-2012, 05:44 AM   #10
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
I wonder if inserting a dummy input would do the trick. Just try
Code:
# db2diag -A
{
    which db2diag
    exec </dev/null
    db2diag -A
} >/somewhere/db2diag.log 2>&1
---- Edit ----

Oops. Please try the script first before running with crontab. Seems you're not using bash but ksh.

Or just do it like this:
Code:
# db2diag -A
{
    which db2diag
    db2diag -A
} </dev/null >/somewhere/db2diag.log 2>&1
---- Edit ----

Or better yet you could test if having a missing stdin is the cause of the problem by running the script with closed input e.g.
Code:
bash|ksh script.sh <&- # closed; might not work with ksh
bash|ksh script.sh </dev/null # or have /dev/null

Last edited by konsolebox; 09-26-2012 at 06:08 AM.
 
Old 09-26-2012, 08:48 AM   #11
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
In your original script you were sourcing another environment file:
Code:
. /home/db2inst1/sqllib/db2profile
I don't see that line in your revised script.

When you "source" a file (as opposed to "executing" it) you're saying to run the file and put any environment settings it makes into your current environment.

It wasn't my intent to suggest you remove that line - I was merely noting that it was being sourced AFTER you used commands like awk and du that might not be in your PATH. There may be (likely are) other environment settings within that file that you need. DB2 isn't something I familiar with but for Oracle there are many environment settings needed so I suspect there are for DB2 as well.

You should "cat /home/db2inst1/sqllib/db2profile" and examine what is in it. Specifically you should check to see if it changes PATH as it could overwrite the one you've set. (If it does something like PATH=$PATH:... then it is incorporating your existing PATH.) Also you should check to see if it has any sourcing lines like the above - often profiles of this nature in turn source other files and any other such files sourced by this one would also be in your environment.
 
Old 10-03-2012, 03:29 AM   #12
mukheja.nitin
LQ Newbie
 
Registered: Sep 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thumbs up

Thanks for the help guys .............

The issue got resolved , db2diag cant be used to pass arguments in cron and -readfile option is used instead for this purpose .
instead of db2diag -A
using either of below entries resolved the issue :

PHP Code:
db2diag -/home/db2inst1/sqllib/db2dump /home/db2inst1/sqllib/db2dump/db2diag.log---> dir of file ... file name

db2diag 
-/OPT/IBM/DB2/CMSTEMP/db2diag -readfile /home/db2inst1/sqllib/db2dump/db2diag.log ----> target dir.... -readfile source file 
 
1 members found this post helpful.
Old 10-11-2012, 01:41 PM   #13
dba80
LQ Newbie
 
Registered: Oct 2012
Posts: 1

Rep: Reputation: Disabled
Quote:
Originally Posted by mukheja.nitin View Post
Thanks for the help guys .............

The issue got resolved , db2diag cant be used to pass arguments in cron and -readfile option is used instead for this purpose .
instead of db2diag -A
using either of below entries resolved the issue :

PHP Code:
db2diag -/home/db2inst1/sqllib/db2dump /home/db2inst1/sqllib/db2dump/db2diag.log---> dir of file ... file name

db2diag 
-/OPT/IBM/DB2/CMSTEMP/db2diag -readfile /home/db2inst1/sqllib/db2dump/db2diag.log ----> target dir.... -readfile source file 
I have similar script but it reads error messages from db2diag.log, I have issues that db2diag process starts to span a lot whenever the system is rebooted. Have any one faced this issue? or know how to provent it?

Thanks.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Script runs from shell -but not from crontab rch Programming 5 07-24-2012 04:07 PM
[SOLVED] bash says "no such file or directory" when crontab runs the shell script ymnoor21 Linux - Newbie 16 03-23-2012 11:18 AM
crontab wont work - runs manually fine alabama78 Linux - Newbie 5 08-02-2010 07:12 AM
crontab wont work - runs manually fine alabama78 Linux - Newbie 5 07-28-2010 01:17 PM
Runs Multiple shell script inside a main script using crontab srimal Linux - Newbie 4 10-22-2009 06:19 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration