Linux 如何在每次启动时运行我自己的脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9871997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how can I run my own script at every bootup
提问by Prak
I have a question, how can I run my own bash script at every startup time in Ubuntu. Suppose I have a script which is doing a particular kind of work. Now I want it to run automatically at the time of starting my Ubuntu system.
我有一个问题,如何在 Ubuntu 的每次启动时运行我自己的 bash 脚本。假设我有一个正在执行特定类型工作的脚本。现在我希望它在启动我的 Ubuntu 系统时自动运行。
回答by hovanessyan
Currently Linux systems (including Ubuntu) support 2 ways of achieving this: Upstart and SysV scripts. Upstart is the "new" way.
目前 Linux 系统(包括 Ubuntu)支持两种实现方式:Upstart 和 SysV 脚本。新贵是“新”的方式。
Generating SysV scripts can be achieved like so:
生成 SysV 脚本可以这样实现:
update-rc.d <your script> defaults
This will make links to start the service in runlevels 2345 and to stop the service in runlevels 016 and will create the appropriate SysV-style scripts inside /etc/rc?.d/
这将创建在运行级别 2345 中启动服务并在运行级别 016 中停止服务的链接,并将在/etc/rc?.d/ 中创建适当的 SysV 样式脚本
The other way would be to write an upstart-job. Upstart jobsare located under /etc/init. The easiest way is to copy an existing job and try to modify it for your script. Here's the upstart stanzasexplained.
另一种方法是写一份新贵的工作。新贵工作位于/etc/init 下。最简单的方法是复制现有作业并尝试为您的脚本修改它。这是新贵节的解释。
回答by Diego Woitasen
There are two options. The easy one, edit /etc/rc.local and call your script from there. The other option is to use upstart. Hace a look at /etc/init/hostname.conf. You can use that file as template, copy it as /etc/init/yourscript.conf, adapt the content and it should work.
有两种选择。最简单的方法是编辑 /etc/rc.local 并从那里调用您的脚本。另一种选择是使用新贵。看看/etc/init/hostname.conf。您可以使用该文件作为模板,将其复制为 /etc/init/yourscript.conf,调整内容,它应该可以工作。
回答by Schien
I have recently run into a situation where one job is ideally started with upstart, and the other one rc.local. Although both methods will get your script executed, upstart makes more sense when daemonizing a script at start time; rc.local, on the other hand, lets the script run its course.
我最近遇到了一种情况,理想情况下,一份工作是从 upstart 开始的,而另一份工作是 rc.local。虽然这两种方法都会让你的脚本被执行,但 upstart 在启动时守护脚本时更有意义;另一方面,rc.local 让脚本运行它的进程。
For example, if the script is already a daemon process, e.g. a server, it can be invoked by rc.local, as it doesn't stay in the foreground and block the terminal.
例如,如果脚本已经是一个守护进程,例如一个服务器,它可以被 rc.local 调用,因为它不会停留在前台并阻塞终端。
But if the script itself is not daemonized, upstart will give it the ability to gracefully start and stop, upon system events. This is good for running a non-exiting PHP script in the background. Although you could use upstart to run, say, Apache httpd, the process "exits" right away in the eyes of Upstart, which makes Upstart pointless, as it is already "terminated".
但是如果脚本本身没有被守护进程,upstart 将赋予它在系统事件时正常启动和停止的能力。这对于在后台运行非退出 PHP 脚本很有用。虽然你可以使用 upstart 来运行,比如 Apache httpd,但在 Upstart 的眼中,这个进程会立即“退出”,这使得 Upstart 毫无意义,因为它已经“终止”了。