Hello. I have come across many scripts for rebooting remote computers using python, but what about the computer the script is running on? If possible, I would also like my script to run in the background so the little cmd box is not visible. Thanks in advance for your help.

Recommended Answers

I think you can have it pass the command to the computer via the os module. Like for Debian-based GNU/Linux distros you could write:

import os
os.system("halt")

The only problem I can think of is that this would require super-user privileges to execute the shut down.

Jump to Post

This mail might help:
http://mail.python.org/pipermail/python-list/2002-August/161778.html

It looks like there are some good example's of it being used in the code, and about adjusting privileges.

EDIT:
It looks like the last argument you'd want to be 1 or True because that's the reboot option...

Jump to Post

All 6 Replies

I think you can have it pass the command to the computer via the os module. Like for Debian-based GNU/Linux distros you could write:

import os
os.system("halt")

The only problem I can think of is that this would require super-user privileges to execute the shut down.

With Windows, if you have the win32api module, you can try this:

import win32api
win32api.InitiateSystemShutdown()

I haven't tried that before though, so you may want to look deeper into that. Hope I helped a bit!

Thanks for your help... it seems that command would work.. except it give me a type error.

import win32api
win32api.InitiateSystemShutdown()
TypeError: InitiateSystemShutdown() takes exactly 5 arguments (0 given)

I will look for the documentation in the meantime.

This post on stackoverflow seems to have the skeleton code for going about and making it a service.

And there's something about using srvany.exe here.

I've never actually done this myself so I can't offer much more insight...

Thanks. I didn't end up doing that exactly, but I greatly appreciate your help nonetheless.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.