<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to write a file into the app folder through a python script in Splunk Dev</title>
    <link>https://community.splunk.com/t5/Splunk-Dev/How-to-write-a-file-into-the-app-folder-through-a-python-script/m-p/183612#M2441</link>
    <description>&lt;P&gt;Look into what is called a Scripted Input, defined in the inputs.conf of your app.  Running a search to execute a script is a bit "heavy."  The scripted input can be set to execute however often you want, and splunkd will execute it for you on that schedule.  Also, the Std Out of your script would get indexed as well, so make sure you configure the index, source, sourcetype for it to show where you want that data to go.  The script itself could do anything - write your file, run a unix command (that's how the unix app stuff works in Splunk), fetch data from an external source for indexing, etc.&lt;/P&gt;

&lt;P&gt;Also, what you did in commands.conf above would be for creating a custom search command in Splunk.  E.g. you want to make a new command called my_script:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; ... | eval foo="bar" | my_script | head 10
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 30 May 2014 14:28:55 GMT</pubDate>
    <dc:creator>jhupka</dc:creator>
    <dc:date>2014-05-30T14:28:55Z</dc:date>
    <item>
      <title>How to write a file into the app folder through a python script</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-to-write-a-file-into-the-app-folder-through-a-python-script/m-p/183611#M2440</link>
      <description>&lt;P&gt;Hi,&lt;BR /&gt;
I have to schedule a script execution in my splunk app with Splunk 6.1.&lt;BR /&gt;
I think to do that with a scheduled query, something like&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| script python my_script
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;adding in my commands.conf file:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;[my_script]
filename = my_script.py
stderr_dest = message
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;here is my_script.py:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;with open('example.txt', 'wb') as handle:
    handle.write("this is a test")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I tried to launch manually the query. It returns no errors but I can't find example.txt file. What am I doing wrong?&lt;/P&gt;</description>
      <pubDate>Fri, 30 May 2014 10:39:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-to-write-a-file-into-the-app-folder-through-a-python-script/m-p/183611#M2440</guid>
      <dc:creator>RiccardoV</dc:creator>
      <dc:date>2014-05-30T10:39:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a file into the app folder through a python script</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-to-write-a-file-into-the-app-folder-through-a-python-script/m-p/183612#M2441</link>
      <description>&lt;P&gt;Look into what is called a Scripted Input, defined in the inputs.conf of your app.  Running a search to execute a script is a bit "heavy."  The scripted input can be set to execute however often you want, and splunkd will execute it for you on that schedule.  Also, the Std Out of your script would get indexed as well, so make sure you configure the index, source, sourcetype for it to show where you want that data to go.  The script itself could do anything - write your file, run a unix command (that's how the unix app stuff works in Splunk), fetch data from an external source for indexing, etc.&lt;/P&gt;

&lt;P&gt;Also, what you did in commands.conf above would be for creating a custom search command in Splunk.  E.g. you want to make a new command called my_script:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; ... | eval foo="bar" | my_script | head 10
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 30 May 2014 14:28:55 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-to-write-a-file-into-the-app-folder-through-a-python-script/m-p/183612#M2441</guid>
      <dc:creator>jhupka</dc:creator>
      <dc:date>2014-05-30T14:28:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a file into the app folder through a python script</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-to-write-a-file-into-the-app-folder-through-a-python-script/m-p/183613#M2442</link>
      <description>&lt;P&gt;Thanks for your reply, but I still don't understand how to write a file from a python script. Where can I find my script's output?&lt;/P&gt;</description>
      <pubDate>Tue, 03 Jun 2014 08:04:27 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-to-write-a-file-into-the-app-folder-through-a-python-script/m-p/183613#M2442</guid>
      <dc:creator>RiccardoV</dc:creator>
      <dc:date>2014-06-03T08:04:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a file into the app folder through a python script</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-to-write-a-file-into-the-app-folder-through-a-python-script/m-p/183614#M2443</link>
      <description>&lt;P&gt;Finally I understand why it didn't working &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;

&lt;P&gt;I have to use absolute path:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;import os
filepath = os.path.join(os.environ['SPLUNK_HOME'], 'etc', 'apps', APP_NAME, TARGET_DIR, "test.txt")

with open(filepath, 'wb') as handle:
  handle.write("test")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;and it works like a charm &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Jun 2014 08:34:39 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-to-write-a-file-into-the-app-folder-through-a-python-script/m-p/183614#M2443</guid>
      <dc:creator>RiccardoV</dc:creator>
      <dc:date>2014-06-03T08:34:39Z</dc:date>
    </item>
  </channel>
</rss>

