在一个Amazon S3 Linux实例中,我有两个名为start_my_app和stop_my_app的脚本,它们永远地启动和停止(反过来运行我的Node.js应用程序)。我使用这些脚本手动启动和停止我的Node.js应用程序。到目前为止一切顺利。

我的问题是:我还想设置它,以便在系统启动时运行start_my_app。我知道我需要在init中添加一个文件。d和我知道如何符号链接到rc内的适当目录。d,但是我不知道我放在init。d中的文件里到底需要什么。我认为它应该只有一行,比如start_my_app,但这对我来说并不管用。


当前回答

Debian 9参见https://askubuntu.com/questions/228304/how-do-i-run-a-script-at-start-up。它帮助了我。Debian 9的简短版本: 在/etc/rc.local中添加命令(以root用户)

/path_to_file/filename.sh ||  exit 1   # Added by me
exit 0

可能,/path_to_file/filename.sh应该是可执行的(我认为是这样)。

其他回答

你可以这样做:

chmod +x PATH_TO_YOUR_SCRIPT/start_my_app 

然后使用下面的命令

update-rc.d start_my_app defaults 100

请参阅有关Cyberciti的本页。

Debian 9参见https://askubuntu.com/questions/228304/how-do-i-run-a-script-at-start-up。它帮助了我。Debian 9的简短版本: 在/etc/rc.local中添加命令(以root用户)

/path_to_file/filename.sh ||  exit 1   # Added by me
exit 0

可能,/path_to_file/filename.sh应该是可执行的(我认为是这样)。

完成任务的多种方法:

定时任务 自动加载 摆在 systemd

编辑rc。本地文件使用nano或gedit编辑器,并添加您的脚本在其中。文件路径可以是“/etc/rc”Local或/etc/rc.d/rc. Local。

sudo nano /etc/rc.local

编辑如下:

#!/bin/sh
/path-to-your-script/your-scipt-name.sh

一旦完成按ctrl+o更新,再按ctrl+x。

使文件可执行。

sudo chmod 755 /etc/rc.local

然后在引导期间启动rc-local服务以运行脚本。

sudo systemctl start rc-local

首先创建你的启动脚本@ /home/user/startup.sh,并使其可执行

chmod +x /home/user/startup.sh

然后为它设置crontab:

$ crontab -e
@reboot  /home/user/startup.sh

现在你的startup.sh脚本将在每次启动时运行。