Search code examples
shelltemporary-files

cannot create temp file for here document: No space left on device


When POSIX shell scripts use here documents, a temporary file is created. Is there any way to environmentally control WHERE (path) these temporary files get created?

I am currently developing an Ant script that launches remote POSIX operating system shell scripts on Linux, SunOS, HP-UX and AIX.Scripts compile C++ source code using GNU Autoconf/Automake. The generated configure script itself makes use of several here documents. If any of those here documents cannot create a file at /var/tmp (on SunOS) a chain reaction of failed configure script tasks results in missing binary files due to failed attempts to compile C++ code. :-(

So,I could keep cleaning up /var/tmp. But these POSIX servers are heavily used by nightly, long-running jobs that temporarily eat /var/tmp space then give it up, sporadically causing No space left on device outages. Obviously, some system administration could alleviate this problem--grow the /var/tmp partition perhaps.

But that is neither here, nor there, really; I want to remove the dependency upon other file systems/partitions entirely. I want my entire scripted system to be self-contained, so that hiccups with /var/tmp space do not matter to my Ant system when a remote POSIX platform runs out of a puny amount of /var/tmp space. Nonsense.

In a perfect POSIX world, I should be able to control the location (path) used by all shell script 'here documents' right? There has to be a configurable way; but all I have found is a way to control the location of 'vi' temporary space using an .exinit file with a directory=~/tmp command. Isn't the same idea implemented by the original UNIX gods for the POSIX shell script here documents?

Tried the following ideas; they didn't make a difference:

export TEMP=~/tmp
export TMP=~/tmp

To reproduce the problem, fill /var/tmp (your POSIX system might be using /tmp) 100%, then try saving the following script to tst.sh. Then make it executable with chmod +x tst.sh. Then run tst.sh by typing it at the shell prompt, of course.

banner <<zzz23EndOfMessagezzz23
E-mail your noontime orders for pizza to the system administrator.
    (Add an extra dollar for anchovy or mushroom topping.)
zzz23EndOfMessagezzz23

Thanks for any leads in advance gurus!


Solution

  • With Bash:

    TMPDIR If set, bash uses its value as the name of a directory in which bash creates temporary files for the shell's use.

    With Zsh:

    TMPPREFIX  A pathname prefix which the shell will use for all temporary files.  Note that this should include an initial part for
               the file name as well as any directory names.  The default is `/tmp/zsh'.
    

    If your POSIX shell doesn't have such feature, the best thing I know is that you modify your own shell by source code to read the value of the exported variable where if valid and writable, use it for temporary files. If not, go back to default. If you're concerned about probably security troubles with it, consider making the change permanent. If you're conservative about possible bugs or system malfunctions that may arise from the modified shell, consider renaming it as another binary e.g. /bin/modsh or /bin/testsh.

    If you a large memory, you can consider mounting tmpfs to your /var/tmp but this may only apply to Linux. I'm not sure about other systems. Linux's tmpfs shares the memory it uses i.e. when not in use, other programs can use it. It's pretty stable and is even more stable if used with along with a swap. And tmpfs can have a specified size limit.