---
# System prepended metadata

title: PID-1156

---

To run socat forever and have it automatically retry indefinitely upon failure, you can use a simple shell script or a tool like systemd on Linux [1][5]. Here are two approaches you can use:

Approach 1: Using a Shell Script
You can create a simple shell script that runs socat in a loop and sleeps for a while before retrying. This script will keep retrying indefinitely.
1. Create a shell script, e.g., run_socat.sh, with the following content:
    ```bash
    #!/bin/bash

    while true; do
        socat [your_socat_options_here]
        sleep 1  # Adjust the sleep time as needed
    done
    ```
    Replace [your_socat_options_here] with the actual socat command and options you want to run. Adjust the sleep time to control how long the script waits between retries.
2. Make the script executable:
    ```bash
    chmod +x run_socat.sh
    ```
3. Run the script:
    ```bash
    ./run_socat.sh
    ```
    This script will keep restarting socat whenever it exits, effectively running it forever.

Approach 2: Using systemd (Linux)
Another approach is to create a systemd service unit to manage socat and ensure it runs forever. This is more robust and suitable for long-term services.
1. Create a systemd service unit file, e.g., /etc/systemd/system/socat.service, with the following content:
    ```bash
    [Unit]
    Description=Socat Service

    [Service]
    ExecStart=/usr/bin/socat [your_socat_options_here]
    Restart=always
    RestartSec=10  # Adjust the restart delay as needed

    [Install]
    WantedBy=multi-user.target
    ```
    Replace /usr/bin/socat with the actual path to the socat executable and `[your_socat_options_here]` with the socat command and options you want to run. Adjust the RestartSec value to control how long systemd waits before restarting socat.
2. Reload the systemd configuration:
    ```bash
    sudo systemctl daemon-reload
    ```
3. Enable the socat service to start at boot:
    ```bash
    sudo systemctl enable socat.service
    ```
4. Start the socat service:
    ```bash
    sudo systemctl start socat.service
    ```
    With this setup, systemd will manage socat, automatically restarting it if it exits and ensuring it runs continuously.

Choose the approach that best fits your requirements and system setup. The systemd approach is recommended for long-running services, while the shell script approach is more straightforward for ad-hoc use.
You can adjust the options according to your specific needs. Note that some users have reported issues with socat when used to establish a persistent connection, and recommend using other tools instead [6].