如何在启动时运行 shell 脚本 [英] How to run a shell script at startup
问题描述
在 Amazon S3 Linux 实例上,我有两个脚本,名为 start_my_app
和 stop_my_app
启动和停止永远(依次运行我的 Node.js 应用程序).我使用这些脚本手动启动和停止我的 Node.js 应用程序.到目前为止一切顺利.
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屋!