Run commands in background
To run commands in background, pass the background option to the commands.run() method. This will return immediately and the command will continue to run in the sandbox. You can then later kill the command using the commands.kill() method.
You can use the wait()
method to wait for the command to finish.
from agentbox import Sandbox
sandbox = Sandbox(api_key="ab_xxxxxxxxxxxxxxxxxxxxxxxxx",
template="wemmodr8mb2uk3kn7exw",
timeout=120)
# Start the command in the background
command = sandbox.commands.run('echo hello; sleep 10; echo world', background=True)
# Get stdout and stderr from the command running in the background.
# You can run this code in a separate thread or use command.wait() to wait for the command to finish.
for stdout, stderr, _ in command:
if stdout:
print(stdout)
if stderr:
print(stderr)
# Kill the command
command.kill()