2021.09.28 09:11 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.
2021.10.01 01:52 PM - edited 2021.10.01 01:54 PM
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.
2021.09.28 09:57 AM
You can use named pipes:
Named pipes – Knowledge Base – kdb+ and q documentation - Kdb+ and q documentation (kx.com)
2021.09.29 07:09 AM
We're not necessarily reading files to use named pipes, even using a grep command still uses the /tmp directory
2021.10.01 01:52 PM - edited 2021.10.01 01:54 PM
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.
EMEA
Tel: +44 (0)28 3025 2242
AMERICAS
Tel: +1 (212) 447 6700
APAC
Tel: +61 (0)2 9236 5700
KX. All Rights Reserved.
KX and kdb+ are registered trademarks of KX Systems, Inc., a subsidiary of FD Technologies plc.