Solved

Running ksh script from a crontab job

Posted on 2013-06-24
10
2,338 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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 76

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 76

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Utilizing an array to gracefully append to a list of EmailAddresses
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

789 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