Roni Warken
Roni Warken

Reputation: 26

Rebooting a server after Windows application is installed using Python

I am developing some Python (version 3.6.1) code to install an application in Windows 7. The code used is this:

winCMD = r'"C:\PowerBuild\setup.exe" /v"/qr /l C:\PowerBuild\TUmsi.log"'
output = subprocess.check_call(winCMD, shell = True)

The application is installed successfully. The problem is that it always requires a reboot after it is finished (a popup with a message "You must restart your system for the configuration changes made to to take effect. Click Yes to restart now or No if you plan to restart later.).

I tried to insert parameter "/forcerestart" (source here) in the installation command but it still stops to request the reboot:

def installApp():
    winCMD = r'"C:\PowerBuild\setup.exe" /v"/qr /forcerestart /l C:\PowerBuild\TUmsi.log"'
    output = subprocess.check_call(winCMD, shell = True)

Another attempt was to create a following command like this one below, although since the previous command is not finished yet (as per my understanding) I realized it will never be called:

rebootSystem = 'shutdown -t 0 /r /f'
subprocess.Popen(rebootSystem, stdout=subprocess.PIPE, shell=True)

Does anyone had such an issue and could solve it?

Upvotes: 1

Views: 187

Answers (1)

Jean-François Fabre
Jean-François Fabre

Reputation: 140276

As an ugly workaround, if you're not time-critical but you want to emphasise the "automatic" aspect, why not

  • run the installCMD in a thread
  • wait sufficiently long to be sure that the command has completed
  • perform the shutdown

like this:

import threading,time
def installApp():
    winCMD = r'"C:\PowerBuild\setup.exe" /v"/qr /l C:\PowerBuild\TUmsi.log"'
    output = subprocess.check_call(winCMD, shell = True)

t = threading.Thread(target=installApp)
t.start()
time.sleep(1800)  # half-hour should be enough
rebootSystem = 'shutdown -t 0 /r /f'
subprocess.Popen(rebootSystem, stdout=subprocess.PIPE, shell=True)

Another (safer) way would be to find out which file is created last in the installation, and monitor for its existence in a loop like this:

while not os.path.isfile("somefile"):
     time.sleep(60)
time.sleep(60)  # another minute for safety
# perform the reboot

To be clean, you'd have to use subprocess.Popen for the installation process, export it as global and call terminate() on it in the main process, but since you're calling a shutdown that's not necessary.

(to be clean, we wouldn't have to do that hack in the first place)

Upvotes: 2

Related Questions