在一个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应该是可执行的(我认为是这样)。

其他回答

另一种选择是在crontab中使用@reboot命令。

不是每个版本的cron都支持这一点,但如果您的实例基于Amazon Linux AMI,那么它就可以工作。

使用sudo输入cron:

Sudo crontab -e

添加一个在启动时运行的命令,在这种情况下是一个脚本:

@reboot sh /home/user/test.sh

拯救策略:

按ESC然后:x保存并退出,或者按ESC然后ZZ(也就是shift+ ZZ)

测试测试:

运行不带cron的测试脚本,以确保它实际工作。 确保您将命令保存在cron中,使用sudo crontab -e 重启服务器以确认一切正常sudo @reboot

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应该是可执行的(我认为是这样)。

我参考了这个博客,总是听起来不错的选择

https://blog.xyzio.com/2016/06/14/setting-up-a-golang-website-to-autorun-on-ubuntu-using-systemd/

vim /lib/systemd/system/gosite.service

Description=A simple go website
ConditionPathExists=/home/user/bin/gosite

[Service]
Restart=always
RestartSec=3
ExecStart=/home/user/bin/gosite

[Install]
WantedBy=multi-user.target

systemctl enable gosite.service

你可以这样做:

chmod +x PATH_TO_YOUR_SCRIPT/start_my_app 

然后使用下面的命令

update-rc.d start_my_app defaults 100

请参阅有关Cyberciti的本页。