<?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: Scripted input without a shell? in Deployment Architecture</title>
    <link>https://community.splunk.com/t5/Deployment-Architecture/Scripted-input-without-a-shell/m-p/55660#M1824</link>
    <description>&lt;P&gt;The processes not being killed is in fact due to Splunk's own behavior, and will not change regardless. While you can call any executable directly from a scripted input, they'll all behave that way. They are launched with &lt;CODE&gt;nohup&lt;/CODE&gt; on Unix, and Windows inherently doesn't kill processes. It's pretty much a bug I think, since I see absolutely no use for the current behavior, but maybe not.&lt;/P&gt;

&lt;P&gt;As a possibly complicated workaround, I would recommend that any long-running custom script should occasionally exit maybe every 10 minutes or 30 minutes, or at some convenient time for the script, and then be scheduled to restart 1 second or zero seconds after exit. Or perhaps every so often see what it's stdout is connected to and exit if there's nothing there.&lt;/P&gt;

&lt;P&gt;You should probably file an ER with Splunk for a parameter for each scripted input stanza to have Splunkd kill child processes/processes launched by the script on exit. Or just file it as a bug.&lt;/P&gt;</description>
    <pubDate>Tue, 09 Aug 2011 20:04:26 GMT</pubDate>
    <dc:creator>gkanapathy</dc:creator>
    <dc:date>2011-08-09T20:04:26Z</dc:date>
    <item>
      <title>Scripted input without a shell?</title>
      <link>https://community.splunk.com/t5/Deployment-Architecture/Scripted-input-without-a-shell/m-p/55659#M1823</link>
      <description>&lt;P&gt;Is it possible to create a scripted input that is launched directly from &lt;CODE&gt;splunkd&lt;/CODE&gt; and not from a shell?   I've tried shell script, a python script, and a &lt;CODE&gt;.path&lt;/CODE&gt; file, and they all seem to be wrapped by a "&lt;CODE&gt;/bin/sh -c &amp;lt;MY_COMMAND&amp;gt;&lt;/CODE&gt;" wrapper shell.&lt;/P&gt;

&lt;P&gt;I have long-running scripted input, and the process is not being shutdown when splunkd restarts, which I think is due to the extra &lt;CODE&gt;sh&lt;/CODE&gt; process not passing down the kill signal to my process.&lt;/P&gt;

&lt;HR /&gt;

&lt;P&gt;&lt;STRONG&gt;Update / Additional info:&lt;/STRONG&gt;&lt;/P&gt;

