Watnog
asked on
Unix ksh: do a command until condition is met
Dear Experts,
Again I need your help.
A file is ftp-ed, upon completion of the transfer a signal is to be given that it can be processed.
First thing was to be sure the file was fully written, I found in the archives guidance on using 'fuser' for that.
So I have:
`/usr/sbin/fuser -f $FILENAME 2>/dev/null 1>path/to/fuser.txt`
As long as the text in fuser.txt is not 'blank' (e.g. a process number is returned), the file is 'in use'.
Until the file is 'blank' the fuser cmd should be redone (every minute or so).
Once there is no text in fuser.txt (size of the file is not '0' but '1' actually) a new command can be launched.
I have been checking 'while' scenario's but it is beyond my skills to implement.
Many thanks in advance.
Again I need your help.
A file is ftp-ed, upon completion of the transfer a signal is to be given that it can be processed.
First thing was to be sure the file was fully written, I found in the archives guidance on using 'fuser' for that.
So I have:
`/usr/sbin/fuser -f $FILENAME 2>/dev/null 1>path/to/fuser.txt`
As long as the text in fuser.txt is not 'blank' (e.g. a process number is returned), the file is 'in use'.
Until the file is 'blank' the fuser cmd should be redone (every minute or so).
Once there is no text in fuser.txt (size of the file is not '0' but '1' actually) a new command can be launched.
I have been checking 'while' scenario's but it is beyond my skills to implement.
Many thanks in advance.
ASKER
No lsof on this hpux box.
...
...
... and if you have only fuser (which doesn't provide a returncode) and not lsof, this is how your command would have to be set up:
while :
do
/usr/sbin/fuser -f $FILENAME 2>/dev/null 1>/path/to/fuser.txt
[[ ! -s /path/to/fuser.txt ]] && break
done
echo $FILENAME is now free!
rm /path/to/fuser.txt
or, without an intermediate file
while :
do
[[ -z $(/usr/sbin/fuser -f $FILENAME 2>/dev/null) ]] && break
done
echo $FILENAME is now free!
wmp
while :
do
/usr/sbin/fuser -f $FILENAME 2>/dev/null 1>/path/to/fuser.txt
[[ ! -s /path/to/fuser.txt ]] && break
done
echo $FILENAME is now free!
rm /path/to/fuser.txt
or, without an intermediate file
while :
do
[[ -z $(/usr/sbin/fuser -f $FILENAME 2>/dev/null) ]] && break
done
echo $FILENAME is now free!
wmp
Heh, I forgot to sleep!
while :
do
/usr/sbin/fuser -f $FILENAME 2>/dev/null 1>/path/to/fuser.txt
[[ ! -s /path/to/fuser.txt ]] && break
sleep 60
done
echo $FILENAME is now free!
rm /path/to/fuser.txt
while :
do
[[ -z $(/usr/sbin/fuser -f $FILENAME 2>/dev/null) ]] && break
sleep 60
done
echo $FILENAME is now free!
while :
do
/usr/sbin/fuser -f $FILENAME 2>/dev/null 1>/path/to/fuser.txt
[[ ! -s /path/to/fuser.txt ]] && break
sleep 60
done
echo $FILENAME is now free!
rm /path/to/fuser.txt
while :
do
[[ -z $(/usr/sbin/fuser -f $FILENAME 2>/dev/null) ]] && break
sleep 60
done
echo $FILENAME is now free!
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 everybody. Wnp you are a star.
Any idea why I have this:
sleep: not found
I had the same with the fuser cmd before, I had to give the full path ...
Any idea why I have this:
sleep: not found
I had the same with the fuser cmd before, I had to give the full path ...
Strange, normally "sleep" is in /usr/bin, a path which is always present, even under cron, so you won't have to specify it.
Please issue
which sleep
What do you see?
Please issue
which sleep
What do you see?
ASKER
/usr/bin/sleep
Your oneliner works correctly when I give the full path to sleep...
Your oneliner works correctly when I give the full path to sleep...
Do you run the command from crontab?
It might be that in HP-UX cron doesnt provide any path, although this would be very uncommon.
You could add to crontab
* * * * * echo $PATH > /tmp/mypath
let it run for a minute or so to then check /tmp/mypath. What's in there?
If you're not under cron, how could you run any command without path (except for the shell bultins)?
What does "echo $PATH" issued from the commandline say?
It might be that in HP-UX cron doesnt provide any path, although this would be very uncommon.
You could add to crontab
* * * * * echo $PATH > /tmp/mypath
let it run for a minute or so to then check /tmp/mypath. What's in there?
If you're not under cron, how could you run any command without path (except for the shell bultins)?
What does "echo $PATH" issued from the commandline say?
ASKER
See below.
This is new to me too actually, I may need to contact a unix adm on this...
#echo $PATH
/usr/bin:/usr/ccs/bin:/usr /contrib/b in:/usr/co ntrib/Q4/b in:/opt/ip f/bin:/opt /hparray/b in:/opt/ne ttladm/bin :/opt/fcms /bin:/opt/ ssh/bin:/o pt/mx/bin: /usr/bin/X 11:/opt/se c_mgmt/bas tille/bin: /opt/dsau/ bin:/opt/d sau/sbin:/ opt/resmon /bin:/opt/ gnome/bin: /opt/perf/ bin:/usr/c ontrib/kwd b/bin:/opt /wbem/bin: /opt/wbem/ sbin:/opt/ graphics/c ommon/bin: /opt/perl/ bin:/opt/p rm/bin:/us r/sbin/dia g/contrib: /opt/sfm/b in:/opt/se c_mgmt/spc /bin:/opt/ hpsmh/bin: /opt/upgra de/bin:/op t/gwlm/bin :/opt/igni te/bin:/op t/hpnpl//b in:/usr/co ntrib/bin/ X11:/opt/m aestro:/op t/maestro/ bin:/opt/d rd/bin:/op t/firefox: /opt/mozil la:/opt/pe rl_32/bin: /opt/perl_ 64/bin:/op t/swa/bin: /opt/galax y/Base:/us r/local/bi n:/opt/thu nderbird:.
This is new to me too actually, I may need to contact a unix adm on this...
#echo $PATH
/usr/bin:/usr/ccs/bin:/usr
cron?
Btw. I am a Unix admin (but not familiar with HP-UX, admittedly).
Btw. I am a Unix admin (but not familiar with HP-UX, admittedly).
ASKER
I think I know what the matter is....
I have this in the script:
PATH=/opt/maestro/OTS/SCRI PTS/TEST/G PI/WRKDIR/ TMP/
I think I shouldn't do that!
:-]
I have this in the script:
PATH=/opt/maestro/OTS/SCRI
I think I shouldn't do that!
:-]
Oh boy!
Glad you found it.
A path is only meant for searching executables! Are really programs/scripts in there?
Thx for the points!
wmp
Glad you found it.
A path is only meant for searching executables! Are really programs/scripts in there?
Thx for the points!
wmp
ASKER
I work in TWS (Tivoli Workload Schedular) a now IBM owned job scheduling software.
There is a lot possible by newly added funcitonality of EDWA (event driven workload automation), but it needs a lot of tinkering to suit your specific needs. So EDWA can catch afile being created and as an action tied to I echo the filename/timestamp etc to a file. I use the values in that file as input for a script. One of the things that script does is checking if the file is completely writtten, and once done it gets on. In my testing I just chose a foolish variable name. ...
There is a lot possible by newly added funcitonality of EDWA (event driven workload automation), but it needs a lot of tinkering to suit your specific needs. So EDWA can catch afile being created and as an action tied to I echo the filename/timestamp etc to a file. I use the values in that file as input for a script. One of the things that script does is checking if the file is completely writtten, and once done it gets on. In my testing I just chose a foolish variable name. ...
lsof $FILENAME2 && busy=1 || busy=0
if [ $busy -eq 0 ]; then
....process file....
fi
restart script every minute by cron and you're done.