Splunk Dev

Shell script won't execute a Python script.

jamaalsd
New Member

I am totally confused after many hours of trying to figure this out and I couldn't find any other posts similar enough to my problem. Here's my problem:

I want an alert to call a shell script (myShellScript.sh)

I then want myShellScript.sh to execute a Python script (myPythonScript.py) in the same directory.

myShellScript.sh looks like:

#!/bin/bash

./myPythonScript.py

myPythonScript.py looks like:

#!/usr/bin/env python

::python code here::

If I execute the shell script from the terminal it works. If I put in a shell script that doesn't try to run an executable into a Splunk alert message it works, but a sheel script that attempts to execute a command is a no-go. None of the logs (splunkd.log, scheduler.log, python.log) list any problems, the python executable has the proper permissions. Anybody know what's going on?

Labels (1)
0 Karma

murphybeck
New Member

You should do it the other way round, run python script inside screen:

screen -dm bash -c 'script -c "python test.py" output.txt'
0 Karma

abnev
New Member

When attempting to call a python script from a script, don't attempt to execute it directly using ./, instead call the interrupter to execute it for you. Something like this should suffice:

#!/bin/bash

SCRIPT_PATH="myPythonscript.py" 
PYTHON="/usr/bin/python"

# call script via the interrupter     
$PYTHON $SCRIPT_PATH

Also make sure that the permissions are right on both scripts (chmod +x) etc.

0 Karma

Ayn
Legend

You're assuming that the script's working directory is the path where it resides - however that's not the case. Scripts execute in the root path "/", so when you try to run "./myPythonScript.py" in the script that's equivalent to attempting to run "/myPythonScript.py".

If you reference your Python script using its full path, or for that matter change your working directory at the start of the shell script, your script will work.

Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Announcing Modern Navigation: A New Era of Splunk User Experience

We are excited to introduce the Modern Navigation feature in the Splunk Platform, available to both cloud and ...

Modernize your Splunk Apps – Introducing Python 3.13 in Splunk

We are excited to announce that the upcoming releases of Splunk Enterprise 10.2.x and Splunk Cloud Platform ...

Step into “Hunt the Insider: An Splunk ES Premier Mystery” to catch a cybercriminal ...

After a whole week of being on call, you fell asleep on your keyboard, and you hit a sequence of buttons that ...