创建在启动时运行的python脚本 [英] Create python script that runs at startup

查看:180
本文介绍了创建在启动时运行的python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

我需要创建一个在启动时运行的python脚本。问题在于该脚本必须独立于平台,因为它将在不同的操作系统上使用。它必须是自动设置的,因为它将由用户运行,因此我将无法在每台机器上设置任务计划程序。

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.

问题


  1. 如何找出计算机在Python上运行的操作系统?

  2. 如何使脚本在启动时运行(Linux,Mac OSX,Windows)


推荐答案

问题1很简单:


如何找出计算机在Python上运行的操作系统?

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

这是 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:


如何在启动时运行脚本(Linux,Mac OSX, Windows)

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

您没有。不在脚本内。您使用某种安装程序(或软件包postflight脚本,或其他)。

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

添加在启动时运行的内容需要root / admin权限。您的脚本(希望)没有以这种权限运行。因此,它无法做到。是的,可以通过各种方式提升特权,但这几乎可以肯定不是您想要在启动时最终运行的脚本中执行的操作。

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:您需要创建一个启动守护程序以及一个附带的启动plist。 创建启动守护程序和代理。如果您尚未阅读该文章,则不应该尝试这样做,并且您已经知道如何阅读该文章,因此没有什么其他要说的。

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:运行和RunOnce注册表项。同样,您不应该不阅读这篇文章就这样做,阅读完这篇文章后,除了两点以外,它很明显:首先,在四个键中,是HKLM Run键。其次,在现代Windows中,它实际上不是在启动时运行的,而是在启动后的首次登录时运行的;如果不可接受,请改用 RunServices

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:什么是安装程序?您是否期望为每个发行家庭提供一种方法? 本入门为您提供所需的大多数信息,除了确切地知道您要在每个发行版上做什么之外。通常,如果您只想让脚本运行一次并退出,并且有一个 rc.local.d ,则只需在其中放置一个链接。否则,您需要创建一个 rc.d 脚本,将其安装到正确的位置,然后运行正确的 chkconfig 命令,或者您需要编辑 rc.local 来运行脚本。但是最简单的事情是:只需将英文文本放入INSTALL文件中,告诉人们这样做。最终,当有人决定为Ubuntu制作DEB或为Redhat制作RPM时,他们会为自己的发行版做正确的事,然后向您提交补丁或单独维护它。

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.

这篇关于创建在启动时运行的python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