18

Is there an environment variable to set the temporary directory on debian based systems?

I have a java applet that uses that environement variable and it's getting confused when launching two instances of the same applet.

Dennis Williamson
  • 60,515
  • 14
  • 113
  • 148
Disco
  • 1,301
  • 5
  • 19
  • 34

8 Answers8

20

I am unsure if the java applet will actually look at the environment variables before it starts, but what you can do it edit /etc/profile and add the following lines:

if [[ -O /home/$USER/tmp && -d /home/$USER/tmp ]]; then
        TMPDIR=/home/$USER/tmp
else
        # You may wish to remove this line, it is there in case
        # a user has put a file 'tmp' in there directory or a
        rm -rf /home/$USER/tmp 2> /dev/null
        mkdir -p /home/$USER/tmp
        TMPDIR=$(mktemp -d /home/$USER/tmp/XXXX)
fi

TMP=$TMPDIR
TEMP=$TMPDIR

export TMPDIR TMP TEMP

To make it a true tmp directory (as in the files go away when the session is ended, you'll want to edit the user's .bash_logout as well as the skeleton .bash_logout (/etc/skel/.bash_logout) to include the following:

if [ -O $TMPDIR && -d $TMPDIR ]; then
        rm -rf $TMPDIR/*
fi

The logout portion is dangerous is the variable doesn't get set and your logged in as root! I wouldn't add this to the root account or anyone that is a member of the wheel group! Proceed at your own caution.

miku
  • 445
  • 1
  • 3
  • 12
TrueDuality
  • 1,844
  • 5
  • 27
  • 37
  • 3
    I wouldn't put the cleanup into .bash_logout at all - what happens if they open up two sessions and log out of one? Use tmpwatch. :) – MikeyB Oct 10 '09 at 04:19
  • That is a much better cleanup solution, thanks for adding that. :) – TrueDuality Oct 16 '09 at 12:34
  • NB: the `tmpwatch` command does not exists on BSD (e.g. OSX) version of unix, for anyone going for portability. My CentOS boxes have it though. :) – Cometsong Nov 29 '18 at 14:44
10

The file you are looking for is:

/etc/environment

You have to set the TEMP variable like:

TEMP=/home/user/tmp
cstamas
  • 6,607
  • 24
  • 42
4

If you want /home/user/tmp to be cleaned on reboot, I suggest you add an @reboot job to the user's personal crontab.

Teddy
  • 5,134
  • 1
  • 22
  • 27
1
export TMPDIR=/path/to/desired/tmp

Use that before running desired command.

nyxee
  • 113
  • 4
  • 1
    This will only print the text `TMPDIR=/path/to/desired/tmp`. Nothing else. – Gerald Schneider Feb 25 '22 at 14:16
  • Sorry about that. Had to be `export` not `echo`. It really saved me a ot of pain. Some process was writing extremely large files to /tmp and I didn't want to allocate the space just for it. Or, reboot. – nyxee Mar 03 '22 at 07:34
1

In C, I would use the tmpfile() call for a posix system, which would avoid the collision. So I would look for a similar Java call before trying to implement it myself, if you haven't already.

Kyle Brandt
  • 82,107
  • 71
  • 302
  • 444
1

Java uses the system property java.io.tmpdir to configure the temporary directory. A reasonable JRE will set that to a sensible value based on the system if not explicitly specified.

1

For me this worked when i was trying to install a jar file using java.

export _JAVA_OPTIONS="-Djava.io.tmpdir=/apps/prod/tmp"

I use a Red Hat Linux. /apps/prod/tmp being the new folder.

chicks
  • 3,639
  • 10
  • 26
  • 36
sajan
  • 11
  • 1
0

https://support.oracle.com/epmos/faces/SearchDocDisplay?_adf.ctrl-state=1dab2wir99_201&_afrLoop=305930829027924#SYMPTOM

export _JAVA_OPTIONS="-Djava.io.tmpdir=<local path>"

Eg:

export _JAVA_OPTIONS="-Djava.io.tmpdir=/home/user/tmp"
RalfFriedl
  • 3,008
  • 4
  • 12
  • 17
scf617
  • 1