Ask coding questions

← Back to all posts
How to make a program that, once closed, opens another window?
willowblanket (1)

So, I'm trying to make a program that, once closed, opens another window of itself, so it never stops unless shut down by task manager or unless the computer itself is shut down, of course. This is just to prank a friend, so no maliciousness. I want to to do this in Python, but if there are any other languages that can help me do this more efficiently, please let me know. I'm a beginning programming student, so I kinda need explanations/answers to be simple. Thanks in advance for your help!

Commentshotnewtop
mwilki7 (262)

Something like that is called a watchdog. If the target program dies, the watchdog restarts it. The only way you can stop the target program is to stop both the target and the watchdog.

For python you could try:
watchdog.py

print("Watchdog start:")
while True: # run forever
    print("Watchdog: process started")  # program started message
    exec(open("./program.py").read())   # run the program
    print("Watchdog: process finished") # program stopped message
print("Watchdog end:") # the watchdog is now finishing

program.py

import time

for i in range (0, 5):
    print(i)
    time.sleep(1)
print("I done")

For C/C++ you could look into forking/exec and poll the system for the process ID and just fork/exec it again if it dies.

KelvinVerhey (33)

@mwilki7 not a good idea to introduce fork bombs etc. servers were taken down with such silly antics.

mwilki7 (262)

@KelvinVerhey only if you write code that fork bombs

rediar (101)

make a windows batch file.

@echo off
shutdown/s

will shut down their computer when the file is run. Please upvote if this was helpful!

ash15khng (391)

@rediar This wouldn't work on Mac and Linux, but I have an alternative:

sudo shutdown -h <time in minutes>

Sadly this needs their password because it doesn't work unless you're the root user or something like that.

rediar (101)

@ash15khng Yeah, thats the problem. It works well for Windows though, obviously.