# 列出任务 list_tasks() { local task_dir=$(get_task_dir) if [[ ! -d "$task_dir" ]]; then echo"没有任务" return 0 fi local tasks=() # 遍历任务目录 fordirin"$task_dir"/task_*/; do if [[ -d "$dir" ]]; then local info_file="$dir/info.json" if [[ -f "$info_file" ]]; then # 过滤状态 if [[ -n "$STATUS" ]]; then local task_status=$(grep -o '"status": "[^"]*"'"$info_file" | cut -d'"' -f4) if [[ "$task_status" != "$STATUS" ]]; then continue fi fi tasks+=("$info_file") fi fi done # 排序 (按创建时间倒序) IFS=$'\n' sorted=($(ls -t "${tasks[@]}" 2>/dev/null)) unset IFS # 限制数量 local count=0 for info_file in"${sorted[@]}"; do if [[ $count -ge $LIMIT ]]; then break fi # 解析信息 ifcommand -v jq &> /dev/null; then localid=$(jq -r '.id'"$info_file") local name=$(jq -r '.name'"$info_file") local status=$(jq -r '.status'"$info_file") local created=$(jq -r '.created_at'"$info_file") case"$FORMAT"in "table") printf"%-25s %-20s %-12s %s\n""$id""$name""$status""$created" ;; "json") jq '.'"$info_file" ;; "simple") echo"$id: $name ($status)" ;; esac else # 简单解析 (无 jq) localid=$(grep -o '"id": "[^"]*"'"$info_file" | cut -d'"' -f4) local name=$(grep -o '"name": "[^"]*"'"$info_file" | cut -d'"' -f4) local status=$(grep -o '"status": "[^"]*"'"$info_file" | cut -d'"' -f4) local created=$(grep -o '"created_at": "[^"]*"'"$info_file" | cut -d'"' -f4) case"$FORMAT"in "table") printf"%-25s %-20s %-12s %s\n""$id""$name""$status""$created" ;; "simple") echo"$id: $name ($status)" ;; esac fi ((count++)) done if [[ $count -eq 0 ]]; then echo"没有任务" fi }
# 显示统计 show_stats() { local task_dir=$(get_task_dir) if [[ ! -d "$task_dir" ]]; then return 0 fi local total=0 local running=0 local completed=0 local failed=0 fordirin"$task_dir"/task_*/; do if [[ -d "$dir" ]]; then local info_file="$dir/info.json" if [[ -f "$info_file" ]]; then ((total++)) local status=$(grep -o '"status": "[^"]*"'"$info_file" | cut -d'"' -f4) case"$status"in "running") ((running++)) ;; "completed") ((completed++)) ;; "failed"|"timed_out") ((failed++)) ;; esac fi fi done echo"" echo"统计:" echo" 总计:$total" echo" 运行中:$running" echo" 已完成:$completed" echo" 失败:$failed" }
# 主函数 main() { if [[ "$FORMAT" == "table" ]]; then printf"%-25s %-20s %-12s %s\n""ID""名称""状态""创建时间" echo"--------------------------------------------------------------------------------" fi list_tasks if [[ "$FORMAT" == "table" ]]; then show_stats fi }
# 获取任务目录 get_task_dir() { local task_id="$1" echo"$HOME/.openclaw/tasks/$task_id" }
# 显示输出 show_output() { local task_id="$1" local task_dir=$(get_task_dir "$task_id") if [[ ! -d "$task_dir" ]]; then echo"Error: Task not found: $task_id" exit 1 fi local info_file="$task_dir/info.json" local log_file="$task_dir/output.log" local error_file="$task_dir/error.log" # 显示任务信息 echo"任务信息:" ifcommand -v jq &> /dev/null; then jq '{id, name, status, exit_code}'"$info_file" else grep -E '"(id|name|status|exit_code)"'"$info_file" fi echo"" # 显示输出 echo"输出:" if [[ -f "$log_file" ]]; then if [[ "$TYPE" == "all" ]] || [[ "$TYPE" == "stdout" ]]; then tail -n "$LINES""$log_file" fi fi if [[ -f "$error_file" ]]; then if [[ "$TYPE" == "all" ]] || [[ "$TYPE" == "stderr" ]]; then echo"--- 错误输出 ---" >&2 tail -n "$LINES""$error_file" >&2 fi fi }
# 跟踪输出 follow_output() { local task_id="$1" local task_dir=$(get_task_dir "$task_id") if [[ ! -d "$task_dir" ]]; then echo"Error: Task not found: $task_id" exit 1 fi local log_file="$task_dir/output.log" # 等待文件创建 while [[ ! -f "$log_file" ]]; do sleep 0.5 # 检查任务是否结束 local status=$(grep -o '"status": "[^"]*"'"$task_dir/info.json" | cut -d'"' -f4) if [[ "$status" == "completed" ]] || [[ "$status" == "failed" ]]; then break fi done # 跟踪输出 tail -f "$log_file" }
# 主函数 main() { if [[ -z "$TASK_ID" ]]; then echo"Error: --task-id is required" exit 1 fi if [[ "$FOLLOW" == true ]]; then follow_output "$TASK_ID" else show_output "$TASK_ID" fi }
# 停止任务 stop_task() { local task_id="$1" local force="$2" local task_dir="$HOME/.openclaw/tasks/$task_id" if [[ ! -d "$task_dir" ]]; then echo"Error: Task not found: $task_id" exit 1 fi local info_file="$task_dir/info.json" # 获取任务状态 local status=$(grep -o '"status": "[^"]*"'"$info_file" | cut -d'"' -f4) if [[ "$status" == "completed" ]] || [[ "$status" == "failed" ]]; then echo"任务已结束:$status" return 0 fi # 获取 PID local pid=$(grep -o '"pid": [0-9]*'"$info_file" | grep -o '[0-9]*') if [[ -z "$pid" ]]; then echo"Error: Task PID not found" exit 1 fi # 停止进程 if [[ "$force" == true ]]; then echo"强制停止任务..." kill -9 "$pid" 2>/dev/null || true else echo"停止任务..." kill -TERM "$pid" 2>/dev/null || true # 等待进程结束 localtimeout=10 while [[ $timeout -gt 0 ]]; do if ! kill -0 "$pid" 2>/dev/null; then break fi sleep 1 ((timeout--)) done # 如果还在运行,强制停止 ifkill -0 "$pid" 2>/dev/null; then echo"任务未响应,强制停止..." kill -9 "$pid" 2>/dev/null || true fi fi # 更新状态 local temp_file="$task_dir/info.json.tmp" ifcommand -v jq &> /dev/null; then jq --arg status "cancelled" \ --arg timestamp "$(date -Iseconds)" \ '.status = $status | .completed_at = $timestamp' \ "$info_file" > "$temp_file" mv"$temp_file""$info_file" fi echo"任务已停止" }
# 清理任务 cleanup_task() { local task_id="$1" local task_dir="$HOME/.openclaw/tasks/$task_id" if [[ -d "$task_dir" ]]; then rm -rf "$task_dir" echo"任务目录已清理:$task_dir" fi }
# 主函数 main() { if [[ -z "$TASK_ID" ]]; then echo"Error: --task-id is required" exit 1 fi stop_task "$TASK_ID""$FORCE" }