&lt;P&gt;I know that splunkd is stopping the shell whenever &lt;CODE&gt;splunkd&lt;/CODE&gt; is shutdown or whenever the scripted-input is disabled. (Note:  To save time during testing, I've been enabling and disabling my scripted input stanza in &lt;CODE&gt;inputs.conf&lt;/CODE&gt;, then issuing the the following refresh link:  &lt;CODE&gt;&lt;A href="http://mysplunkserver:8000/en-US/debug/refresh?entity=admin%2Fscript" target="test_blank"&gt;http://mysplunkserver:8000/en-US/debug/refresh?entity=admin%2Fscript&lt;/A&gt;&lt;/CODE&gt;, which has been working like a charm.)  Whenever the input is disabled (or &lt;CODE&gt;splunkd&lt;/CODE&gt; shutdown), the shell wrapper process goes away, but the child process (aka, my scripted input program) continues to run.  But instead of my process being a grandchild of &lt;CODE&gt;splunkd&lt;/CODE&gt;, now it's directly under process 1 (init).&lt;/P&gt;

&lt;P&gt;Here are two examples showing the processes running on my system (output generated by &lt;CODE&gt;pstree -A -p&lt;/CODE&gt;).&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;Example 1:&lt;/STRONG&gt;  This shows my scripted input when it's enabled.  My scripted input process is pid 4177, with several threads.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;init(1)-+
        |-splunkd(2642)-+-splunkd(2643)-+-sh(4176)---java(4177)-+-{java}(4185)
        |               |               |                       |-{java}(4186)
        |               |               |                       |-{java}(4187)
        |               |               |                       |-{java}(4188)
        |               |               |                       |-{java}(4189)
        |               |               |                       |-{java}(4191)
        |               |               |                       |-{java}(4194)
        |               |               |                       |-{java}(4195)
        |               |               |                       |-{java}(4201)
        |               |               |                       |-{java}(4202)
        |               |               |                       |-{java}(4203)
        |               |               |                       |-{java}(4204)
        |               |               |                       |-{java}(4205)
        |               |               |                       |-{java}(4207)
        |               |               |                       `-{java}(4211)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;STRONG&gt;Example 2:&lt;/STRONG&gt;  I then disabled by input (&lt;CODE&gt;disabled=1&lt;/CODE&gt; in &lt;CODE&gt;inputs.conf&lt;/CODE&gt;), then refreshed the "admin/script" entities, and now my process tree looks as follows:  (Note that the "java" process is now owned by init, and the wrapper shell (4176) is now gone.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;init(1)-+
        |-java(4177)-+-{java}(4185)
        |            |-{java}(4186)
        |            |-{java}(4187)
        |            |-{java}(4188)
        |            |-{java}(4189)
        |            |-{java}(4191)
        |            |-{java}(4194)
        |            |-{java}(4195)
        |            |-{java}(4201)
        |            |-{java}(4202)
        |            |-{java}(4203)
        |            |-{java}(4204)
        |            |-{java}(4205)
        |            |-{java}(4207)
        |            `-{java}(4211)
        |-splunkd(2642)-+-splunkd(2643)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The problem seems to be that the wrapper shell (&lt;CODE&gt;/bin/sh&lt;/CODE&gt;) is simply not passing on the kill request.  &lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;Note:&lt;/STRONG&gt;  As shown above, I'm using my own wrapper script to setup the environment and launching the java executable using &lt;CODE&gt;exec&lt;/CODE&gt; to prevent an additional shell layer in the mix.  I've messed around with using traps and such (when I wasn't using &lt;CODE&gt;exec&lt;/CODE&gt;, of course), but ultimately if the parent process (aka the &lt;CODE&gt;/bin/sh&lt;/CODE&gt; wrapper shell) doesn't pass down the signal, there's nothing to trap.  My only other option is implementing some kind of polling mechanism to see if my parent process is dead.  So once again, I'm back to:  How do I turn off that annoying wrapper shell and keep things simple?&lt;/P&gt;

&lt;HR /&gt;

&lt;P&gt;I'm running Splunk 4.1.8 on Ubuntu 8.04 (32 bit) and &lt;CODE&gt;/bin/sh&lt;/CODE&gt; is currently using &lt;CODE&gt;dash&lt;/CODE&gt; (which is the Ubuntu default)&lt;/P&gt;</description>
      <pubDate>Tue, 09 Aug 2011 19:38:16 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Deployment-Architecture/Scripted-input-without-a-shell/m-p/55659#M1823</guid>
      <dc:creator>Lowell</dc:creator>
      <dc:date>2011-08-09T19:38:16Z</dc:date>
    </item>
    <item>
      <title>Re: Scripted input without a shell?</title>
      <link>https://community.splunk.com/t5/Deployment-Architecture/Scripted-input-without-a-shell/m-p/55660#M1824</link>
      <description>&lt;P&gt;The processes not being killed is in fact due to Splunk's own behavior, and will not change regardless. While you can call any executable directly from a scripted input, they'll all behave that way. They are launched with &lt;CODE&gt;nohup&lt;/CODE&gt; on Unix, and Windows inherently doesn't kill processes. It's pretty much a bug I think, since I see absolutely no use for the current behavior, but maybe not.&lt;/P&gt;

&lt;P&gt;As a possibly complicated workaround, I would recommend that any long-running custom script should occasionally exit maybe every 10 minutes or 30 minutes, or at some convenient time for the script, and then be scheduled to restart 1 second or zero seconds after exit. Or perhaps every so often see what it's stdout is connected to and exit if there's nothing there.&lt;/P&gt;

&lt;P&gt;You should probably file an ER with Splunk for a parameter for each scripted input stanza to have Splunkd kill child processes/processes launched by the script on exit. Or just file it as a bug.&lt;/P&gt;</description>
      <pubDate>Tue, 09 Aug 2011 20:04:26 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Deployment-Architecture/Scripted-input-without-a-shell/m-p/55660#M1824</guid>
      <dc:creator>gkanapathy</dc:creator>
      <dc:date>2011-08-09T20:04:26Z</dc:date>
    </item>
    <item>
      <title>Re: Scripted input without a shell?</title>
      <link>https://community.splunk.com/t5/Deployment-Architecture/Scripted-input-without-a-shell/m-p/55661#M1825</link>
      <description>&lt;P&gt;I know your the splunk guru here, but I don't think you're completely right on this one.  Splunk does appear to kill the wrapping shell process when the scripted input is disabled.  I've added some additional info to the original question.&lt;/P&gt;</description>
      <pubDate>Tue, 09 Aug 2011 21:02:37 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Deployment-Architecture/Scripted-input-without-a-shell/m-p/55661#M1825</guid>
      <dc:creator>Lowell</dc:creator>
      <dc:date>2011-08-09T21:02:37Z</dc:date>
    </item>
    <item>
      <title>Re: Scripted input without a shell?</title>
      <link>https://community.splunk.com/t5/Deployment-Architecture/Scripted-input-without-a-shell/m-p/55662#M1826</link>
      <description>&lt;P&gt;Agree that the current behavior makes no sense.  We are seeing the same issue in the VMware App.  Our scripted input's child process doesn't get shut down when Splunkd shuts down and the /bin/sh exits.  We have found that it only happens on Ubuntu - i.e. it is fine on CentOS - so we're going to file it as a bug.&lt;/P&gt;</description>
      <pubDate>Tue, 30 Aug 2011 04:41:50 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Deployment-Architecture/Scripted-input-without-a-shell/m-p/55662#M1826</guid>
      <dc:creator>Curt_Collins</dc:creator>
      <dc:date>2011-08-30T04:41:50Z</dc:date>
    </item>
    <item>
      <title>Re: Scripted input without a shell?</title>
      <link>https://community.splunk.com/t5/Deployment-Architecture/Scripted-input-without-a-shell/m-p/55663#M1827</link>
      <description>&lt;P&gt;Here's a python script I'm using in various scenarios where I'm invoking a subprocess that should be killed when the script is disabled or splunk is stopped:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;import sys
import signal
from subprocess import *

process = Popen(...)

def cleanup(s, f):
    try:
        process.terminate()
    except:
        sys.exit(1)

signal.signal(signal.SIGTERM, cleanup)
(out,err) = process.communicate()
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Splunk sends a SIGTERM to the script and the callback attached to the signal terminates the subprocess. &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;if os.uname()[0] == 'Linux':
    from threading import Thread
    import time

    class PPIDWatcher(Thread):
        def __init__(self): super(PPIDWatcher, self).__init__()
        def run(self):
            ppid = os.getppid()
            while True:
                time.sleep(1)
                try:
                    os.kill(ppid,0)
                except:
                    cleanup()
    PPIDWatcher().start()
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 30 Aug 2011 09:22:36 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Deployment-Architecture/Scripted-input-without-a-shell/m-p/55663#M1827</guid>
      <dc:creator>ziegfried</dc:creator>
      <dc:date>2011-08-30T09:22:36Z</dc:date>
    </item>
    <item>
      <title>Re: Scripted input without a shell?</title>
      <link>https://community.splunk.com/t5/Deployment-Architecture/Scripted-input-without-a-shell/m-p/55664#M1828</link>
      <description>&lt;P&gt;I've tried this approach, but the problem I had was that the "splunkd" process was killing the shell script that was  wrapping the python script; and the signal never got passed down from that shell to the python process.  It could be a version/OS thing.  What OS are you using, and which version of Splunk?&lt;/P&gt;</description>
      <pubDate>Tue, 30 Aug 2011 17:45:55 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Deployment-Architecture/Scripted-input-without-a-shell/m-p/55664#M1828</guid>
      <dc:creator>Lowell</dc:creator>
      <dc:date>2011-08-30T17:45:55Z</dc:date>
    </item>
    <item>
      <title>Re: Scripted input without a shell?</title>
      <link>https://community.splunk.com/t5/Deployment-Architecture/Scripted-input-without-a-shell/m-p/55665#M1829</link>
      <description>&lt;P&gt;Out of curiosity, does your &lt;CODE&gt;/bin/sh&lt;/CODE&gt; point to &lt;CODE&gt;dash&lt;/CODE&gt; on your Ubuntu box?&lt;/P&gt;</description>
      <pubDate>Tue, 30 Aug 2011 17:48:16 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Deployment-Architecture/Scripted-input-without-a-shell/m-p/55665#M1829</guid>
      <dc:creator>Lowell</dc:creator>
      <dc:date>2011-08-30T17:48:16Z</dc:date>
    </item>
    <item>
      <title>Re: Scripted input without a shell?</title>
      <link>https://community.splunk.com/t5/Deployment-Architecture/Scripted-input-without-a-shell/m-p/55666#M1830</link>
      <description>&lt;P&gt;It's running on Ubuntu. Why are you wrapping a python script with a shell script?&lt;/P&gt;</description>
      <pubDate>Tue, 30 Aug 2011 18:24:49 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Deployment-Architecture/Scripted-input-without-a-shell/m-p/55666#M1830</guid>
      <dc:creator>ziegfried</dc:creator>
      <dc:date>2011-08-30T18:24:49Z</dc:date>
    </item>
    <item>
      <title>Re: Scripted input without a shell?</title>
      <link>https://community.splunk.com/t5/Deployment-Architecture/Scripted-input-without-a-shell/m-p/55667#M1831</link>
      <description>&lt;P&gt;And it works on at least OSX as well...&lt;/P&gt;</description>
      <pubDate>Tue, 30 Aug 2011 18:25:17 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Deployment-Architecture/Scripted-input-without-a-shell/m-p/55667#M1831</guid>
      <dc:creator>ziegfried</dc:creator>
      <dc:date>2011-08-30T18:25:17Z</dc:date>
    </item>
    <item>
      <title>Re: Scripted input without a shell?</title>
      <link>https://community.splunk.com/t5/Deployment-Architecture/Scripted-input-without-a-shell/m-p/55668#M1832</link>
      <description>&lt;P&gt;Nope.  Splunk is doing that itself, and I can't make it NOT wrap the process in a shell.  Hence I posted this question.&lt;/P&gt;</description>
      <pubDate>Tue, 30 Aug 2011 18:30:55 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Deployment-Architecture/Scripted-input-without-a-shell/m-p/55668#M1832</guid>
      <dc:creator>Lowell</dc:creator>
      <dc:date>2011-08-30T18:30:55Z</dc:date>
    </item>
    <item>
      <title>Re: Scripted input without a shell?</title>
      <link>https://community.splunk.com/t5/Deployment-Architecture/Scripted-input-without-a-shell/m-p/55669#M1833</link>
      <description>&lt;P&gt;Killing a child process (and all of its children) is not necessarily easy in all cases in unix.  If splunk starts the scripted input in its own process group, then it would be easier to kill all processes within the group.  Similar to gkanapathy's advice, your scripted input could occasionally (top of a loop?) check its parent pid, and exit cleanly if the parent is init (pid 1)&lt;/P&gt;</description>
      <pubDate>Wed, 31 Aug 2011 21:26:23 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Deployment-Architecture/Scripted-input-without-a-shell/m-p/55669#M1833</guid>
      <dc:creator>dwaddle</dc:creator>
      <dc:date>2011-08-31T21:26:23Z</dc:date>
    </item>
    <item>
      <title>Re: Scripted input without a shell?</title>
      <link>https://community.splunk.com/t5/Deployment-Architecture/Scripted-input-without-a-shell/m-p/55670#M1834</link>
      <description>&lt;P&gt;You're right. I didn't see that I've built a rather dirty workaround for Linux. I'll add it to the post, but I think it's something you already had in mind.&lt;/P&gt;</description>
      <pubDate>Tue, 13 Sep 2011 22:00:42 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Deployment-Architecture/Scripted-input-without-a-shell/m-p/55670#M1834</guid>
      <dc:creator>ziegfried</dc:creator>
      <dc:date>2011-09-13T22:00:42Z</dc:date>
    </item>
  </channel>
</rss>

