2

So I am currently working remotely and trying to have a Python script run on an Ubuntu machine while not maintaining the ssh connection to it. The usual solution is a detached screen that allows to let the script run while breaking the connection pipe.

However, my script calls another program (LTspice) which happens to run only on Windows but was installed on the Ubuntu machine using Wine.

I am using the subprocess library in python to perform this call. (wine LTspice.exe)

Although the script works perfectly when ssh-connected, as soon as the pipe is broken, the script fails to process for an unknown reason, but as soon as I reconnect and reattach, the script works again. The issue might come from different aspects as it involves ssh, screen, wine, python subprocess and LTspice but i am trying to investigate and solve this issue. (I am from a physics background so please cope with my shallow knowledge on those aspects, I want to learn).

In order to have a simple test on the issue that is a part of my main script, I created this small other test script:

Test.py:

from subprocess import call
import subprocess
import os
import sys
import config2 as config # a config file that includes the paths in a Linux format

file_path = config.tmp_asc_file[:-4]
spice_exe_path = config.LTSpice_executable_path
pathC='C:/'+file_path.split('/',5)[-1] #this adapt the path to windows format that LTspice reads 
                                       #inside the wine environment

while True:
    command="wine "+ spice_exe_path+' -wine -netlist "'+ pathC+ '.asc"'
    print(repr(command))
    #call(["bash","-c",command])
    #subprocess.run(['wine',spice_exe_path,'-wine','-netlist',pathC+'.asc' ],check=True)
    subprocess.run(['bash','-c', command],check=True)

    command='wine '+ spice_exe_path+' -wine -b'+' -ascii "'+ pathC+'.net"'
    print(repr(command))
    #call(["bash","-c",command])
    #subprocess.run(['wine',spice_exe_path,'-wine','-b','-ascii',pathC+'.net' ],check=True)
    subprocess.run(['bash','-c', command],check=True)

    try:
        os.remove(file_path+'.net')
    except:
        print("file does not exist")
    try:
        os.remove(file_path+'.log')
    except:
        print("file does not exist")
    try:
        os.remove(file_path+'.op.raw')
    except:
        print("file does not exist")
    try:
        os.remove(file_path+'.raw')
    except:
        print("file does not exist")

This script was created following my understanding of the advice found in Running Bash commands in Python. I just run this (python test.py) in a linux screen. Files are processed by LTspice and the output files created can thus be removed. Detaching and re attaching does not make the script fail. But detaching, breaking the ssh connection (simple exit after detaching) then connecting again to the remote machine and re attaching shows the "file does not exist" prints in previous output lines while most recent shows the script is working again.

Could this issue be due to screen and subprocess interaction? Or a wine/screen? Or maybe just LTspice issues working with wine? I am eager for any feedback on methods to investigate and test the cause(s) of my issue, or bypass it as long as the solution allows me to not stay connected to the remote machine.

Thanks in advance.

flogoru
  • 31
  • 4
  • You should probably drink the wine before working on the subprocess. – Trenton McKinney Jul 01 '20 at 02:18
  • 1
    Several bottles of wine may convince to deal with wine again. Maybe. One thing you should change are your `try except`. As written, anything could be happening and you’d assume file errors. `try xxx except IOError as e: print(e)` is more explicit. Never catch a bare except without raising it. The other thing: could you not chain yet another subprocess to run your exe? Isolating it from ssh considerations? If that fails it’s less exe-related. – JL Peyret Jul 01 '20 at 05:57
  • Thanks for this feedback on the try. Could you explain what you mean by your chain sentence? do you want me to remove one of the subprocess call? How can i isolate from ssh? – flogoru Jul 01 '20 at 14:27
  • 1
    If I understand correctly ssh - python - subprocess - bash - exe. First, not sure what bash contributes. Try `subprocess.run(command.split(),check=True) `. 2nd what if you ssh - python1 - subprocess - python -subprocess- bash - exe? Where python1 is just a minimal python script that subprocesses your current script. Pointless normally, but it might buffer your exe’s launch context from ssh changes. Then again it might not. – JL Peyret Jul 01 '20 at 16:13
  • 1
    Also besides trying wo bash, another you could do is running a Linux native utility (not a bash builtin) from your script. Not sure what a good pick is : `grep xxx *` might be but a) short run b) the `*` needs a shell to expand it c) grep can return error codes when it doesn't find stuff. You need something that runs long enough to disconnect ssh, no shell needed and doesnt complain under your run conditions. Run that. If it works with your current setup, then you know wine is likely at fault. If it errors like your exe, you know it’s either your env/ssh or your subprocess usage. – JL Peyret Jul 01 '20 at 16:30
  • Update: I tried to run the "wine "+ spice_exe_path+' -wine -netlist "'+ pathC+ '.asc"' command in loop, detect if file is created (then delete it) else raise an error, directly with a bash script (no python). I could not log out of the ssh connection even after detaching the screen. So I guess it is due to wine/LTspice and ssh interaction. – flogoru Jul 02 '20 at 20:43

0 Answers0