LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-14-2006, 01:30 PM   #1
kcella
LQ Newbie
 
Registered: Nov 2003
Posts: 3

Rep: Reputation: 0
cron force user profile


Is there any way to have the cron daemon automatically run the user's crontab with the full user login profile without having to source them?

I want to avoid runnign my home profile and /etc/profile everytime I run a cron job.
 
Old 10-14-2006, 03:41 PM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,053
Blog Entries: 14

Rep: Reputation: 1226Reputation: 1226Reputation: 1226Reputation: 1226Reputation: 1226Reputation: 1226Reputation: 1226Reputation: 1226Reputation: 1226
No you'd have to source them.

However most of the time you don't really need the full profiles. Often your main issue is lack of PATH to binaries so you can just include those in the script you're having cron run.

e.g. Say you had a script that ran:
Code:
#!/bin/bash
date >>/tmp/mycron.log
ls -l / >>/tmp/mycron.log
The cron job might fail because it doesn't know where to find "date" or "ls". By adding full paths you get around this:
Code:
#!/bin/bash
/bin/date >>/tmp/mycron.log
/bin/ls -l / >>/tmp/mycron.log

For long scripts where you use the same command more than once it is more efficient to make the full paths of those commands variables then call them. (awk and grep are a couple I often do this for). So the above becomes:
Code:
#!/bin/bash
DATE=/bin/date
LS=/bin/ls
$DATE >>/tmp/mycron.log
$LS -l / >>/tmp/mycron.log
For very complex items you might wish to just set a PATH as you do in profile so you don't have to define a separate variable for each command:
Code:
#!/bin/bash
PATH=/bin:/usr/bin:/usr/local/bin:/opt/whatever/bin
date >>/tmp/mycron.log
ls -l / >>/tmp/mycron.log
Of course if they're are other variables you need from the profile you can just include those in the script.
Code:
#!/bin/bash
PATH=/bin:/usr/bin:/usr/local/bin:/opt/whatever/bin
MYDIR=/
date >>/tmp/mycron.log
ls -l $MYDIR >>/tmp/mycron.log
Of course if it's something fairly involved like an Oracle login where you have to set LD_LIBRARY_PATH, TNSNAMEs etc... it's a lot easier just to source in the profile. Otherwise you can just extract the variables you need from the profile and include them in your script. The reason you'd do the latter is just to avoid having to look at "stty: not a terminal" and other ugly messages you see when running a profile. These messages don't actually hurt anything but they do make your logs look crappy.

Last edited by MensaWater; 10-14-2006 at 03:43 PM.
 
  


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
Force logout of user. Cannot erase user account. philippeP Linux - General 5 07-12-2006 12:22 PM
system profile vs user profile bonito SUSE / openSUSE 3 06-28-2006 02:02 PM
How do I set my profile in CRON job? lolson33 Linux - General 2 12-06-2005 05:18 PM
Load kde profile from /mnt/removable/user instead of /home/user preacher.ca Linux - General 3 12-02-2005 04:00 PM
Force user ust Linux - General 1 10-09-2003 12:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:43 AM.

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