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 - returns a :task, doesn't block
t: execute.async "sleep 5; echo done"
print wait t                    ; "done\n"

; direct shell execution
execute.directly "echo 'hello' > output.txt"

Tip
Use execute.code when you need to check if a command succeeded, and execute.async for long-running processes that shouldn't block. execute.async hands you a :task - wait it for the output, or cancel it to kill the process (and its whole group, on POSIX). See the Tasks module.

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][
        t: execute.async command        ; returns a :task
        print "Started process"
        return t
    ]

    stop: function [task][
        cancel task                     ; kills the process (& its group on POSIX)
        print "Terminated process"
    ]
]

processManager: to :processManager []!

; let's start another Arturo process
task: processManager\start sys\binary
pause 1000
processManager\stop task

Note
terminate (and terminate.code:) still work for a raw integer PID - handy for processes you didn't spawn via execute.async. For tasks, prefer cancel.

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