I want to run a Python process in background, and I use the following command in PowerShell.
powershell > PowerShell.exe -windowstyle hidden python my_process.py
But, How can I know whether it is running in background? The task manager can not show a process named python my_process.py that running in background, and I don't know the process id on task manager, it just show some python and powershell processes running in background. I can not identify which process is my Python process.
Not actually a programming question, but:
In Task Manager's Process page, choose View > Select Columns and add the Command Line column. Then you can see the actual command line for each process and you should be able to track down the ones you're interested in.
This is for Windows 7; I know they made some changes to the Task Manager for Windows 10 but don't have access to a Windows 10 machine at the moment.
Related
Having trouble with Windows Task Scheduler, and didn't find a solution browsing through other questions. We've got several Python Scripts running via Task Scheduler that are all running just fine, but there's one in particular that just won't run.
The "Run whether use is logged in or not" box is checked, as well as the "Run with highest privileges" box, as it is with the ones that are working.
In the Start Program box I have "C:\Program Files\Python310\python.exe" and in the Add Arguments box I have C:\Users\<user>\<dir1>\<dir2>\script.py.
Using cmd, the following runs the program and generates the file I want perfectly, which is just the exact same things as above.
"C:\Program Files\Python310\python.exe" C:\Users\<user>\<dir1>\<dir2>\script.py
I've tried changing the location of the python interpreter, using a different interpreter version, using task scheduler to run CMD first with the above as arguments, and nothing seems to do it.
On every attempt, I get the following:
Task Started (1)
Action started (1)
Action completed (2)
Task completed (2)
And every time this happens, the script hasn't actually run. I even copy/pasted my the bits in task scheduler into CMD to make sure I didn't overlook a typo or something like that.
Thanks in advance!
Is there any way to run python script on Windows VM continuously. This script should run even computer restarts automatically.
What I am doing right now?
I have a script called FooBar.py, it contains infinite while loop to execute main() function continuously. I am running this script on powershell.
What is problem in this approach?
Sometime this VM restarts automatically or powershell window may close accidentally. This kind of issues causing failure of script execution.
What I have tried so far?
I tried pythonw.exe instead of python.exe to run the script but this does not resolve my problem.
Is there any way to run FooBar.py script continuously, is there any way in windows scheduler to restart script execution even after computer restarts
You can use pm2 to schedule the startup of your script. You can find more information on how to use it here:
https://towardsdatascience.com/automate-your-python-script-with-pm2-463238ea0b65
you can use a module of python named "schedule"
and possibly you can run your code at whatever time you need !
use "pip install schecule" to download the library or module.
for an example i will leave you a pic how to use it.
enter image description here
i mean job in the picture is a function what you want to do.
if you want it to do for a time interval in secdonds then,you can use
schedule.every(#duration#).seconds.do(#declared function#)
thank you!
Create a task in windows scheduler with trigger On a schedule and with trigger option Repeat task every 1 min. Then in a Settings tab there is the dropdown menu If the task is already running, then the following rule applies: where you can choose Do not start a new instance.
I'm on Windows 7. I have a program that launches a Python script using the system Python interpreter. The process quickly finishes. I want a way to see the process, its command line arguments, and any other information about it. (PID?)
How can I do this? The process is killed before I could open Process Explorer. I can cause the program to launch the Python script whenever I want.
I was calling a python script (2.7) from a console (Ubuntu 14.04) using a command: python script_name.py. At some point, I wanted to stop the running script by pressing Ctrl-C. However, when I checked Ubuntu System Monitor, the memory used by the python script was not freed up (I monitored Ubuntu System Monitor before I called the script, during the process, and after I pressed Ctrl-C to stop the script). I tried to free up the memory using a command explained on http://www.upubuntu.com/2013/01/how-to-free-up-unused-memory-in.html , but didn't work (I mean, the memory usage was not changed).
However, if I used pycharm to run and stop the script, the memory was freed up directly once I pressed the Stop button. For some reasons (such as from ssh or just to test from console), I want to run my script from the console (without using pycharm or any other IDEs).
My question is, what is the command, or how to stop running python script and free up directly the memory used by the script, if I run the script from the console?
Many thanks in advance.
Those commands did not work since what you're trying to achieve is not what they do. How did you check the memory being used by your Python script. I use top to see memory and could used by each process (sorted in ascending order by default). You may have checked before the system had time to register that the python process was killed, I've used this a lot and I've never tun into with the OS not getting memory back after a process has been killed with ctrl + c.
Pycharm is probably doing some cleanup when you stop the program from it versus just having to wait for the OS to reclaim memory versys when you SIGTERM a process from a shell
I would like to create a simple Python program that will concurrently execute 2 independent scripts. For now, the two scripts just print a sequence of numbers but my intention is to use this program to concurrently run a few Twitter streaming programs in the future.
I suspect I need to use subprocess.Popen but I cannot quite get my head around what arguments I should put in there. There was a similar question on StackOverflow but the code provided there (pasted below) doesn't print anything. I will appreciate your help.
My files are:
thread1.py
thread2.py
import subprocess
subprocess.Popen(['screen', './thread1.py']))
subprocess.Popen(['screen', './thread2.py'])
Use supervisord
supervisord is process control system just for the purpose of running multiple command line scripts.
It features:
multiple controlled processes
autorestarting failed runs
log stdout and stderr output
starting scripts in order (using priority)
command line utility to view latest log output, stop, start, restart the processes
This solution works only on *nix based systems, it is not available on Windows.
As wanderlust mentioned, why do you want to do it this way and not via linux command line?
Otherwise, the solution you post is doing what it is meant to, i.e, you are doing this at the command line:
screen ./thread1.py
screen ./thread2.py
This will open a screen session and run the program and output within this screen session, such that you will not see the output on your terminal directly. To trouble shoot your output, just execute the scripts without the screen call:
import subprocess
subprocess.Popen(['./thread1.py'])
subprocess.Popen(['./thread2.py'])
Content of thread1.py:
#!/usr/bin/env python
def countToTen():
for i in range(10):
print i
countToTen()
Content of thread2.py:
#!/usr/bin/env python
def countToHundreds():
for i in range(10):
print i*100
countToHundreds()
Then don't forget to do this on the command line:
chmod u+x thread*.py
You can also just open several Command Prompt windows to run several Python programs at once - just run one in each of them:
In each Command Prompt window, go to the correct directory (such as C:/Python27) and then type 'python YourCodeNo1.py' in one Command Prompt window, 'python YourCodeNo2.py' in the next one ect. .
I'm currently running 3 codes at one time in this way, without slowing any of them down.