How to call .profile in cron? | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

How to call .profile in cron?

Shell Programming and Scripting


Tags
linux

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-18-2005
abhijeetkul abhijeetkul is offline
Registered User
 
Join Date: Oct 2005
Last Activity: 2 August 2006, 7:42 AM EDT
Location: Mumbai,India
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
Data How to call .profile in cron?

Hello all,

I want to call my users .profile in cron?

I understand that i have to do it explicitly in crontab entry.

How can we do it ? How can i write a simple script calling it?

Thanks & Regards
Abhijeet
Sponsored Links
    #2  
Old 11-20-2005
mschwage mschwage is offline
Registered User
 
Join Date: Jul 2005
Last Activity: 13 August 2009, 1:56 PM EDT
Location: Oak Park, IL
Posts: 102
Thanks: 0
Thanked 1 Time in 1 Post
. $HOME/.profile

-Mike
Sponsored Links
    #3  
Old 12-02-2005
guhas guhas is offline
Registered User
 
Join Date: Sep 2005
Last Activity: 19 December 2005, 1:14 PM EST
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
How is the .profile file comes into play when we run a scripts ?

How is the .profile file comes into play when we run a scripts ?
    #4  
Old 12-02-2005
mschwage mschwage is offline
Registered User
 
Join Date: Jul 2005
Last Activity: 13 August 2009, 1:56 PM EDT
Location: Oak Park, IL
Posts: 102
Thanks: 0
Thanked 1 Time in 1 Post
It doesn't... the .profile is read only by a login shell, not every time a shell script is run.

When you login, since your .profile is read, you have certain things set up for you- like your $PATH. If those things are in environment variables (that is, if they've been exported), then scripts will have access to those things. Again, a good example is the $PATH. However, when cron runs a script the .profile is not read. This causes problems when you write scripts because things work when you run them but not when cron runs them.

So you need to always check your assumptions when doing shell programming... things like "What directory does the shell script think it's in?" and "What PATH does the shell script require?"
-Mike
Sponsored Links
    #5  
Old 12-04-2005
grahamb grahamb is offline
Registered User
 
Join Date: Dec 2005
Last Activity: 27 July 2008, 5:17 PM EDT
Location: Windsor, Ont Canada
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Including .profile: an example

Hi

Here is a line from my crontab:

Code:
10 4 * * * . ~/.profile; ${SCRIPTS}/backup_mysql.sh >> ${BACKUPS_LOG}/${HOSTNAME}_mysql_v${VERS}.script.log 2>&1

Assume each crontab command line generates a new shell. The ". ~/.profile;" command string: "dot space <...>", populates the environment variables for the new shell. The variables - in this case - are specific to the USER. They are called from the users home directory, etc.

The ". ~/.profile; " command string must be on the same crontab line, separated with ";" - a semi-colon. Do the same for EACH crontab command line.

Hope this is helpful!

Regards
GrahamB
Sponsored Links
    #6  
Old 12-04-2005
Cameron's Avatar
Cameron Cameron is offline Forum Advisor  
Registered User
 
Join Date: Nov 2001
Last Activity: 23 October 2011, 10:07 PM EDT
Location: Brisbane, Australia
Posts: 509
Thanks: 7
Thanked 0 Times in 0 Posts
For the scripts that I've written, I execute the .profile within the script - not in cron.

hence, in the script ${SCRIPTS}/backup_mysql.sh, you would see ...
Code:
#!/usr/bin/ksh
...
#- Ensure User Environment Set -#
. ${HOME}/.profile
...
[rest of script]

Hope that's of some help.
Sponsored Links
    #7  
Old 12-04-2005
mschwage mschwage is offline
Registered User
 
Join Date: Jul 2005
Last Activity: 13 August 2009, 1:56 PM EDT
Location: Oak Park, IL
Posts: 102
Thanks: 0
Thanked 1 Time in 1 Post
Quote:
Originally Posted by grahamb
Hi

Here is a line from my crontab:

Code:
10 4 * * * . ~/.profile; ${SCRIPTS}/backup_mysql.sh ...

Assume each crontab command line generates a new shell. The ". ~/.profile;" command string: "dot space <...>", populates the environment variables for the new shell. ...
Caveat Programmer: . ~/.profile MAY work- for example, if you are in Linux, where sh is the same as bash. However, for Solaris and other UNICES, sh is the bourne shell and will not recognize the ~ (tilde) metacharacter.

For them, you must use
Code:
. $HOME/.profile

Thus, in general, it is better to use $HOME in cronjobs rather than getting used to a metacharacter which is peculiar to non-bourne shells.

Also, I would caution against using $HOME/.profile in any shell script, in any event. This is because the $HOME/.profile is for setting up your login profile. When you are running a script you are not logged in. Your scripting environment should be stable, secure, and knowable. Since my .profile is mine, it's liable to be messed with at any time. Also, if you're doing such things as database backups or some system-related function with your ID- or depending on resources affiliated with your ID- what happens when you leave your job?

Make a nice happy, stable home for your shell scripts. It's good style, and some SA will thank you for it later...
-Mike Schwager
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Can we call crontab -e into .profile? ksrivani UNIX for Advanced & Expert Users 3 10-06-2011 10:41 AM
Call .profile in perl script Pratik4891 Shell Programming and Scripting 2 09-30-2011 02:26 AM
how to run script? call other script? su to another user? make a cron? instant000 Shell Programming and Scripting 15 11-23-2009 04:01 PM
Please Help: How to Call SSH Trust from Cron job. suman82 Shell Programming and Scripting 4 12-15-2008 02:43 PM
cron .profile Schoey Solaris 1 10-21-2005 06:37 AM



All times are GMT -4. The time now is 05:13 AM.