Thursday, October 27, 2022

[SOLVED] Execute a command each time curl outputs a newline

Issue

I'm trying to use curl to infinitely stream messages from a remote server.

I periodically receive notifications like this:

$ curl -s https://my-server.com/status
Backup in progress...
Backup completed.
Disk space is about to fill up.

I want send_notify to be executed each time curl outputs a new line.
I created the script below, but it did not work.

notify_pipe() {
    read -r msg
    notify-send "$msg"
}

# Does nothing, just a blinking cursor.
stdbuf -oL curl -s https://my-server.com/status | notify_pipe 

Please help me understand why this is not working and how to make it work.

I'm a beginner, I would appreciate any help! (and please forgive my poor English)


Solution

You need to read the output lines in a loop:

stdbuf -oL curl -s https://my-server.com/status |
    while read line ; do
        notify-send Status "$line"
    done


Answered By - choroba
Answer Checked By - Terry (WPSolving Volunteer)