Python Forum
autostart python scripts in background (Windows10)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
autostart python scripts in background (Windows10)
#1
Hi,
I have problem with autostart python scripts in Windows10:

I made BAT file and put it to autostart folder,
BAT file contens:

@echo off
"C:\Windows\py.exe" "C:\Users\OEM\Desktop\program\main_v4.py"
pause


Python script (main_v4.py) run another scripts:

os.system('python connect_to_hortex_base_palette.py')


but when i rebot win I recive error:
python: can't open file 'connect_to_hortex_base_palette.py': [Errno 2] No such file or directory

_________________________________________________________________________________________________

But when I Run main_v4.py by pressing file by click mouse scripts work well.
as I saw when I click mouse the script, I see c:\windows\py.exe run the script, but when I rebot win I see c:\windows\system32\cmd.exe

My request is:
how i can run script correctly in background without windows(for example cmd window)?

*I know pythonw.exe run scripts in backround but have the same problem as described above.
Reply
#2
For this i would use schedule.
Then just put the python file in startup folder.
file associations to .py your main Python version eg Python 3.7.
If problem with basic setup look here Python 3.6/3.7 and pip installation under Windows.

So to do a test,a little annoying code if try to close notepad it will spawn a new one every 10-sec.
If leave notepad open will do nothing,this make it easy to see that something is running in background at startup.
This dos not start cmd if put in start up folder run silent in background,if running normal in cmd(better cmder) you will see output of print.
# Install
pip install schedule
pip install psutil
# start_notepad.py
import schedule
import time, os
import psutil

def check_process():
    PROCNAME = "notepad.exe"
    for proc in psutil.process_iter():
        if proc.name() == 'notepad.exe':
            return True
    else:
        return False

def check_run():
    if check_process():
        print('Notepad is running')
    else:
        print('Notepad in not running')
        print('Start <notepad.exe>')
        os.startfile("notepad.exe")

schedule.every(.10).minutes.do(check_run)
while True:
    schedule.run_pending()
    time.sleep(1)
Reply
#3
(Sep-30-2019, 03:21 PM)john36 Wrote: os.system('python connect_to_hortex_base_palette.py')
I added C:/Users/OEM/Desktop/program/:
os.system('python C:/Users/OEM/Desktop/program/connect_to_hortex_base_palette.py')
and now it's working but with popup window.

Now I'm still fighting with pythonw.exe (without windows), but now I will try you adwise, thank You
Reply
#4
Do not use os.system(),subprocess has replaced it in safer way a long time ago.

Can force silent always using subprocess.
import subprocess

si = subprocess.STARTUPINFO()
si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
#si.wShowWindow = subprocess.SW_HIDE # default
subprocess.run(['python', 'start_notepad.py'], startupinfo=si)
Reply
#5
(Sep-30-2019, 11:18 PM)snippsat Wrote: Do not use os.system(),subprocess has replaced it in safer way a long time ago.

Can force silent always using subprocess.
import subprocess

si = subprocess.STARTUPINFO()
si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
#si.wShowWindow = subprocess.SW_HIDE # default
subprocess.run(['python', 'start_notepad.py'], startupinfo=si)
It's working, Thank You.
If I can ask, how can I run 5 scripts with this module(subprocess)?
Can I use subrocess without output return from subprocess?
All my python scripts works in infinity loops.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using Autostart to run a GUI program at startup. Rpi Edward_ 1 577 Oct-28-2023, 03:19 PM
Last Post: SpongeB0B
  Trying to us python.exe from our network to run scripts cubangt 3 816 Aug-17-2023, 07:53 PM
Last Post: deanhystad
  Help with autostart homac 1 1,658 May-22-2021, 07:20 AM
Last Post: Gribouillis
  Running python scripts from github etc pacmyc 7 3,609 Mar-03-2021, 10:26 PM
Last Post: pacmyc
  Reading SQL scripts from excel file and run it using python saravanatn 2 2,459 Aug-23-2020, 04:49 PM
Last Post: saravanatn
  No Scripts File present after python installation ag2207 5 4,766 Jul-30-2020, 11:11 AM
Last Post: buran
  win32 API: Launch Application to RDP session from background process python script rangeshgupta 0 2,091 May-28-2020, 09:41 PM
Last Post: rangeshgupta
  How to merge my two python scripts in one ? HK2432 0 2,113 Jan-31-2020, 10:16 PM
Last Post: HK2432
  How can I Open and close .py file from python scripts SayHiii 9 5,622 Dec-17-2019, 06:10 AM
Last Post: Malt
  How to background another process in Python? Kalet 2 2,462 Oct-21-2019, 05:17 AM
Last Post: newbieAuggie2019

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020