Linux 如何在 Fedora 14 机器启动时运行脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5573592/
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 to run script on startup of a Fedora 14 machine
提问by Alex
I have an AWS AMI with Fedora 14 installed. I do not have root access but I can do pretty much anything using sudo
. I have a script that I want to run on machine startup - myScript.sh
.
我有一个安装了 Fedora 14 的 AWS AMI。我没有 root 访问权限,但我几乎可以使用sudo
. 我有一个要在机器启动时运行的脚本 - myScript.sh
.
This script is called by calling sudo /dir/dir/myScript.sh start
. It also supports the commands stop
and status
which I use very often.
该脚本通过调用sudo /dir/dir/myScript.sh start
. 它也支持命令stop
和status
我经常使用。
What is the ideal way to run such a script at startup?
在启动时运行此类脚本的理想方式是什么?
采纳答案by sarnold
You have a few mechanisms easily available:
您可以轻松使用一些机制:
The two most common mechanisms:
两种最常见的机制:
Adding an upstart job definitionfile. This looks easy, but does limit the distributions that can use your provided script to a handful of newer ones (Ubuntu, Google's Chrome distribution, Fedora, probably RHEL?).
添加新贵作业定义文件。这看起来很简单,但确实将可以使用您提供的脚本的发行版限制为少数较新的发行版(Ubuntu、Google 的 Chrome 发行版、Fedora,可能是 RHEL?)。
Adding a sysv-init initscript. init-scripts are harder to write, but have much more flexibility in providing more command line arguments for sysadmins to use. They are also supported nearly everywhere, even distros which have switched (some or most) of their initscripts over to Upstart jobs.
添加sysv-init initscript。init-scripts 更难编写,但在为系统管理员提供更多命令行参数方面具有更大的灵活性。它们也几乎在任何地方都得到支持,甚至是已经将(部分或大部分)initscripts 切换到 Upstart 作业的发行版。
I don't see it often, it is more a system-administrator choice than a software developer choice, but you can also use the system Vixie cron(8)
facility to start programs at boot.
我不经常看到它,它更像是系统管理员的选择而不是软件开发人员的选择,但您也可以使用系统 Vixiecron(8)
工具在引导时启动程序。
You can add entries to the system crontab in /etc/cron.d/*
; Vixie cron supports a @reboot
specifier which is excellent for this task.
您可以在系统 crontab 中添加条目/etc/cron.d/*
;Vixie cron 支持@reboot
非常适合此任务的说明符。
Or, you can add an entry to your own personal crontab(5)
file; again, Vixie cron's @reboot
works for users too, but you cannot run an interactivesudo(8)
from a crontab(5)
. See the NOPASSWD
documentation in sudoers(5)
if you want to use your own personal crontab(5)
file, but be warned the other admins on the machine may hate this idea.
或者,您可以在自己的个人crontab(5)
文件中添加一个条目;再次,vixie cron在的@reboot
作品,为用户太多,但你不能运行的互动sudo(8)
从crontab(5)
。如果您想使用自己的个人文件,请参阅NOPASSWD
文档,但请注意机器上的其他管理员可能讨厌这个想法。sudoers(5)
crontab(5)
回答by Owen Fraser-Green
I think the simplest and most universal method (although less flexible than @sarnold's suggestions) is to add /dir/dir/myScript.sh start
to /etc/rc.local. The script will then be run on boot after all the other start-up scripts have run.
我认为最简单和最通用的方法(虽然不如@sarnold 的建议灵活)是添加/dir/dir/myScript.sh start
到 /etc/rc.local。在所有其他启动脚本运行后,该脚本将在启动时运行。