You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将输入从其他终端或脚本重定向至运行中的终端/脚本?

Keep Your Long-Running Evaluation Script Alive & Feed It Inputs Remotely

Absolutely, you can keep your resource-heavy file evaluation script running continuously and send it new paths from other terminals or scripts—no need to restart it every time. Here are three practical, battle-tested methods to make this work:

Method 1: Named Pipe (FIFO) – Simple & Reliable

A named pipe is a special file that acts like a bridge between processes. It’s perfect for this use case because it lets you send input to your script whenever you want, while keeping the script running indefinitely.

Steps to set it up:

  1. Create the named pipe in your terminal:
    mkfifo /tmp/script_input_pipe
    
  2. Modify your script to read from the pipe (or start it with the piped input). If your script uses a loop to process inputs, update it to read from the pipe:
    # Inside your script, replace any existing input logic with this loop
    while read -r file_path; do
        # Your existing file evaluation logic here
        echo "Evaluating files at: $file_path"
        evaluate_files "$file_path"
    done < /tmp/script_input_pipe
    
  3. Send inputs from other terminals/scripts:
    • Single path:
      echo "/home/user/documents/reports" > /tmp/script_input_pipe
      
    • Multiple paths from a file:
      cat /path/to/list_of_paths.txt > /tmp/script_input_pipe
      

The pipe will block your script until new input arrives, so it stays alive and ready to process new paths whenever you send them.

Method 2: Unix Domain Socket – Flexible for Complex Use Cases

If you need more control (like sending structured data, handling multiple concurrent senders, or even remote access), a Unix domain socket is a great choice. It’s faster than TCP sockets for local use and secure.

Example with nc (netcat):

  1. Start your script listening on a socket:
    nc -lU /tmp/script_socket | while read -r file_path; do
        evaluate_files "$file_path"
    done
    
  2. Send inputs from another terminal:
    echo "/home/user/photos" | nc -U /tmp/script_socket
    

If your script is written in Python, you can build a simple socket server directly into it to handle inputs more granularly—this gives you full control over parsing and error handling.

Method 3: Tail a Log File – For Batch or Scheduled Inputs

If you want to queue up paths over time (e.g., from a cron job or another service), you can have your script tail a log file and process new lines as they’re added.

Setup:

  1. Create a dedicated input file:
    touch /tmp/script_inputs.log
    
  2. Run your script with tail -f to follow the file:
    tail -f /tmp/script_inputs.log | while read -r file_path; do
        evaluate_files "$file_path"
    done
    
  3. Add paths to the file from anywhere:
    echo "/var/log/app_logs" >> /tmp/script_inputs.log
    

Pro Tips:

  • Handle invalid inputs: Add checks in your loop to skip empty lines or invalid paths (e.g., if [ -d "$file_path" ]; then ... fi).
  • Permissions: Make sure the pipe/socket/file has the right permissions so other users/scripts can write to it (use chmod if needed).
  • Persist across reboots: If you want the pipe/socket to exist after a reboot, add the mkfifo command to your .bashrc or a system startup script.

内容的提问来源于stack exchange,提问作者Emir Husic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:06:39