# Run Python script continuously from Logstash

**URL:** <https://discuss.elastic.co/t/run-python-script-continuously-from-logstash/307656>\
**Category:** Logstash\
**Created:** [June 20, 2022, 1:21pm UTC](https://discuss.elastic.co/t/run-python-script-continuously-from-logstash/307656 "2022-06-20T13:21:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![priyankamondalhcl](https://avatars.discourse-cdn.com/v4/letter/p/57b2e6/32.png) [@priyankamondalhcl](https://discuss.elastic.co/u/priyankamondalhcl)\
**Post date:** [June 20, 2022, 1:21pm UTC](https://discuss.elastic.co/t/run-python-script-continuously-from-logstash/307656/1 "2022-06-20T13:21:16Z")

</div>

Hi Team,

My requirement is to run python script in background from logstash, and the output I need to take as event in logstash how to achieve that.

Note: I am using exec input plugins, which eventually is not sending data until script exits, also it is running in interval.

let me know if you need any further detail to analyze my issue.

---

<div class="post-metadata">

**Author:** ![leandrojmp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leandrojmp/32/107231_2.png) [@leandrojmp](https://discuss.elastic.co/u/leandrojmp)\
**Post date:** [June 20, 2022, 1:45pm UTC](https://discuss.elastic.co/t/run-python-script-continuously-from-logstash/307656/2 "2022-06-20T13:45:05Z")

</div>

By running in background you mean let it running or execute the script in some scheduled interval?

The `exec` input executes a command in the scheduled interval and populates the field message with the output of the command.

If you need to make your python script running constantly, like a service, then I'm not sure you can do that from logstash.

---

<div class="post-metadata">

**Author:** ![priyankamondalhcl](https://avatars.discourse-cdn.com/v4/letter/p/57b2e6/32.png) [@priyankamondalhcl](https://discuss.elastic.co/u/priyankamondalhcl)\
**Post date:** [June 22, 2022, 12:33pm UTC](https://discuss.elastic.co/t/run-python-script-continuously-from-logstash/307656/3 "2022-06-22T12:33:50Z")

</div>

I am running python script which is capturing snmptrap and printing the data in console whenever there is trap. But while running the script from logstash manually I can check python script running in background, it is not printing the data. Or to be more precise data is displaying only after closing the logstash process (which I am running manually)  
I used exec and pipe input plugin, nothing worked. Please help me here.

---

<div class="post-metadata">

**Author:** ![leandrojmp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leandrojmp/32/107231_2.png) [@leandrojmp](https://discuss.elastic.co/u/leandrojmp)\
**Post date:** [June 22, 2022, 1:13pm UTC](https://discuss.elastic.co/t/run-python-script-continuously-from-logstash/307656/4 "2022-06-22T13:13:09Z")

</div>

> [@priyankamondalhcl](#):
>
> I am running python script which is capturing snmptrap and printing the data in console whenever there is trap.

So you need your script to keep running, isn't just something that you run and it prints an output and exit. As I said, I don't think you can use the `exec` input for scripts like that.

The exec input is for things that you run, execute some logic and prints an output, not for long running scripts that periodically print stuff, you will only get the script output after it stopped running, this is the expected behavior of the `exec` input.

If you want to use this python script you will need to change it and make it output to a file or to a port and configure your logstash to read from the file or listen on the port.

---

<div class="post-metadata">

**Author:** ![priyankamondalhcl](https://avatars.discourse-cdn.com/v4/letter/p/57b2e6/32.png) [@priyankamondalhcl](https://discuss.elastic.co/u/priyankamondalhcl)\
**Post date:** [June 27, 2022, 5:12am UTC](https://discuss.elastic.co/t/run-python-script-continuously-from-logstash/307656/5 "2022-06-27T05:12:04Z")

</div>

Thanks @leandrojmp for your suggestion. I am using pipe input plugins to long run python script, and also http input plugins to open a new port on local. Further python script is posting all the incoming event to that port (modified the print section in the python script itself)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 25, 2022, 5:12am UTC](https://discuss.elastic.co/t/run-python-script-continuously-from-logstash/307656/6 "2022-07-25T05:12:28Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
