I have a python app that sends a notification whenever a specified YouTube channel gains a subscriber. I would like to have this run in the background instead of in terminal. Is there a way to do this? Here are my libaries if they could cause an issue:
import os
import urllib.request
import json
import time
from sys import platform
from plyer import notification
import subprocess
You can run your Python app as a service.
If you are using Windows you need some libraries such as:
win32serviceutil
win32service
win32event
servicemanager
In case of your Operating System is Windows, you can use .pyw file extension and then run the app, it will run in the background, you could only stop it using the Task Manager.
On Linux, the easiest solution is to add & at the end of the command.
I. e: python3 file.py &.
But it will not be started every time you start up the computer, you can use tools such as systemd for example, to automatically run it every time.
Related
I’m new to Python and I have written a few python selenium tests. In my main file I have used the main.py to run all my selenium tests by using the import os module. I can execute the main.py through pycharm and python interpretar successfully but I can’t get it to work when trying to set up a schedule in Windows Tasks scheduler.
This is the error I’m getting
“The system cannot execute the specified program”
Any help would be appreciated. Thanks
In my main.py file I have
Import os
os.system (“python C:/Users/………filename.py”)
os.system (“python C:/Users/………filename2.py”)
I'm new to Azure automation, and have a python script that runs fine on the local machine. The script runs a PowerShell command to get sign in logs, and does some string parsing on them before writing to a storage account table. I was able to install all dependencies for the script via the "Python Packages" blade in the automation account. Is there a way to add powershell.exe somewhere so that the subprocess library can be used to run the PowerShell command? Alternatively, is there a library you can import to be able to run the PowerShell command or grab sign in logs from Azure AD without directly using the powershell.exe file?
Also, Microsoft.Powershell.Core is added in the Modules within the automation account. Am I just using the wrong filename and should use something other than powershell.exe? Is there a different module you have to install, or am I referencing it wrong?
Here is the relevant portion of the script:
import subprocess
import sys
import json
import azure.core
from azure.core.credentials import AzureNamedKeyCredential
from azure.data.tables import TableServiceClient, TableClient
data = subprocess.check_output(["powershell.exe", "Connect-AzureAD -AccountId placeholder#placeholder.com \n Get-AzureADAuditSignInLogs"]).decode(sys.stdout.encoding)
This throws a "FileNotFound" error for powershell.exe. In the context of an Azure Automation runbook, how can you run powershell commands with the subprocess library, then? Any ideas are much appreciated. Many thanks
I have tested in my environment
I get the same error as yours if I use subprocess in my python script to run PowerShell command
This is because the subprocess module is not able to find the powershell.exe path
For the workaround, you can use either of the options:
You can use PowerShell script instead of python script
You can use MS Graph API in your python code instead of PowerShell commands
I try this code in Linux:
import os
import signal
for i in range(10000):
print i
if i==6666:
os.kill(os.getpid(),signal.SIGINT)
it works well. But it doesn't work in Windows, because the attribute 'kill' is not present in os module for Windows
How can I send SIGINT to self program in Windows?
from win32api import GenerateConsoleCtrlEvent
GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0)
Is there a way to lock the PC from a Python script on Windows?
I do not want to implement some kind of locking on my own - I'd like to use the same lock screen that's also used when the user presses WIN+L or locks the machine via the start menu.
This can be done with the LockWorkStation() function from user32.dll:
This function has the same result as pressing Ctrl+Alt+Del and clicking Lock Workstation.
In Python it can be called using using the ctypes/windll FFI from the Python stdlib:
import ctypes
ctypes.windll.user32.LockWorkStation()
A good solution that makes us avoid using Libraries/DLL files is to use the command prompet/ power shell.
try running this command in your cmd rundll32.exe user32.dll, LockWorkStation....The PC is Locked!!
so we can use subprocess to run this command like this:
import subprocess
cmd='rundll32.exe user32.dll, LockWorkStation'
subprocess.call(cmd)
One more solution :
Run command to install the necessary package
pip install pyautogui
Run the below code
import pyautogui
from time import sleep
pyautogui.hotkey('win', 'r')
pyautogui.typewrite("cmd\n")
sleep(0.500)
pyautogui.typewrite("rundll32.exe user32.dll, LockWorkStation\n")
I'm running several portable apps from my python app.
Consider the following code:
import win32com.shell.shell as w32shell
import os
import sys
import win32process as process
PORTABLE_APP_LOCATION = "C:\\Windows\\System32\\calc.exe"
#This function runs a portable application:
def runPortable():
try:
startObj = process.STARTUPINFO()
process.CreateProcess(PORTABLE_APP_LOCATION,None,None,None,8,8,None,None,startObj)
# OR
#w32shell.ShellExecuteEx(lpFile=PORTABLE_APP_LOCATION)
except:
print(sys.exc_info()[0])
runPortable()
1) Should I expect any differences in the execution of this code from pythonw or python ?
2) If I change PORTABLE_APP_LOCATION to the path to a portable version of CDBurnerXP and use the ShellExecuteEx option instead of CreateProcess, I see the process is started on Windows Task Manager but the actual window of the app isn't visible. This doesn't happen with other EXEs such as a portable version of GIMP that do show up after being ran. I assume this difference comes from a property of the executables. Anybody knows what's causing this?
3) Under what terms does Windows prompts the "Are you sure you want to run this EXE"? I believe CDBurnerXP is signed with a certificate but still sometimes Windows pops this question when trying to run this EXE from within python.
Thanks a lot.
About Your first question , you should pay attention that when executing python code using pythonw.exe runtime , your sys.stdout buffer is limited to 4096 Bytes and when overflowed will throw an IOError wich you will not see because the code is running windowless .
I am a newbie in this field. May be this can help you
use subprocess.call, more info here:
import subprocess
subprocess.call(["C:\\temp\\calc.exe"])
or
import os
os.system('"C:/Windows/System32/notepad.exe"')
i hope it helps you...