在一个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,但这对我来说并不管用。


当前回答

创建自己的/init可执行文件

这不是你想要的,但很有趣!

只需选择一个任意的可执行文件,甚至是一个shell脚本,并使用命令行参数引导内核:

init=/path/to/myinit

在引导结束时,Linux内核在给定的路径上运行第一个用户空间可执行文件。

有几个项目提供了主流发行版(如systemd)所使用的流行的init可执行文件,并且在大多数发行版中,init会派生出一堆用于正常系统操作的进程。

但是我们可以劫持/初始化它来运行我们自己的最小脚本,以更好地理解我们的系统。

这里是一个最小的可复制设置:https://github.com/cirosantilli/linux-kernel-module-cheat/tree/f96d4d55c9caa7c0862991025e1291c48c33e3d9/README.md#custom-init

其他回答

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

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

这个简单的解决方案在一个运行CentOS的亚马逊Linux实例上对我有效。 编辑/etc/rc.d/rc.本地文件,并把命令放在那里。在这个文件中提到,它将在所有其他init脚本之后执行。所以在这方面要小心。这是如何文件寻找我目前..最后一行是我的脚本名称。

你可以这样做:

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

使用Python 3微服务或shell;使用Ubuntu Server 18.04 (Bionic Beaver)或Ubuntu 19.10 (Eoan Ermine)或Ubuntu 18.10 (Cosmic Cuttlefish)我总是喜欢这些步骤,而且它也总是有效:

Creating a microservice called p example "brain_microservice1.service" in my case: $ nano /lib/systemd/system/brain_microservice1.service Inside this new service that you are in: [Unit] Description=brain_microservice_1 After=multi-user.target [Service] Type=simple ExecStart=/usr/bin/python3.7 /root/scriptsPython/RUN_SERVICES/microservices /microservice_1.py -k start -DFOREGROUND ExecStop=/usr/bin/python3.7 /root/scriptsPython/RUN_SERVICES/microservices/microservice_1.py -k graceful-stop ExecReload=/usr/bin/python3.7 /root/scriptsPython/RUN_SERVICES/microservices/microservice_1.py -k graceful PrivateTmp=true LimitNOFILE=infinity KillMode=mixed Restart=on-failure RestartSec=5s [Install] WantedBy=multi-user.target Give the permissions: $ chmod -X /lib/systemd/system/brain_microservice* $ chmod -R 775 /lib/systemd/system/brain_microservice* Give the execution permission then: $ systemctl daemon-reload Enable then, this will make then always start on startup $ systemctl enable brain_microservice1.service Then you can test it; $ sudo reboot now Finish = SUCCESS!!

这可以用相同的主体脚本来运行shell, react…数据库启动脚本…任何类型的OS代码…希望这能帮助你…

...