Solved

Running ksh script from a crontab job

Posted on 2013-06-24
10
2,248 Views
Last Modified: 2013-06-24
I have a ksh script that writes data to a file and then sends out an e-mail with that file embeded into the e-mail. I need this job to run every Sunday at 3am. Below is my crontab entry:

00 03 * * 0 Scripts/myscript.ksh 2>&1

At exactly 3am I get an e-mail with the correct subject but there is no content - the file does not get embedded.  If I run my ksh script manually i get an e-mail with a content.

I have one idea - in my script the full path to the file to be e-mailed is $ORACLE_BASE/Scripts/myscript.ksh, and I know crontab does not allow environmental variables. Of course the variable is in the ksh script but I have no clue if it makes any difference to the crontab.

P.S I also knoe that listing the path to the script as Script/myscript.ksh is fine because that's how all other scripts are listedin the crontab.

Does anyone have any ideas why my file does not get e-mailed?

The command I use to e-mail the file is:

mail -s "My subject" "$email1" < $FILE1
0
Comment
Question by:YZlat
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
Comment Utility
>>and I know crontab does not allow environmental variables.

Scripts executed from cron need to manually set necessary environment variables.  You must have an environment variable set in your environment that the script needs.  This is why you can run it and cron cannot.
0
 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
>> Of course the variable is in the ksh script  <<

What exactly does that mean?

As you probably meant with "crontab does not allow ...", crontab does not provide the user's standard environment, so you must set all environment variables inside the script itself, one way or the other.

It's quite usual to "source" an initalization file, like .oraenv.

cron can run "Scripts/myscript.ksh" not because it knows what $ORACLE_BASE is but because  $ORACLE_BASE is the $HOME of the crontab user, and the user's home directory is in the PATH set by cron.

>> the full path to the file to be e-mailed is $ORACLE_BASE/Scripts/myscript.ksh <<

Do you really want to mail the script itself?

What's the actual content of $FILE1? Does it include a path?

The PATH variable set by cron well contains the user's home, but PATH is only for searching executables, not data files.
0
 
LVL 5

Expert Comment

by:DOSLover
Comment Utility
Does the file get created? After 3am job, do you see the file in the designated directory?  Also please check your .profile settings. Any settings from your profile that are used when you manually login and run the script will not be available from cron. The script has to explicitly set those environment variables. Also, do you see any errors in the log? You can change cron to write to a log:
00 03 * * 0 Scripts/myscript.ksh 2>&1 >> /yourhome/log/myscriptlog.log
0
 
LVL 2

Expert Comment

by:guruworld
Comment Utility
Hi
Try this..just to force cron export environment variables before excuting

On Linux
00 03 * * 0 . /home/username/.bash_profile; Scripts/myscript.ksh /tmp/out.log 2>&1

On Solaris
00 03 * * 0 . /export/home/username/.profile; Scripts/myscript.ksh /tmp/out.log 2>&1

I recommend you specify absolute path to script(Dont be like the others :) ).
/.../Scripts/myscript.ksh
0
 
LVL 35

Author Comment

by:YZlat
Comment Utility
slightwv, by manually setting variables you mean something like that?

ORACLE_BASE=/u01/app/oracle
export ORACLE_BASE

woolmilkporc, it's a typo. Instead of  $ORACLE_BASE/Scripts/myscript.ksh it should have been  $ORACLE_BASE/Scripts/mytextfile.log


DOSlover, yes, the file gets created whether I run it with crontab or manually, it just does not get e-mailed, even though e-mail does get sent out
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
Comment Utility
>>by manually setting variables you mean something like that?

yes.  Or all on one line:
export ORACLE_BASE=/u01/app/oracle
0
 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
As I wrote: The PATH variable set by cron well contains the user's home, but PATH is only for searching executables, not data files.

So take measure to have $ORACLE_BASE set inside the script, either manually or by sourcing an initalization file.
0
 
LVL 35

Author Comment

by:YZlat
Comment Utility
just checked the file generated at 3am and it is empty.

So I think the issues might be permissions. When I run it manually, I am logged in with my account to the server and then I do su - oracle to run as an oracle user. But what does it run as when it runs from crontab?
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
Comment Utility
>>But what does it run as when it runs from crontab?

It runs as the user/owner of the crontab file.

Is oracle running the cron file or is root?

I suggest having the sys admins set oracle up as a cron user.  Then you, as the dba, can set up your own cron entries.
0
 
LVL 5

Expert Comment

by:DOSLover
Comment Utility
If the file is created, but empty, it is very likely that cron job (that runs with your login) has a problem when it tries to retrieve data from the database. How does the script connect to the db? as "/ as sysdba" or does the script supply credentials. Please try supplying explicit credentials to connect to the database.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now