Issue with variables via cronned script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Issue with variables via cronned script
# 8  
Old 10-19-2010
Thanks all for your replies

Vbe,
I updated the profile with your excerpt code and when i source my profile it returned interactive? what does it mean? Thank you.

DGPickett,
Below is the profile setup page, please let me know what is wrong in here, Thank you.
Quote:
EDITOR=vi; export EDITOR
set -o vi

ORACLE_HOME=/orcl/app/oracle/product/11.1.0.7; export ORACLE_HOME
SYBASE=/3rdparty/sybase/ocs/12_5; export SYBASE
INFORMATICA=/apps/informatica/v7; export INFORMATICA
PATH=$PATH:/usr/bin:/usr/sbin:/usr/ucb:/usr/ccs/bin:$INFORMATICA/server/bin:$ORACLE_HOME/bin; export PATH
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$INFORMATICA/server/bin:$ORACLE_HOME/lib:$ORACLE_HOME/odbc/lib:/lib:/usr/openwin/lib:/usr/lib; export LD_LIBRARY_PATH

LANG=C; export LANG
LC_ALL=C; export LC_ALL
LC_TYPE=C; export LC_TYPE
NLS_LANG=AMERICAN_AMERICA.WE8ISO8859P1; export NLS_LANG

INT_SRV=tt_prd; export INT_SRV
TT_DOMAIN=Domain_turntable; export TT_DOMAIN
INFA_HOME=/apps/informatica/v8.6.1; export INFA_HOME

TA=/ADP_Impact_TT_Input; export TA
TX=/Impact_TT_Archive; export TX
TI=/Internal_Impact_TT_Input; export TI
TL=/Impact_TT_Load; export TL
TO=/ADP_Impact_TT_Output; export TO
TF=/product/apps/feeds/TT; export TF
TS=/product/apps/informatica/v7/pc/ScriptLogs; export TS
TT=/product/apps/informatica/v7/pc/TgtFiles; export TT
TE=/product/apps/informatica/v7/pc/ExtProc; export TE

