Issue with Subprocess Execution in Streamlit Environment

I have a Python script that uses the subprocess module to execute shell scripts. When I run the script directly using Python, it works fine and executes the shell scripts as expected. However, when I integrate the script with Streamlit, the shell scripts are not executed properly.

import time
import subprocess

def run_shell_scripts():
    subprocess.run(['bash', 'KtmxSMI_1_Auto_T_B.sh'])
    time.sleep(2)
    subprocess.run(['bash', 'tmxSMI_1_Auto_T_B.sh'])

if __name__ == '__main__':
    run_shell_scripts()

1 Like

Hi @YogKar,

Thanks for sharing this question!

Is the path to the .sh file the full path? I would also check that it is not permission issues that’s blocking the app from executing the shell scripts. If this is not helpful, try debugging the subprocess calls to see if you can capture the output or errors from your shell scripts. Something like this could work (haven’t tested it yet):

def run_shell_scripts():
    result1 = subprocess.run(['bash', 'KtmxSMI_1_Auto_T_B.sh'], 
                             capture_output=True, 
                             text=True)
    st.write("Output:", result1.stdout)
    st.write("Error:", result1.stderr)

Let me know if this is helpful.

1 Like

Thanks for the reply.

Yes, that’s the complete file path. The shell script is located in the same folder. Additionally, it functions properly when executed as a Python script instead of using Streamlit. I’ll attempt the debugging steps you suggested

1 Like

image

This is what I get.

tmux new-session -d -s Auto_T ; send-keys “python3 Auto_T.py” is inside the .sh file.

To add more context, this setup is running on AWS Ubuntu. Interestingly, the individual shell script works well on its own. Even when I run it using crontab, it still functions as expected. Moreover, when I create a separate Python file and execute it using the ‘python’ command, everything works smoothly.

Yes, it indeed creates a new tmux session named Auto_T, but it doesn’t execute the Python file as expected.

1 Like

Thanks for the clairifcation.

1 Like

Thanks for the extra details. I’ll look into this more.

Finally found out. This issue has nothing to do with Streamlit but rather with the nested tmux.
@tonykip Thanks for your efforts.

1 Like

Oh great! Glad you figure it out. Do you mind providing more details on the issue that way if someone runs into this error in future they might find it helpful?

Sure,

I had a Streamlit app running under a tmux session. This Streamlit app initiated another tmux session and attempted to run a Python file within a .sh file. However, tmux encounters an issue with nested sessions and refuses to send keys in such cases. Therefore, I’ve confirmed that the problem does not lie with Streamlit. I’ve identified the source of the issue and am currently searching for a solution

Ref: tmux new-session -d -s Auto_T ; send-keys “python3 Auto_T.py”

2 Likes

Thank you for updating this part.

Happy Streamlit-ing! :balloon:

1 Like

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