如何在启动时运行Shell脚本 [英] How to run a shell script at startup
问题描述
在 Amazon S3 Linux实例上,我有两个名为start_my_app
和
On an Amazon S3 Linux 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.
我的问题:我还想进行设置,以便在系统启动时运行start_my_app
.我知道我需要在init.d
中添加一个文件,并且知道如何将其符号链接到rc.d
中的正确目录,但是我无法弄清楚在init.d
中放置的文件中实际需要什么. >.我认为它应该只是一行,例如start_my_app
,但这对我没有用.
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.
推荐答案
在您放入/etc/init.d/
的文件中,您必须使用以下命令将其设置为可执行文件:
In the file you put in /etc/init.d/
you have to set it executable with:
chmod +x /etc/init.d/start_my_app
由于@meetamit,如果此命令无法运行,则必须创建指向/etc/rc.d/
Thanks to @meetamit, if this does not run you have to create a symlink to /etc/rc.d/
ln -s /etc/init.d/start_my_app /etc/rc.d/
请注意,在最新的Debian上,这将不起作用,因为您的脚本必须符合LSB(至少提供以下操作:启动,停止,重新启动,强制重新加载和状态): https://wiki.debian.org/LSBInitScripts
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
请注意,您应该放置脚本的绝对路径而不是相对路径,这样可以解决意外的问题:
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
这篇关于如何在启动时运行Shell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!