How to change default /tmp to /home/user/tmp
Solution 1
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.
Solution 2
The file you are looking for is:
/etc/environment
You have to set the TEMP variable like:
TEMP=/home/user/tmp
Solution 3
If you want /home/user/tmp
to be cleaned on reboot, I suggest you add an @reboot
job to the user's personal crontab
.
Solution 4
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.
Solution 5
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.
Related videos on Youtube
Disco
Updated on September 17, 2022Comments
-
Disco over 1 year
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.
-
poige about 7 years
-
-
abby over 14 yearsThis assumes he is the one developing the application
-
Kyle Brandt over 14 yearsTrueDuality: Ah, my mistake
-
Steve Townsend over 14 yearsI 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. :)
-
GURU-MVG over 14 yearsJava has
java.io.File.createTempFile
. Now with added secure RNG. -
abby over 14 yearsThat is a much better cleanup solution, thanks for adding that. :)
-
Fedir RYKHTIK almost 10 yearsAnd even
export TEMP=/home/user/tmp
-
user649102 almost 10 years@Fedir that is in the shell, yes.
-
Hama Sabah over 5 yearsNB: 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. :) -
dortegaoh about 2 yearsThis will only print the text
TMPDIR=/path/to/desired/tmp
. Nothing else. -
nyxee about 2 yearsSorry about that. Had to be
export
notecho
. 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.