[SOLVED] Unable to shell script thru crontab but the same runs manually
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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/bashPATH=$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.
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.
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"
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.
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 ....
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.
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 -A /home/db2inst1/sqllib/db2dump /home/db2inst1/sqllib/db2dump/db2diag.log---> dir of file ... file name
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 -A /home/db2inst1/sqllib/db2dump /home/db2inst1/sqllib/db2dump/db2diag.log---> dir of file ... file name
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.