ttyname=$(/usr/bin/tty)
today=$(/bin/date +'%Y%m%d')
HISTDIR=$HOME/.sys/hists
HISTFILE=$HOME/.sys/hists/$today.${ttyname##*/}
HISTSIZE=500

if [ ! -d "$HISTDIR" ]
then
mkdir -p 700 -m $HISTDIR
fi

alias cda='cd /ADP_Impact_TT_Input'
alias cdx='cd /Impact_TT_Archive'
alias cdi='cd /Internal_Impact_TT_Input'
alias cdl='cd /Impact_TT_Load'
alias cdo='cd /ADP_Impact_TT_Output'
alias cdf='cd /product/apps/feeds/TT'
alias cds='cd /product/apps/informatica/v7/pc/ScriptLogs'
alias cdt='cd /product/apps/informatica/v7/pc/TgtFiles'
alias cde='cd /product/apps/informatica/v7/pc/ExtProc'
alias cdT='cd /product/apps/informatica/tools'

# Setup prompt
standout=`tput smso`
standend=`tput rmso`
HOSTNAME=`hostname`
PS1=
PS1C=$PS1
PS1=$standout\$PWD$standend'
$PS1C'
PS1="
${PS1} $LOGNAME@$HOSTNAME# "
export PS1 PS1C
# 9  
Old 10-19-2010
Move anything that needs a terminal down into a block (except setting ttyname right above), and when you get there, only if [ "$ttyname" != "not a tty" , run that block. The aliases may work in scripts, or destabilize them, you get to choose. Ditto for functions in .profile/.kshrc! Scripts do not do HIST OR PS or tput, etc.

---------- Post updated at 05:08 PM ---------- Previous update was at 05:07 PM ----------

It is a little more readable to put 'export' on the next line, but keep them one per.
# 10  
Old 10-20-2010
sorry i couldn't able to comprehend what you are saying, i checked the manual page for tty it's just saying it return's user terminal name. I am confused when you say "teriminal down" where & when we will you use tty??, also can you please interpret me in layman standard what's happening in the below script, what i should do to make it work in k shell, appreciate your help.
Quote:
ttyname=$(/usr/bin/tty)
today=$(/bin/date +'%Y%m%d')
HISTDIR=$HOME/.sys/hists
HISTFILE=$HOME/.sys/hists/$today.${ttyname##*/}
HISTSIZE=500
Thanks a lot for all your replies.
# 11  
Old 10-20-2010
Firstly let's find out what environment you have in cron:
Try a one off cron containing just an "env" command and post the output.
I still think that the shell invoked by your cron does not understand $( ) notation.

Another way. Try an "at" job to find out what Shell cron uses:
Code:
at now
echo "" >/dev/null
<ctrl/d>
warning: commands will be executed using /usr/bin/sh

As others have hinted, another potential problem here is the "tty" command.
The "tty" command requires a real physical terminal and will not work from cron. The error message is "not a tty". In your case I think it hit a syntax error (in whatever Shell your cron uses) before trying the "tty" command.

The method you are using to generate history log file names needs a rethink if you want to run from cron OR you just do not execute those lines when running from cron by testing whether stdout is a terminal.

e.g.
Code:
if [ ! -t 1 ]
then

ttyname=$(/usr/bin/tty)
today=$(/bin/date +'%Y%m%d')
HISTDIR=$HOME/.sys/hists
HISTFILE=$HOME/.sys/hists/$today.${ttyname##*/}
HISTSIZE=500

if [ ! -d "$HISTDIR" ]
then
mkdir -p 700 -m $HISTDIR
fi

fi

Personally I would make a separate script containing a suitable shebang line and only those lines which I need when running from cron and check that it is more than is in the sytem's own "oraenv".



Attendum:
Don't forget that HISTFILE needs a terminal. It will not do anything from a cron job.
About the name of $HISTFILE. Your method will not stop sessions from different users getting appended because the "tty" name is reused when someone logs out. You need to include a bit more to make the filename unique within one calendar day e.g. the login username and current process id ($$) .

Last edited by methyl; 10-20-2010 at 09:51 AM..
# 12  
Old 10-20-2010
Quote:
Originally Posted by Ariean
sorry i couldn't able to comprehend what you are saying, i checked the manual page for tty it's just saying it return's user terminal name. I am confused when you say "teriminal down" where & when we will you use tty??, also can you please interpret me in layman standard what's happening in the below script, what i should do to make it work in k shell, appreciate your help.

Thanks a lot for all your replies.
1) Move your ttyname= line to the bottom, and add ' 2>&1' just before ')'.
2) Just under that, say: if [ "$ttyname" != "not a tty" ] ; then interactive-stuff . . . fi
3) Start moving things a script either does not need, or can not run, down into that if block, like:

HIST*= variables (scripts do now write .sh_history or can inherit this)
EDITOR=vi; export EDITOR (scripts do not edit the command history or can inherit this)
TERM=
PS*= (shell scripts do not prompt the user or can inherit this)
tput (there is no t to put to)

set -o vi (a .profile needs to be sh friendly, which means no {set -o, $() ${#%}, export var=val }, but this is ksh only, so put it at the very bottom of the interactive block, and devise how to ensure it is run by ksh, say if [ `ps -p $$ | grep -c ksh` = 1 ]; then ksh-directives . . . fi)

I do not know if other env vars are interactive-support-only, but you should. Setting excess env vars is not a big deal, but once you have a place for a thing, put it in the right place.

Ensuring SHELL= is set is nice, for not-necessarily-interactive things like starting an xterm, as well as any shell-out from a command.

Then, the .profile stops short if called from cron, ssh/ssh2/rsh, nohup after the starting terminal hangs up, etc. Also, ksh-specific things do not run under sh.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue nesting variables in csh.

The variables given are already defined ($file1-$file3, $obsid1-$obsid3, and $n=3). When I go to run the code, the terminal outputs "Missing }." I believe the error is with the nesting of variables. It would save a lot of time getting this while loop working. set i = 1 while (${i} <=... (5 Replies)
Discussion started by: ojdefdidit
5 Replies

2. Solaris

Solaris 8 ssh issue - $SSH_ORIGINAL_COMMAND undefined variables

I face a weird question I don't know how to deal with. I tried to limit the permission of root user to remote login using ssh. So I did the following for a client server, 1. edit /usr/local/etc/sshd_config and modify as below PermitRootLogin forced-commands-only 2. using pubkey... (7 Replies)
Discussion started by: bestard
7 Replies

3. Shell Programming and Scripting

Issue in shell script variables

Hi, I have a file at $HOME/t.txt, below is file content cat $HOME/t.txt CUSTOMER_${REGION}.out Am using this file content in one script $HOME/samp.sh, below is the script #!/bin/bash REGION=USA x=`cat ${HOME}/t.txt` echo $x Am getting following output.. CUSTOMER_${REGION}.out ... (3 Replies)
Discussion started by: shieksir
3 Replies

4. Shell Programming and Scripting

Running a script with multiple variables like 25 variables.

Hi All, i have a requirement where i have to run a script with at least 25 arguements and position of arguements can also change. the unapropriate way is like below. can we achieve this in more good and precise way?? #!/bin/ksh ##script is sample.ksh age=$1 gender=$2 class=$3 . . .... (3 Replies)
Discussion started by: Lakshman_Gupta
3 Replies

5. Shell Programming and Scripting

Scripting Issue with Variables from awk

Greetings all, Disclaimer: I'm a novice and always welcome best practices as I'm learning. File example: 100,1.1.1.1,1.1.1.2,10.10.10.1,172.16.1.10,172.16.1.20 101,1.1.2.1,1.1.2.2,10.10.20.1,172.16.2.10,172.16.2.20 102,1.1.3.1,1.1.3.2,10.10.30.1,172.16.3.10,172.16.3.20 ...and so on ... (3 Replies)
Discussion started by: sjrupp
3 Replies

6. Shell Programming and Scripting

awk issue expanding variables in ksh script

Hi Guys, I have an issue with awk and variables. I have trawled the internet and forums but can't seem to get the exactt syntax I need. I have tried using awk -v and all sorts of variations but I have hit a brick wall. I have spent a full day on this and am just going round in circles. ... (3 Replies)
Discussion started by: gazza-o
3 Replies

7. Shell Programming and Scripting

Awk script problem - Variables Causing Issue

can someone please explain to me what i'm doing wrong with this code: WELT=$(awk '(($1 ~ "^${caag}$") || ($2 ~ "^${caag}$"))' /tmp/Compare.TEXT) when run from the command line, it works. but it seems to be having a problem doing the comparison when variables are involved. i tested from... (1 Reply)
Discussion started by: SkySmart
1 Replies

8. Shell Programming and Scripting

White spaces issue with shell variables

Hi all, I've a requirement as below Source file src.txt sample data: A<10 white spaces>B12<5 white spaces>C<17 white spaces> A1<5 white spaces>B22<5 white spaces>C13<17 white spaces> when I'm fetching a record from this file into a shell variable like below: vRec=`head -1 src.txt... (2 Replies)
Discussion started by: madhu_1126
2 Replies

9. UNIX for Dummies Questions & Answers

testing variables issue

oh,i don't understand my shell file like this #!/bin/sh # # testnum=$(ifconfig eth0|sed -n 8p|awk '{print $2}'|cut -c7-) echo $testnum sleep 2 echo $testnum sleep 2 echo $testnum sleep 2 echo $testnum but the output was always the same value like # ./test.sh 3779052732 3779052732... (8 Replies)
Discussion started by: flw521521
8 Replies

10. UNIX for Dummies Questions & Answers

Issue with parsing config variables

I am using MKS tool kit on windows server. One config variable is defined in windows environment and I am trying to use that variable. # Below RootDir is defined in windows RootDir="\\f01\var" # in unix script details="$RootDir/src|$RootDir/tgt" src=`echo $details|awk -F '|' '{print... (1 Reply)
Discussion started by: madhukalyan
1 Replies
Login or Register to Ask a Question