System
functions and helpers for interacting with the operation system and shell
Functions
Predicates
The System module provides access to operating system functionality, environment variables, process management, and system information.
Key Concepts
- Environment variable access
- System information retrieval
- Process execution and management
- Command-line argument handling
- Configuration management
Basic Usage
System Information
; get system information
print ["OS:" sys\os]
print ["CPU Arch:" sys\cpu\arch]
print ["CPU Cores:" sys\cpu\cores]
print ["Endianness:" sys\cpu\endian]
print ["Hostname:" sys\hostname]
; OS: macos
; CPU Arch: amd64
; CPU Cores: 8
; Endianness: little
; Hostname: drkameleons-MBP.home
; get process information
proc: process
print ["PID:" proc\id]
; PID: 92123
Environment Variables
; access environment variables
vars: env
print ["PATH:" env\PATH]
print ["HOME:" env\HOME]
; check specific variable
if key? env 'ARTURO_HOME ->
print ["Arturo home:" env\ARTURO_HOME]
Command Execution
; simple command execution
result: execute "ls -l"
print result
; with arguments as block
files: execute.args:["-l" "-a"] "ls"
; get exit code
status: execute.code "some_command"
if status <> 0 ->
print "Command failed!"
; asynchronous execution
pid: execute.async "long_running_process"
print ["Started process:" pid]
; direct shell execution
execute.directly "echo 'hello' > output.txt"
Tip
Useexecute.code
when you need to check if a command succeeded, andexecute.async
for long-running processes that shouldn't block.
Command-line Arguments
; access command line arguments
arg ; get raw arguments list as a block
; arg\0 -> first argument
; arg\1 -> second argument, etc...
inspect args
; with: -a --boom --c:123 "one"
;
; [ :dictionary
; a : true :logical
; boom : true :logical
; c : 123 :integer
; values : [ :block
; one :string
; ]
; ]
Common Patterns
Process Management
define :processManager [
start: function [command][
pid: execute.async command
print ["Started process:" pid]
return pid
]
stop: function [pid][
terminate pid
print ["Terminated process:" pid]
]
stopWithCode: function [pid code][
terminate.code:code pid
print ["Terminated process:" pid "with code:" code]
]
]
processManager: to :processManager []!
; let's start another Arturo process
pid: processManager\start sys\binary
pause 1000
processManager\stop pid
Configuration Management
; example configuration helper
getConfig: function [key defaultValue][
if key? config key ->
return config\[key]
return defaultValue
]
; usage
port: getConfig 'server_port 8080
host: getConfig 'server_host "localhost"
Path Management
; get path information
print ["Current dir:" path\current]
print ["Home dir:" path\home]
print ["Temp dir:" path\temp]
System Checks
; check for administrator/root privileges
if superuser? ->
print "Running with elevated privileges"
; check if running as standalone script
if standalone? ->
print "Running as standalone script"
; forcefully exit
exit