home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Unix Power ToolsUnix Power ToolsSearch this book

15.5. Limiting File Sizes

Here are techniques to keep you from creating large files (which can happen by accident, such as with runaway programs). Your shell may be able to set process limits. If you're writing a program in C or another language that has access to kernel system calls, you can set these limits yourself. And there's one more trick you can use.

These limits are passed to child processes. So, if your shell sets a limit, all programs you start from that shell will inherit the limit from their parent process.

15.5.2. Other Ideas

File size limits only apply to processes that are invoked from a shell where the limit is set. For instance, at and cron jobs might not read the shell setup file (Section 3.3) that sets your limit. One way to fix this is to set the limit explicitly before you start the job. For instance, to keep your cron job named cruncher from core-dumping, make the crontab entry similar to one of these:

; Section 28.16

47 2 * * *   ulimit -c 0; cruncher
47 2 * * *   bash -c 'ulimit -c 0; exec cruncher'

If you've written a daemon (Section 1.10) in C that starts as your workstation boots up (so no shell is involved), have your program invoke a system call like ulimit(3) or setrlimit(2).

If the unwanted files are created in a directory where you can deny write permission to the directory itself -- and the files are not created by a process running as root (filesystem permissions don't apply to root) -- simply make the directory unwritable. (If the process needs to write temporary files, have it use /tmp. An environment variable such as TMP or TMPDIR may control this.)

You can prevent the files from being created by putting a zero-size unwritable file in the directory where the files are being created. Because the file is zero-length, it doesn't take any disk space to store:

chmod Section 50.5

% touch core
% chmod 000 core

If all else fails, try making a symbolic link to /dev/null (Section 43.12).

--ML and JP



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.