KX Community

Find answers, ask questions, and connect with our KX Community around the world.
KX Community Guidelines

Home Forums kdb+ Control system command directory used (instead of /tmp)

  • Control system command directory used (instead of /tmp)

    Posted by igor on September 28, 2021 at 12:00 am

    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.

    igor replied 9 months ago 3 Members · 3 Replies
  • 3 Replies
  • konq

    Member
    September 28, 2021 at 12:00 am
  • igor

    Member
    September 29, 2021 at 12:00 am

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

  • rocuinneagain

    Member
    October 1, 2021 at 12:00 am

    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.

Log in to reply.