Skip to content

Commit d005747

Browse files
committed
chore: unix exit command handling
1 parent 457557f commit d005747

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

openagent/cli.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
import signal
44
import sys
55
import click
6-
from dotenv import load_dotenv
76
import uvicorn
87
import threading
98
import time
109

10+
from dotenv import load_dotenv
1111
from openagent.agent.agent import OpenAgent
1212
from openagent.api.app import app, set_agent
1313

@@ -41,6 +41,24 @@ def check_exit_windows(loop):
4141
time.sleep(1)
4242

4343

44+
def check_exit_unix(loop):
45+
"""Check for exit command on Unix (press 'q' to quit)"""
46+
print("\nPress 'q' to quit...")
47+
while True:
48+
try:
49+
# Non-blocking read from stdin
50+
import select
51+
52+
if select.select([sys.stdin], [], [], 0)[0]:
53+
key = sys.stdin.read(1)
54+
if key == "q":
55+
print("\nShutting down...")
56+
os._exit(0)
57+
except Exception as e:
58+
print(f"Error reading input: {e}")
59+
time.sleep(1)
60+
61+
4462
@click.group()
4563
def cli():
4664
"""OpenAgent CLI tool"""
@@ -63,6 +81,7 @@ def start(file, host, port):
6381
loop = asyncio.new_event_loop()
6482
asyncio.set_event_loop(loop)
6583

84+
# Set platform specific handlers
6685
if sys.platform == "win32":
6786
signal.signal(signal.SIGINT, win_handler)
6887
check_exit_func = check_exit_windows
@@ -73,6 +92,8 @@ def start(file, host, port):
7392
loop.add_signal_handler(
7493
signal.SIGTERM, lambda: asyncio.create_task(shutdown(agent, loop))
7594
)
95+
check_exit_func = check_exit_unix
96+
7697
# Create FastAPI config
7798
config = uvicorn.Config(app, host=host, port=port, loop=loop)
7899
server = uvicorn.Server(config)

0 commit comments

Comments
 (0)