Determine what script/module a python instance is executing? - python

I'm writing a program to monitor a python script running. There will be multiple instances of this script, which is a server, running from different locations on the computer. My program will be monitoring to see that all instances that are being "watched" are up and running and will restart them as necessary.
The server script cannot be edited.
My problem is that the server process just shows up as the python executable, and I am unable to determine the location of the specific server script on the computer.
Is there anyway to determine what script is actually running on a specific python/pythonw.exe process? And also its path?
ENV: Windows only

You can run the following command from the command line:
WMIC PROCESS get Caption,Commandline,Processid,ExecutablePath
This will show you which module is being executed.

Related

Embedded linux start python from crontab with terminal access and subprocess permissions

I have an embedded linux system that I need to run a python script whenever it boots. The python script needs to have a terminal interface so the user can interact and see outputs. The script also spawns another process to transfer large amounts of data over SPI, this was written in C.
I've managed to get the script to start on launch and have terminal access by adding
#reboot /usr/bin/screen -d -m python3 /scripts/my_script.py
to the crontab. I can then do "screen -r" and interact with the script. However if launched in this way the script fails to start the external SPI script. In python I launch the script with subprocess.Popen
proc=subprocess.Popen(["./spi_newpins,"-o","/media/SD/"+ latest_file"])
and this works perfectly whenever I manually launch the script, even within screen. Just not when it is launched by crontab. Does anyone have any ideas on how to get the spi subprocess to also work from crontab?
Fixed now, I had to add an absolute path to the spi_newpins function call
proc=subprocess.Popen(["/scripts/./spi_newpins","-o","/media/SD/"+ latest_file"])

Can't run Python Script on remote VM via SSH

I have an Azure VM into which I SSH in. The VM is a W10 host.
I can create files with touch, change directories and so on, but whenever I try to run a python script that is hosted on the VM I get the following error:
The system cannot execute the specified program.
At first glance I thought that there was a problem related to my pyhton alias and the PATH variable, so I decided to use RDP to log into the machine, open a CMD and try the same command, which worked just fine. The python program executed flawlessly.
I used where to find where is my python.exe located at, so whenever I run the script on my remote terminal I can do something like:
C:\Users\User01\AppData\Local\Microsoft\WindowsApps\python.exe test.py
This does result in the same error message as the one stated above.
Can I get some help?
Hi try to execute your command as below.
ssh user#machine python < script.py

Console window closes abruptly while using windows task schedular to run Python script

I have written some Python code to automate a task. Then I scheduled the Python script to execute twice a week using windows task scheduler. The task runs but there are two issues.
The console prints some error. It is strange because when I run the script manually after opening command prompt everything works as expected. This is what I do manually.
cd directory
directory>script.py
Inside the task scheduler I have directly specified the location of the script to run as C:\full\path\to\script.py
Second issue is that I can't even diagnose the error because the console closes as soon as it executes the script. This is despite the fact that I have added a dummy input command at the end of script like this:
All my Python code
input('Press any key to exit')
I also tried to keep the console open by using time.sleep(60) after importing the time module.
Can anyone tell me how can I keep the console open. Let me repeat that I am running the script using windows task scheduler. Running the script manually generates no error.
Thanks.

How do I run a python script using an already running blender?

Normally, I would use "blender -P script.py" to run a python script. In this case, a new blender process is started to execute the script. What I am trying to do now is to run a script using a blender process that is already running, instead of starting a new one.
I have not seen any source on this issue so far, which makes me concern about the actual feasibility of this approach.
Any help would be appreciated.
Blender isn't designed to be started from the cli and to then keep receiving more commands from the cli as it is running. It does however include a text editor that can open text files and run the text block as a python script, it also includes a python console that can be used to interactively type in commands while blender is running. You may also find this addon useful as it lets you to run a text block in the python console, this leaves you with an interactive session that contains the variables as they exist at the end of the scripts execution.
There is a cli option to run blender as a python console blender --python-console - the gui does not get updated while this console is running, so you could open and exec several scripts and then when you exit the console, blender will update it's gui and allow interactive use, or if you start in background mode -b then it will quit when you exit the console.
My solution was to launch Blender via console with a python script (blender --python script.py) that contains a while loop and creates a server socket to receive requests to process some specific code. The loop will prevent blender from opening the GUI, and the socket will handle the multiple requests inside the same blender process.

How do I write a python program to get my Linux machine to restart at a signal?

I have one main program "main.py" that may freeze occasionally. Whenever I detect this happens, I want to have a separate program "watch.py" get my Linux machine to restart. These scripts start at bootup automatically since I edited /etc/rc.local.
Right now /etc/rc.local looks like this -
python watch.py &
python main.py &
This should let both programs run simultaneously. When I notice main.py has frozen, I'll give a signal manually to watch.py (using a remote TCP connection) to restart. What should my python code or shell script be to actually restart the system when "watch.py" receives a signal?
Possible answers could be writing some python code to restart a Linux machine, writing some python code to exit with a certain argument, and upon noticing the argument, execute "sudo reboot".

Categories