Control system command directory used (instead of /tmp)

https://learninghub.kx.com/forums/topic/control-system-command-directory-used-instead-of-tmp

Hi all,

Anyone know if there’s any way to control which directory is used during system commands?

 

We have run into issues where /tmp is full and would like to use a different directory to avoid processes crashing.

You cannot control the use of /tmp but you could possibly use a redirect to send the bulk of output to a different location.

In a basic form:

q)system"ls > /my/chosen/path/out.txt 2>&1;"
q)result:read0`:/my/chosen/path/out.txt
q)hdel`:/my/chosen/path/out.txt
`:/my/chosen/path/out.txt
q)result
"file1"
"file2"

 

You could aim to make a more reusable function.

For familiarity you could use the TMPDIR environment variable:

q)setenv[`TMPDIR] "/my/chosen/path"

Then create a function to run system commands

systemTMPDIR:{[c] 
  f:first system"mktemp"; //Make a temp file respecting TMPDIR
  c:c," > ",f," 2>&1;echo $?"; //Add redirect to tmp file and capture of exit code
  e:"J"$first system c; //Execute the command
  f:hsym `$f; 
  r:read0 f; //Read the result of the command 
  hdel f; //Delete the tmp file
  $[not 0=e; //Check if the exit code was an error (not 0)
    [-1 last r;'`os]; //If an error print the last line and signal with 'os
    r] //If success return the data 
}

On success:

q)systemTMPDIR"ls"
"file1"
"file2"

On failure:

q)systemTMPDIR"blah"
sh: 1: blah: not found
'os
[0] systemTMPDIR"blah"
^

*Note: This is just a small example and likely will not behave the exact same as the native ‘system’ in all cases.

You can use named pipes:
Named pipes Knowledge Base kdb+ and q documentation - Kdb+ and q documentation (kx.com)

We’re not necessarily reading files to use named pipes, even using a grep command still uses the /tmp directory