0

Background

I need to create a python script that runs at start-up. The problem is that this script must be platform independent because it will be used on different operating systems. It needs to be an automatic set up because it will be run by the user and so I won't be able to set task schedulers up on each individual machine.

Questions

  1. How to find out which OS a computer is running on in Python?
  2. How to make a script run at startup (Linux, Mac OSX, Windows)
xxmbabanexx
  • 8,256
  • 16
  • 40
  • 60
taylor
  • 49
  • 4
  • 4
    So what's your question? – wRAR Feb 22 '13 at 00:49
  • [This post](http://nedbatchelder.com/blog/201001/running_code_at_python_startup.html) might suite your need. – Octipi Feb 22 '13 at 00:53
  • @WinRAR I just added what I think might be the actual question. Hopefully the edit is approved. – xxmbabanexx Feb 22 '13 at 00:55
  • have you tried anything? its so easy to google – Drewdin Feb 22 '13 at 01:06
  • 2
    Not really fair to down-vote this as it seems to be asked in good faith... – BenDundee Feb 22 '13 at 01:09
  • I have searched google but all I could find was platform specific stuff. – taylor Feb 22 '13 at 04:22
  • @taylor: You pretty much need to use the platform-specific stuff for each platform you care about. It's not just that nobody's bothered to wrap everything up nicely, it's that the issues you need to deal with are very different between Windows and Unix, and pretty different even between different Unixes (especially OS X vs. everything else). You can, of course, write your own wrapper that works for your specific use case, which will be a lot simpler than a wrapper that's completely general. – abarnert Feb 22 '13 at 19:24
  • Thanks, I just didn't know if python had inbuilt functions to run scripts at startup or not. – taylor Feb 25 '13 at 00:20

1 Answers1

6

Question 1 is easy:

How to find out which OS a computer is running on in Python?

That's sys.platform:

if sys.platform.startswith('win') or sys.platform.startswith('cygwin'):
    do_windows_stuff()
elif sys.platform.startswith('darwin'):
    do_osx_stuff()
elif sys.platform.startswith('linux'):
    do_linux_stuff()
else:
    raise Exception("Nobody's written the stuff for {}, sorry".format(sys.platform))

The second part is also easy, but not in the way you wanted to hear:

How to make a script run at startup (Linux, Mac OSX, Windows)

You don't. Not from within the script. You use some kind of installer (or package postflight script, or whatever).

Adding things that run at startup requires root/admin rights. Your script (hopefully) is not running with such rights. Therefore, it can't do it. Yes, it's possible to elevate privileges in various ways, but that almost certainly isn't what you want to do inside a script that's going to end up running at startup.

So, how does your installer do it then?

OS X: You need to create a Launch Daemon, with an accompanying launchd plist. This is described in Creating Launch Daemons and Agents. You shouldn't be trying to do this if you haven't read that article, and you'll already know how if you have read that article, so there's not much else to say.

Windows: The official way to do this is explained in Run and RunOnce Registry Keys. Again, you shouldn't do this without reading this article, and after reading the article it's pretty obvious, except for two things: First, out of the four keys, it's the HKLM Run key. Second, in modern Windows, this doesn't actually run at startup, but at the first login after startup; if that's not acceptable, look into RunServices instead.

Linux: What's an installer? And were you expecting one way to do it for every distro family? This primer gives you most of the information you need, except for knowing exactly what you want to do on each distro. In general, if you just want your script to run once and quit, and there's an rc.local.d, and you just need to drop a link in there. Otherwise, you either need to create an rc.d script, install it to the right place, and run the right chkconfig command, or you need to edit rc.local to run your script. But the simplest thing is: just put English text in the INSTALL file telling people to do it. Eventually, when someone decides to make a DEB for Ubuntu or an RPM for Redhat or whatever, they'll do the right thing for their distro, and either submit a patch to you or maintain it separately.

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • 1
    Ok so it's not going to be as easy as I thought, but at least I know what direction to look at. Thank you – taylor Feb 22 '13 at 05:20