Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop Logstash process automatically after imported all data

Situation:

  • I'm importing data to Elasticsearch via Logstash at 12 pm manually every day.
  • I understand there is no "close" on Logstash because ideally, you would want to continuously send data to the server.
  • I am using elk-docker as my ELK stack.
  • I wrote a shell script that sends a command to a docker container to execute the following:

dailyImport.sh

docker exec -it $DOCKER_CONTAINER_NAME opt/logstash/bin/logstash --path.data /tmp/logstash/data -e \
'input {
    file {
        path => "'$OUTPUT_PATH'"
        start_position => "beginning"
        sincedb_path => "/dev/null"
        mode => "read"
        file_completed_action => "delete"
    }
}
filter {
    csv {
        separator => ","
        columns => ["foo", "bar", "foo2", "bar2"]
    }

}

output {
    elasticsearch{
        hosts => "localhost:9200"
        index => "foo"
        document_type => "foo"
    }
    stdout {}
}'

What I have tried and understood:

  • I have read that adding read mode and file_completed_action to delete would stop the operation, I tried it but it didn't work.
  • I would still need to send Ctrl + C manually to stop the pipeline. e.g:
^C[2019-02-21T15:49:07,787][WARN ][logstash.runner          ] SIGINT received. Shutting down.
[2019-02-21T15:49:07,899][INFO ][filewatch.observingread  ] QUIT - closing all files and shutting down.
[2019-02-21T15:49:09,764][INFO ][logstash.pipeline        ] Pipeline has terminated {:pipeline_id=>"main", :thread=>"#<Thread:0x6bdb92ea run>"}
Done

I have read that I could do the following, but don't know how:

  • Monitor the sincedb file to check when Logstash has reached EOF, then kill Logstash.
  • Use the stdin input instead. Logstash will shut down by itself when stdin has been closed and all inputs has been processed. On the flip side, it Logstash dies for whatever reason you don't know how much it has processed.

Reference: https://discuss.elastic.co/t/stop-logstash-after-processing-the-file/84959

What I want:

  • I don't need a fancy progress bar to tell me how much data I have imported (against the input file).
  • I only want to end the operation when "it's done" and maybe send a Ctrl + C when it reaches the EOF or "finished importing".
like image 601
Anson C Avatar asked Feb 21 '19 16:02

Anson C


People also ask

How do I stop Logstash from terminal?

Logstash responds to a SIGTERM by attempting to halt inputs and waiting for pending events to finish processing before shutting down. Show activity on this post. kill -SIGHUP {logstash_pid} reload the config file and restart the pipeline.

How do I keep Logstash running?

First you open your SSH session, then type screen at the prompt. That opens a new session in which you can run your logstash command. When it runs, you simply press Ctrl+a d in order to detach your self from that screen and you can safely logout.

How do I know if Logstash is running?

The most basic thing to check is the status of the Logstash status: sudo service logstash status.


1 Answers

for file input in read mode there's recently a way to exit the process upon reading all files, just set:

input { file { exit_after_read => true } }

https://www.elastic.co/guide/en/logstash/current/plugins-inputs-file.html#plugins-inputs-file-exit_after_read

like image 96
kares Avatar answered Nov 15 '22 10:11

kares