LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 08-28-2004, 11:58 AM   #1
gepesz
LQ Newbie
 
Registered: Jun 2004
Location: Washington DC, US
Distribution: Debian, Kubuntu
Posts: 23

Rep: Reputation: 15
/etc/crontab & environment variables


Hello gurus,

I've been trying to execute a daily sh script with /etc/crontab but I seem to run into problems regarding environment variables. I defined all normal users' environment variables in /etc/profile, such as $JAVA_HOME, $PATH etc.

The problem is that $JAVA_HOME cannot be found, which is used in the daily sh script. My /etc/crontab starts like this:

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
.................

Seems like it uses sh shell, not bash.

1) Does /etc/profile set the environment variables for bash shell ONLY?
2) Where do I set the env vars for sh shell?
3) Can I simply put JAVA_HOME=/usr/j2sdk/ at the top of /etc/crontab, ie declare env vars in the /etc/crontab file itself?
 
Old 08-28-2004, 12:48 PM   #2
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Blue Ridge Mountain
Distribution: Linux Mint 17, Debian 8
Posts: 7,865

Rep: Reputation: 311Reputation: 311Reputation: 311Reputation: 311
"1) Does /etc/profile set the environment variables for bash shell ONLY?"

Quite possibly. sh is written to an older set of standards than bash. They are not 100% compatible. However in many cases distributions link sh -> bash. When bash detects that it has been invoked as sh then bash mimics the behavior of the older sh program.

"2) Where do I set the env vars for sh shell?"

bash sets the environmental variables by executing the commands in /etc/profile. I would guess that you can set the sh environmental variables during boot by having sh execute a copy of /etc/profile which has been changed to conform to sh syntax. I don't know how to do that on Debian (or SuSE for that matter).

"3) Can I simply put JAVA_HOME=/usr/j2sdk/ at the top of /etc/crontab, ie declare env vars in the /etc/crontab file itself?"

Yes. I think you might have to export the variable:
JAVA_HOME=/usr/j2sdk/
export JAVA_HOME

-----------------------
Steve Stites

Last edited by jailbait; 08-28-2004 at 04:31 PM.
 
Old 08-28-2004, 02:09 PM   #3
damaru
LQ Newbie
 
Registered: Jul 2004
Distribution: Suse, Ubuntu
Posts: 7

Rep: Reputation: 0
Hi,

What I do is this:
For each crontab task, I make a shell script in my home directory. Suppose it's called some_task.

I begin the shell script with:

#!/bin/bash -l

This causes bash to start as a login shell, which means it will read /etc/profile and ~/.bash_profile. In my experience, commands run from cron don't automatically read those files.

Of course, you could set the env vars in the shell script itself, if you needed different settings from those in the regular bash profile files.

cheers,
 
Old 09-03-2004, 01:26 AM   #4
sjspig
LQ Newbie
 
Registered: Mar 2003
Distribution: Modded-Knoppix
Posts: 23

Rep: Reputation: 15
Define JAVA_HOME = path to java at the beginning of your cron. No export of the variable is necessary.

1) Does /etc/profile set the environment variables for bash shell ONLY?

- the /etc/profile does not get used by cron because it does not load the bash shell and the process loads the inherited environment given to it by cron

2) Where do I set the env vars for sh shell?

- this I don't know - it doesn't have anything to do with the cron environment even when cron runs sh, bash, etc.. - cron's environment is defined by cron as it starts - it usually contains the PATH and maybe a couple other variables.

3) Can I simply put JAVA_HOME=/usr/j2sdk/ at the top of /etc/crontab, ie declare env vars in the /etc/crontab file itself?

Yes - You're on the right track..
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Scripts & Environment Variables geeman2.0 Linux - General 12 07-05-2005 05:10 PM
environment variables naren Linux - Software 1 12-15-2004 06:44 AM
Setting global variables from a crontab job davee Programming 2 10-06-2003 07:05 AM
Environment Variables if you su concoran Linux - Newbie 1 08-16-2002 07:18 PM
environment variables aethereal Linux - Newbie 7 12-19-2001 09:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration