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


当前回答

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

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

其他回答

使用sudo输入cron:

Sudo crontab -e

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

@reboot sh /home/user/test.sh

拯救策略:

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

测试测试:

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

这是我在Red Hat Linux系统上执行的方法。

把你的脚本放在/etc/init.D,由根用户和可执行程序拥有。在脚本的顶部,您可以给出chkconfig指令。以oracle用户启动Java应用为例,执行以下脚本。

脚本名为/etc/init.d/apex

#!/bin/bash
# chkconfig: 345 99 10
# Description: auto start apex listener
#
case "$1" in
 'start')
   su - oracle -c "cd /opt/apex ; java -jar apex.war > logs/apex.log 2>logs/apex_error.log &";;
 'stop')
   echo "put something to shutdown or kill the process here";;
esac

这表示脚本必须在级别3、4和5上运行,启动/停止的优先级是99和10。

然后,作为root用户,你可以使用chkconfig在启动时启用或禁用脚本:

chkconfig --list apex
chkconfig --add apex

您可以使用服务启动/停止顶点。

在卢本图,我不得不处理相反的情况。Skype启动后开始运行,我发现在~/。配置/自动启动/ skypeforlinux.desktop文件文件内容如下:

[Desktop Entry]
Name=Skype for Linux
Comment=Skype Internet Telephony
Exec=/usr/bin/skypeforlinux
Icon=skypeforlinux
Terminal=false
Type=Application
StartupNotify=false
X-GNOME-Autostart-enabled=true

删除这个文件对我有帮助。

使用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代码…希望这能帮助你…

...

这里有一个更简单的方法!

首先:编写一个shell脚本并将其保存为.sh 这里有一个例子

#!/bin/bash
Icoff='/home/akbar/keyboardONOFF/icon/Dt6hQ.png'
id=13
fconfig=".keyboard"
echo "disabled" > $fconfig
xinput float $id
notify-send -i $Icoff "Internal Keyboard disabled";

这个脚本将在启动时禁用内部键盘。

第二步:打开应用程序“启动应用程序首选项”

在这里输入图像描述

在这里输入图像描述

第三:单击“添加”。 第四:在NAME部分给出一个名字。 第五:在命令部分浏览到你的.sh。 第六:编辑你的命令部分:

bash <space> path/to/file/<filename>.sh <space> --start

第七步:点击添加。完成了!

现在重新启动你的电脑来确认。

干杯!