Linux 如何在启动时运行 shell 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12973777/
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 a shell script at startup
提问by meetamit
On an Amazon S3Linux instance, I have two scripts called start_my_app
and stop_my_app
which start and stop forever(which in turn runs my Node.js application). I use these scripts to manually start and stop my Node.js application. So far so good.
在Amazon S3Linux 实例上,我调用了两个脚本start_my_app
,stop_my_app
它们永远启动和停止(依次运行我的 Node.js 应用程序)。我使用这些脚本手动启动和停止我的 Node.js 应用程序。到现在为止还挺好。
My problem: I also want to set it up such that start_my_app
is run whenever the system boots up. I know that I need to add a file inside init.d
and I know how to symlink it to the proper directory within rc.d
, but I can't figure out what actually needs to go inside the file that I place in init.d
. I'm thinking it should be just one line, like, start_my_app
, but that hasn't been working for me.
我的问题:我还想将它设置start_my_app
为在系统启动时运行。我知道,我需要添加一个文件里面init.d
,我知道如何把它符号链接到内正确的目录rc.d
,但我无法弄清楚什么是真正需要去文件,在我的地方里面init.d
。我认为它应该只是一行,例如, start_my_app
,但这对我不起作用。
采纳答案by Jonathan Muller
In the file you put in /etc/init.d/
you have to set it executable with:
在您放入的文件中,/etc/init.d/
您必须将其设置为可执行文件:
chmod +x /etc/init.d/start_my_app
Thanks to @meetamit, if this does not run you have to create a symlink to /etc/rc.d/
感谢@meetamit,如果这没有运行,你必须创建一个符号链接 /etc/rc.d/
ln -s /etc/init.d/start_my_app /etc/rc.d/
Please note that on latest Debian, this will not work as your script have to be LSB compliant (provide, at least, the following actions: start, stop, restart, force-reload, and status): https://wiki.debian.org/LSBInitScripts
请注意,在最新的 Debian 上,这将不起作用,因为您的脚本必须符合 LSB(至少提供以下操作:启动、停止、重新启动、强制重新加载和状态): https://wiki.debian .org/LSBInitScripts
As a note, you should put the absolute path of your script instead of a relative one, it may solves unexpected issues:
请注意,您应该放置脚本的绝对路径而不是相对路径,它可能会解决意外问题:
/var/myscripts/start_my_app
And don't forget to add on top of that file:
并且不要忘记在该文件的顶部添加:
#!/bin/sh
回答by Gilles Quenot
A simple approach is to add a line in /etc/rc.local
:
一个简单的方法是在 中添加一行/etc/rc.local
:
/PATH/TO/MY_APP &
or if you want to run the command as a special user :
或者如果您想以特殊用户身份运行命令:
su - USER_FOOBAR -c /PATH/TO/MY_APP &
(the trailing ampersand backgrounds the process and allows the rc.local to continue executing)
(尾随和号使进程成为后台并允许 rc.local 继续执行)
If you want a full init script, debian distro have a template file, so :
如果你想要一个完整的初始化脚本,debian 发行版有一个模板文件,所以:
cp /etc/init.d/skeleton /etc/init.d/your_app
and adapt it a bit.
并稍微调整一下。
回答by chris
Another option is to have an @reboot command in your crontab.
另一种选择是在您的 crontab 中有一个 @reboot 命令。
Not every version of cron supports this, but if your instance is based on the Amazon Linux AMI then it will work.
并非每个版本的 cron 都支持这一点,但如果您的实例基于 Amazon Linux AMI,那么它就可以工作。
回答by Saule
This is the way I do it on Red Hat Linuxsystems.
这是我在Red Hat Linux系统上的做法。
Put your script in /etc/init.d
, owned by root and executable. At the top of the script, you can give a directive for chkconfig
. Example, the following script is used to start a Java application as user oracle.
将您的脚本放入/etc/init.d
,由 root 和可执行文件拥有。在脚本的顶部,您可以为chkconfig
. 例如,以下脚本用于以 oracle 用户身份启动 Java 应用程序。
The name of the script is /etc/init.d/apex
脚本的名称是 /etc/init.d/apex
#!/bin/bash
# chkconfig: 345 99 10
# Description: auto start apex listener
#
case "" in
'start')
su - oracle -c "cd /opt/apex ; java -jar apex.war > logs/apex.log 2>logs/apex_error.log &";;
'stop')
echo "put something to shutdown or kill the process here";;
esac
This says that the script must run at levels 3, 4, and 5, and the priority for start/stop is 99 and 10.
这表示脚本必须在 3、4 和 5 级运行,并且启动/停止的优先级为 99 和 10。
Then, as user root
you can use chkconfig
to enable or disable the script at startup:
然后,作为用户,root
您可以chkconfig
在启动时启用或禁用脚本:
chkconfig --list apex
chkconfig --add apex
And you can use service start/stop apex
.
你可以使用service start/stop apex
.
回答by SagarSave
For some people, this will work:
对于某些人来说,这将起作用:
You could simply add the following command into System→ Preferences→ Startup Applications:
您可以简单地将以下命令添加到System→ Preferences→ Startup Applications:
bash /full/path/to/your/script.sh
回答by fieldsweeper
The absolute easiest method if all you want to run is a simple script, (or anything) is if you have a gui to use system > preferences then startup apps.
如果您只想运行一个简单的脚本(或任何东西),最简单的方法是如果您有一个 gui 来使用系统 > 首选项然后启动应用程序。
just browse to the script you want and there you go. (make script executable)
只需浏览到您想要的脚本即可。(使脚本可执行)
回答by Hemant kumar
Set a crontab for this
为此设置一个 crontab
#crontab -e
@reboot /home/user/test.sh
after every startup it will run the test script.
每次启动后,它将运行测试脚本。
回答by IR PRO
You can do it :
你能行的 :
chmod +x PATH_TO_YOUR_SCRIPT/start_my_app
then use this command
然后使用这个命令
update-rc.d start_my_app defaults 100
Please see this page on Cyberciti.
请参阅Cyberciti上的此页面。
回答by Kibrom Gebre
- Add your script to /etc/init.d/ directory
- Update your rc run-levels:
$ update-rc.d myScript.sh defaults NN
where NN is the order in which it should be executed. 99 for example will mean it would be run after 98 and before 100.
- 将您的脚本添加到 /etc/init.d/ 目录
- 更新您的 rc 运行级别:
$ update-rc.d myScript.sh defaults NN
其中 NN 是应该执行的顺序。例如,99 意味着它将在 98 之后和 100 之前运行。
回答by Luciano Ghilarducci
Just have a line added to your crontab..
只需在您的 crontab 中添加一行即可。
Make sure the file is executable:
确保文件是可执行的:
chmod +x /path_to_you_file/your_file
To edit crontab file:
编辑 crontab 文件:
crontab -e
Line you have to add:
您必须添加的行:
@reboot /path_to_you_file/your_file
That simple!
就这么简单!