Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Unix ksh: do a command until condition is met

Posted on 2010-11-17
15
Medium Priority
?
847 Views
Last Modified: 2012-05-10
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.

0
Comment
Question by:Watnog
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 2
15 Comments
 
LVL 6

Expert Comment

by:brb6708
ID: 34155152
this is how i did it in bash - ksh will be similiar I assume

lsof $FILENAME2 && busy=1 || busy=0
if [ $busy -eq 0 ]; then
   ....process file....
fi

restart script every minute by cron and you're done.

0
 

Author Comment

by:Watnog
ID: 34155317
No lsof on this hpux box.
...
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 34155325
... 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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 68

Expert Comment

by:woolmilkporc
ID: 34155344
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!
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 2000 total points
ID: 34155412
One-liner:

while [[ ! -z $(/usr/sbin/fuser -f $FILENAME 2>/dev/null) ]]; do sleep 60; done; echo $FILENAME is now free!

0
 
LVL 6

Expert Comment

by:brb6708
ID: 34155449
0
 

Author Comment

by:Watnog
ID: 34155575
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 ...
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 34155630
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?



0
 

Author Comment

by:Watnog
ID: 34155684
/usr/bin/sleep
Your oneliner works correctly when I give the full path to sleep...
 
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 34155751
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?
0
 

Author Comment

by:Watnog
ID: 34155794
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/bin:/usr/contrib/Q4/bin:/opt/ipf/bin:/opt/hparray/bin:/opt/nettladm/bin:/opt/fcms/bin:/opt/ssh/bin:/opt/mx/bin:/usr/bin/X11:/opt/sec_mgmt/bastille/bin:/opt/dsau/bin:/opt/dsau/sbin:/opt/resmon/bin:/opt/gnome/bin:/opt/perf/bin:/usr/contrib/kwdb/bin:/opt/wbem/bin:/opt/wbem/sbin:/opt/graphics/common/bin:/opt/perl/bin:/opt/prm/bin:/usr/sbin/diag/contrib:/opt/sfm/bin:/opt/sec_mgmt/spc/bin:/opt/hpsmh/bin:/opt/upgrade/bin:/opt/gwlm/bin:/opt/ignite/bin:/opt/hpnpl//bin:/usr/contrib/bin/X11:/opt/maestro:/opt/maestro/bin:/opt/drd/bin:/opt/firefox:/opt/mozilla:/opt/perl_32/bin:/opt/perl_64/bin:/opt/swa/bin:/opt/galaxy/Base:/usr/local/bin:/opt/thunderbird:.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 34155828
cron?

Btw. I am a Unix admin (but not familiar with HP-UX, admittedly).
0
 

Author Comment

by:Watnog
ID: 34155859
I think I know what the matter is....
I have this in the script:

PATH=/opt/maestro/OTS/SCRIPTS/TEST/GPI/WRKDIR/TMP/

I think I shouldn't do that!

:-]
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 34155881
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
0
 

Author Comment

by:Watnog
ID: 34156190
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. ...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question