LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-05-2009, 01:01 AM   #1
18Googol2
Member
 
Registered: Oct 2008
Distribution: FreeBSD, RedHat, Centos, Solaris, AIX
Posts: 34

Rep: Reputation: 17
Variable in crontab


Assume my username is foo, and I wanna set up PATH variable in my crontab. I specify $USER for portable purpose:

PATH="/bin:/sbin:/usr/bin:/usr/sbin:/home/$USER/bin"

But cron doesnt inteprete $USER as foo :S

Then I have no choice but to hard code my username: /home/foo/bin :S

Is there any way to "not" hard code my username? Thanks.
 
Old 03-05-2009, 01:58 AM   #2
w3bd3vil
Senior Member
 
Registered: Jun 2006
Location: Hyderabad, India
Distribution: Fedora
Posts: 1,191

Rep: Reputation: 49
A workaround would be to create a bash script and put that in the crontab instead.
I don't think you can define variables in crontab as Bash.
 
Old 03-05-2009, 03:25 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
The variable providing the username of the crontab's owner is LOGNAME, not USER. Here is a relevant excerpt from man 5 crontab:
Quote:
Several environment variables are set up automatically by the cron(8) daemon. SHELL is set to
/bin/sh, and LOGNAME and HOME are set from the /etc/passwd line of the crontab´s owner. HOME and
SHELL may be overridden by settings in the crontab; LOGNAME may not.
 
Old 03-05-2009, 09:57 AM   #4
18Googol2
Member
 
Registered: Oct 2008
Distribution: FreeBSD, RedHat, Centos, Solaris, AIX
Posts: 34

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by colucix View Post
The variable providing the username of the crontab's owner is LOGNAME, not USER. Here is a relevant excerpt from man 5 crontab:
$LOGNAME is exactly what Im after. Thanks
 
Old 03-05-2009, 10:13 AM   #5
18Googol2
Member
 
Registered: Oct 2008
Distribution: FreeBSD, RedHat, Centos, Solaris, AIX
Posts: 34

Original Poster
Rep: Reputation: 17
No, it doesnt work. cron doesnt intepretes $HOME and $LOGNAME as I wish. I let it echo $PATH and here is the output:

/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/$HOME/bin
 
Old 03-05-2009, 11:00 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You're right. If using
Code:
PATH=/bin:/usr/bin:$LOGNAME/bin
5 * * * * echo $PATH > cron.log
it does not work, since there is no variable substitution in crontab. But if you set it using export in the crontab entry it should work:
Code:
5 * * * * export PATH=/bin:/usr/bin:/home/$LOGNAME/bin ; echo $PATH > cron.log
However, the most convenient way is to put your commands in a shell script and use the proper sha-bang to force the execution of the script as if the shell was invoked as a login shell
Code:
#!/bin/bash -l
in this way the bash profile is sourced and the PATH is set the way you have in your usual shell environment.
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
AWK a variable Ouptut to a new variable and using the new variable with the old one alertroshannow Linux - Newbie 4 02-16-2009 12:08 AM
Sed search for variable and delete entire line, but variable contains forward /'s Passions Programming 2 11-10-2008 03:44 PM
replaced crontab, now should get crontab back to what it was raminn Linux - Newbie 2 10-20-2008 07:15 PM
Not able to print variable value in crontab entry naren_0101bits Programming 6 12-10-2007 12:04 PM
system-wide crontab in /etc/crontab ner Linux - General 2 11-18-2003 12:35 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:50 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
Open Source Consulting | Domain Registration