views:

72

answers:

2

I have a command e.g. ls-l > file.txt When there is insufficient space on my drive, the above command just stalls waiting for something to happen. Does anyone know about a code that I could write enabling me to display a message about the lack of space on my drive? E.g. could I use IPC or do you have any other ideas? Thanks in advance.

A: 

you can use df command to determine amount of free space on drive, and do not start your processing if there's less than 5M of free space for example.

Or you can check free space amount from your inner program and write warning message to the STDERR.

zed_0xff
Unfortunately, the command will always have to run. I will just have to cater for the blocking issue. Do you mean that I will have to write some sort of loop that will always calculate the free space using df and when it goes beyond some limit I display a message? But is there not a more efficient way of verifying whether the process has halted instead of having a loop.
A: 

Pipe command output through another program which will intercept write errors (e.g. ENOSPC), print diagnostics and fail if nothing else can be done.

This program can be as simple as cat:

foobar | cat > file.txt

cat will report error and die, and foobar will receive SIGPIPE with default action to die as well.

rkhayrov
Could you please provide some more information?
What is the desired behavior when application runs out of disk space? Fail with some diagnostics instead of waiting indefinitely, continue writing as in circular buffer, something else?
rkhayrov
When the application has filled the disk there is no need to wait indefinitely for more space to be available, just warn by displaying "out of disk space" and stop from there or exit the code.
See updated answer.
rkhayrov
Is there a reason why the cat command is considerably slowing down my program?
Of course it introduces some overhead, but it should not be significant unless your program is absolutely output bound. I think there is no way to make it faster without modifying the original program to intercept write errors such as ENOSPC.
rkhayrov
Thanks, but yeah, it does slow down quite considerably and I have to wait ages. But is it not possible to use perl interprocess commnunication to wait for stdout and stderr and let it handle that?