问题
R creates a session-specific temp directory in a subdirectory of TMPDIR using mkdtemp(), and removes that directory tree when R exits. This directory is used by default when calling tempfile(), and when calling pdf().
I have found that for some very long-running (e.g. 2 day) Rscript jobs, the session-specific temp directory appears to be gone. I get a "No such file or directory" error produced by plot.new(), or similar error writing to a path created with tempfile().
I've used the same code on several data sets, but I only see this problem on the largest, longest-running ones. The same process the eventually gets the error is able to create other PDFs earlier in the process, so presumably the temp directory existed at some point in the life of the process. Note also that in the PDF case the problem eventually results in a segfault, which ends the R process before the on-exit cleanup of the session-specific temp directory. I pulled the name of the session-specific temp directory from the core file and confirmed that it does not exist.
Any idea what is deleting the session-specific temp directory?
The segfault that shows that R crashes before it gets to the temp directory cleanup code:
#0 0x00000037ff467934 in fwrite () from /lib64/libc.so.6
#1 0x00002ae33b8c10e2 in PDF_endpage () at devPS.c:6509
#2 0x00002ae33b8c284b in PDF_Close () at devPS.c:7257
#3 0x00002ae338dbf05e in removeDevice.part.0 ()
from /broad/software/free/Linux/redhat_6_x86_64/pkgs/r_3.3.0/lib64/R/lib/libR.so
#4 0x00002ae338dbf4c9 in Rf_KillAllDevices ()
from /broad/software/free/Linux/redhat_6_x86_64/pkgs/r_3.3.0/lib64/R/lib/libR.so
#5 0x00002ae338ee42e4 in Rstd_CleanUp ()
from /broad/software/free/Linux/redhat_6_x86_64/pkgs/r_3.3.0/lib64/R/lib/libR.so
#6 0x00002ae338e21947 in run_Rmainloop ()
from /broad/software/free/Linux/redhat_6_x86_64/pkgs/r_3.3.0/lib64/R/lib/libR.so
#7 0x00000000004007bb in main () at Rmain.c:29
回答1:
Per this answer by @kba on Serverfault, to the question When does /tmp get cleared?
That depends on your distribution. On some system, it's deleted only when booted, others have cronjobs running deleting items older than n hours.
- On Debian-like systems: on boot (the rules are defined in
/etc/default/rcS
).- On RedHat-like systems: by age (RHEL6 it was
/etc/cron.daily/tmpwatch
; RHEL7 and RedHat-like with systemd it's configured in/usr/lib/tmpfiles.d/tmp.conf
, called bysystemd-tmpfiles-clean.service
).- On Gentoo
/etc/conf.d/bootmisc
.
You can get around this by changing your R working directory. In your ~/.Renviron file, put
TEMP=~/Rworkdir
or something similar. (You may have to use TMP instead of TEMP.) This will tell R to create its working directory under your home directory. Substitute any path that works for you.
来源:https://stackoverflow.com/questions/41027871/why-does-r-session-specific-temp-directory-disappear-in-long-running-r-process