Send sleep to background

Hello all,

I've a small script where it checks for the existence of a particular file and sleeps for 5 seconds if it doesn't exist. I would like to send the sleep output to background so that I don't see so many sleep messages in the build output.

#!/bin/sh -x
until [ -f /tmp/examplefile.txt ]
do
sleep 3
done
echo "File found"

Adding & to the end of the sleep command doesnt work. Any help is much appreciated.

Regards
builderj:wall:

Linux has the inotify command to do exactly this. What OS do you have?

RedHat 5 and Redhat 6

Any reason you are using the sh -x option? Try leaving that out.

/usr/bin/sleep does not produce any output.....

@rovf : /usr/bin/sleep did not work. Even tried /bin/sleep.

whereis sleep
sleep: /bin/sleep /usr/share/man/man1p/sleep.1p.gz /usr/share/man/man3p/sleep.3p.gz /usr/share/man/man1/sleep.1.gz /usr/share/man/man3/sleep.3.gz

@Scrutinizer : even after leaving our sh -x, it didn't work.

WHAT "did not work"?

sleep itself does NOT produce any output, as rovf correctly stated. And, as Scrutinizer said, leaving out the -x option will stop xtrace output (which, btw, would go to stderr anyway).
If you are talking of the loop just keeps looping frantically, that's because you put sleep into background, peacefully sleeping there, side by side, while the main loop continues and continues and ...

Dude, try:

while [ ! -f /tmp/checkfile.txt ]
do
	sleep 3
done
echo "File exists now"

Or if i may talk about using TUI:

while [ ! -f /tmp/checkfile.txt ]
do
	tui-wait 3 "Checking for file in..."
done
tui-status $? "Found file"

hth

Hi RudiC,

I use the below part inside another shell script (parent script) where the parent script is executed as "sh -ex parent.sh".

until [ -f /tmp/examplefile.txt ]
do
/bin/sleep 3
done
echo "File found"

If the above is a standalone script, I can run it by just using "sh" as per Scrutinizer. Works great.

Since this part is inside another script, I get the output as below.

07:53:09 + '[' -f /opt/cc...... ']'
07:53:09 + sleep 5
07:53:11 + '[' -f /opt/cc...... ']'
07:53:11 + sleep 5
07:53:19 + '[' -f /opt/cc...... ']'
07:53:19 + sleep 5
07:53:21 + '[' -f /opt/cc...... ']'
07:53:21 + sleep 5
07:53:28 + '[' -f /opt/cc...... ']'
07:53:28 + sleep 5
07:53:32 + '[' -f /opt/cc...... ']'
07:53:32 + sleep 5
07:53:36 + '[' -f /opt/cc...... ']'
07:53:36 + sleep 5
07:53:41 + '[' -f /opt/cc...... ']'
07:53:41 + sleep 5
07:53:46 + '[' -f /opt/cc...... ']'
07:53:46 + sleep 5
07:53:51 + '[' -f /opt/cc...... ']'
07:53:51 + sleep 5
07:53:56 + '[' -f /opt/cc...... ']'
07:53:56 + sleep 5
07:54:01 + '[' -f /opt/cc...... ']'
07:54:01 + sleep 5
07:54:06 + '[' -f /opt/cc...... ']'
07:54:06 + sleep 5
07:54:11 + '[' -f /opt/cc...... ']'

@sea:

first method works great if I execute it as ./test.sh or sh test.sh.
since this is inside another script, it didn't work.

This is what i get if use your second method.
tui-wait: command not found

So - what's wrong? Above is exactly what would be expected, except mayhap the timing in the first three or four loops.

I want to send these message to background or stop it from printing on screen. From developers point of view, these should not be visible from the output log.

You probably have a set -x inside your script somewhere.
That is what is causes that output, not 'some' error of the code.

Yes, as i've said, "if i may talk about using TUI", those commands are not (yet) part of the default installations.
Wanted to link there, but it seems the thread and its 8 pages is gone from the "Scripts & Programming" sticky section??

---------- Post updated at 19:30 ---------- Previous update was at 19:25 ----------

There is no output log, unless you do log the (specified) output.
That is not applicable here (from what we've seen here).

You could do like:

/path/script.sh 2>/path/to/logfile.everything >&2
# OR
/path/script.sh 2>/path/to/logfile.error >/path/to/logfile.stdout
# OR
/path/script.sh >/path/to/logfile.stdout

EDIT:
But usualy, you only want:

/path/script.sh 2>/path/to/logfile.error 

hth

But - - - you've been told by Scrutinizer NOT to use the -x option?

Err...
And from within said /path/script.sh, the this would be some sample code:

echo "this is default output" >&1
echo "this is an error message" >&2
echo_fails "This line (its error message) will appear in the error log"
echo "This message will appear in the stdout logfile, if redirected"

hth

@ RudiC, As I said , I run the parent script using sh -xe option for some other purpose and the sleep is just part of the parent script. So i cannot do what Scrutinizer said.

@Sea : Thanks much. Will try it out.

Then you will have to live with that output, or redirect that too while calling the parent script.

sh -xe parent.sh 2>output.log

hth

You can temporarily disable shell tracing, for example that way:

(
    set +x
    while [ ! -f /tmp/checkfile.txt ]
    do
	sleep 3
    done
    echo "File exists now"
)

@jlliagre : That worked perfectly. Thank you so much.