0

I am running compiled c code on the Ubuntu subsystem for Windows. I would like to automate this process with a Python script that can open the terminal, send and read commands as well as closing the window.

I am trying this on Python 3.6 in Windows 10. I thought it was going to be the same as interfacing Python with the CMD but all the things I try are not yielding results. I want to know if anyone has tried to automate processes with the Ubuntu subsystem for Windows and how can that be achieved. First, I had to find the .exe that launches the Ubuntu app, since it's downloaded from the microsoft store.

Using OS simply gives me the following result without opening the terminal.

import os
os.system(r"C:\Program Files\WindowsApps\CanonicalGroupLimited.UbuntuonWindows_1804.2019.521.0_x64__79rhkp1fndgsc\Ubuntu.exe")

1

Also using subprocess will open the terminal. But parting from there, how can I give it commands and read the outputs?

import subprocess
subprocess.Popen([r"C:\Program Files\WindowsApps\CanonicalGroupLimited.UbuntuonWindows_1804.2019.521.0_x64__79rhkp1fndgsc\Ubuntu.exe"])

1 Answers1

0

I think there's a few things worth mentioning here.

Firstly, I don't think you need to use Python at all. If you want to schedule your compiled C code to run at a particular time (or linked to some other condition etc.), you can do all of this within WSL. I would suggest using cron which already comes with Ubuntu. It's a scheduling daemon that can execute tasks at specified intervals.

Secondly, if you want or need to use Python to achieve this (for whatever reason), I suggest you install a second version of Python within WSL. This will allow you to easily run whatever shell commands you want e.g. to execute your compiled C code. You don't need to worry about opening an Ubuntu window and then closing it etc. Personally I would actually go further and say that you should ditch your Windows 10 Python and just go with the WSL Python - it makes a lot of things much easier, especially if you're already familiar with Linux or are using WSL a lot for other tasks anyway.

Lastly, if you still really need to automate this from Windows 10, I suggest you use the Windows Task Scheduler to schedule a .bat file which opens bash (WSL) and executes a command as per the following link:

How to run program on ( ubuntu bash windows 10 ) from windows task scheduler

(or if you don't need to schedule it, you can instead just execute the command from Python)

Hope that helps!

amin_nejad
  • 989
  • 10
  • 22