Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Running ksh script from a crontab job

Posted on 2013-06-24
10
Medium Priority
?
2,665 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 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

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…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
This video shows how to recover a database from a user managed backup
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…
Suggested Courses

609 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