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


当前回答

很多答案都是在启动时启动某个东西,但通常你想稍微晚一点启动它,因为你的脚本依赖于例如网络。使用at只是添加这个延迟,例如:

at now + 1 min -f /path/yourscript

你可以在/etc/rc.中添加本地的,还在cron喜欢:

# crontab -e
@reboot at now + 1 min -f /path/yourscript

把cron和at结合起来是不是很有趣?信息在手册页。

至于@reboot可能不被广泛支持的评论,试试看吧。我发现/etc/rc.Local在支持systemd的发行版上已经过时了,比如ubuntu和raspbian。

其他回答

将脚本添加到/etc/init.d /目录 更新rc运行级别: update-rc美元。d myScript.sh默认NN,其中NN是它应该执行的顺序。例如,99意味着它将在98之后和100之前运行。

只需有一行添加到您的crontab..

确保文件是可执行的:

chmod +x /path_to_you_file/your_file

编辑crontab文件。

crontab -e

你必须添加的行:

@reboot  /path_to_you_file/your_file

这么简单!

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

chmod +x /home/user/startup.sh

然后为它设置crontab:

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

现在你的startup.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代码…希望这能帮助你…

...

完成任务的多种方法:

定时任务 自动加载 摆在 systemd