Solved

Running ksh script from a crontab job

Posted on 2013-06-24
10
2,392 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 39271335
>>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
ID: 39271364
>> 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
ID: 39271413
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:guruworld
ID: 39271602
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
ID: 39271681
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
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39271693
>>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
ID: 39271699
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
ID: 39271865
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 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39271874
>>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
ID: 39272345
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
replacing file name with spaces in ibm aix. 2 34
FTP Script 25 67
update using pipeline function 3 34
Korn Shell Script - Is there a way to LOOP it? 13 46
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
Utilizing an array to gracefully append to a list of EmailAddresses
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

730 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