[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Running ksh script from a crontab job

Posted on 2013-06-24
10
Medium Priority
?
2,721 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 78

Accepted Solution

by:
slightwv (䄆 Netminder) earned 2000 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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 78

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 78

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
Suggested Courses
Course of the Month19 days, 8 hours left to enroll

872 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