pvinodp
asked on
why is my command unable to append to a file?
when i run the script directly then I am able to append to the variable [FileName] $logFile
script1.sh
!/bin/sh
IPA_LOG=${IPA_LOG:=/tmp/ol i}
monitor="gf"
process_pid="10162"
cmd_array=("cat /proc/${process_pid}/statu s")
logFile=${IPA_LOG}/$monito r.log
fileType="mem"
cmd_array[1]="lsof -s -p $process_pid | awk '{ user = \$4 } { if ( user == \"$fileType\" ) { print } }' | sort -nrk 7 | head -100"
for i in "${cmd_array[@]}"
do
eval ${i} >> $logFile
done
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---
but if called thru chain of script:
!/bin/sh
#scriptA
...
..
start()
{
scriptB &
}
!/bin/sh
#scriptB
...
..
doSomething(){
script1 &
}
Now the script1 is unable to append to the logFile variable. I have tried changing the chmod and chown options. But no use.
script1.sh
!/bin/sh
IPA_LOG=${IPA_LOG:=/tmp/ol
monitor="gf"
process_pid="10162"
cmd_array=("cat /proc/${process_pid}/statu
logFile=${IPA_LOG}/$monito
fileType="mem"
cmd_array[1]="lsof -s -p $process_pid | awk '{ user = \$4 } { if ( user == \"$fileType\" ) { print } }' | sort -nrk 7 | head -100"
for i in "${cmd_array[@]}"
do
eval ${i} >> $logFile
done
--------------------------
but if called thru chain of script:
!/bin/sh
#scriptA
...
..
start()
{
scriptB &
}
!/bin/sh
#scriptB
...
..
doSomething(){
script1 &
}
Now the script1 is unable to append to the logFile variable. I have tried changing the chmod and chown options. But no use.
ASKER
ok. let me put it more clearly:
following is the outline of script1.sh.
!/bin/sh
pid=$1
type=$2
functionA(){
while :
do
if[cpu_id -gt threshold_const]
then
for i in "${cmd_array[@]}"
do
eval ${i} >> $logFile
done
fi
done
}
if [x$type == xgf]
then
#accordingly i populate the cmd_array and logFile variable
functionA
fi
if [x$type == xok]
then
#accordingly i populate the cmd_array and logFile variable
functionA
fi
If I call this script by passing the 2 arguments the logs are not appending. But I can see on the filesystem that the timestamp of the logFile is updated.
If i just remove all the code and just keep the following then I see that the logFile is appended.
!/bin/sh
IPA_LOG=${IPA_LOG:=/tmp/ol i}
monitor="gf"
process_pid="10162"
cmd_array=("cat /proc/${process_pid}/statu s")
logFile=${IPA_LOG}/$monito r.log
fileType="mem"
cmd_array[1]="lsof -s -p $process_pid | awk '{ user = \$4 } { if ( user == \"$fileType\" ) { print } }' | sort -nrk 7 | head -100"
for i in "${cmd_array[@]}"
do
eval ${i} >> $logFile
done
The only diffrence here is that the logFile is static and there is no while loop. Do u see any leak somewhere?
following is the outline of script1.sh.
!/bin/sh
pid=$1
type=$2
functionA(){
while :
do
if[cpu_id -gt threshold_const]
then
for i in "${cmd_array[@]}"
do
eval ${i} >> $logFile
done
fi
done
}
if [x$type == xgf]
then
#accordingly i populate the cmd_array and logFile variable
functionA
fi
if [x$type == xok]
then
#accordingly i populate the cmd_array and logFile variable
functionA
fi
If I call this script by passing the 2 arguments the logs are not appending. But I can see on the filesystem that the timestamp of the logFile is updated.
If i just remove all the code and just keep the following then I see that the logFile is appended.
!/bin/sh
IPA_LOG=${IPA_LOG:=/tmp/ol
monitor="gf"
process_pid="10162"
cmd_array=("cat /proc/${process_pid}/statu
logFile=${IPA_LOG}/$monito
fileType="mem"
cmd_array[1]="lsof -s -p $process_pid | awk '{ user = \$4 } { if ( user == \"$fileType\" ) { print } }' | sort -nrk 7 | head -100"
for i in "${cmd_array[@]}"
do
eval ${i} >> $logFile
done
The only diffrence here is that the logFile is static and there is no while loop. Do u see any leak somewhere?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks for the input...
inside the function i have another line which has > instead of >>
thanks for suggesting to define "set -x" inside the function
inside the function i have another line which has > instead of >>
thanks for suggesting to define "set -x" inside the function
Are there any error messages?
Is the IPA_LOG variable defined?
Add a
Open in new window
near the top of script1 and post the output.
BTW, I assume you typed the scripts here as they contain errors. Always best to copy/paste.