运行 shell 脚本在 Ubuntu 14.04 上启动(不是登录)

Run an shell script on startup (not login) on Ubuntu 14.04

我有一个构建服务器。我正在使用 Azure 构建代理脚本。这是一个 shell 脚本,它将在服务器启动时持续 运行。问题是我似乎无法在启动时将其设置为 运行。我已经尝试了 /etc/init.d/etc/rc.local,但代理没有 运行。与引导日志中的构建代理无关。

我为 /etc/init.d 创建了脚本 agent.sh,其中包含:

#!/bin/bash

sh ~/agent/run.sh 

赋予它适当的权限chmod 755 agent.sh并将其移动到/etc/init.d

并且对于 /etc/rc.local,我只是附加了以下内容

sh ~/agent/run.sh &

exit 0.

之前

我做错了什么?

编辑:添加示例。

编辑 2:刚刚注意到 init.d 自述文件说 shell 脚本需要以 #!/bin/sh 而不是 #!/bin/bash 开头。也用绝对路径,但没有变化。

最终编辑:正如@ewrammer 所建议的,我使用了 cron 并且它起作用了。 crontab -e 然后 @reboot /home/user/agent/run.sh

如果您不发布您所做的事情,很难看出哪里出了问题,但为什么不将它添加为 cron 作业并使用 @reboot 作为模式呢?然后cron会在每次电脑启动时运行脚本

为了以防万一,使用主管可能是个好主意,在 Ubuntu 14 中你没有 systemd 但你可以选择其他 https://en.wikipedia.org/wiki/Process_supervision.

如果使用 immortal,安装后,您只需在 /etc/immortal 中创建一个 run.yml 文件,内容如下:

cmd: /path/to/command
log:
    file: /var/log/command.log

这将在每次启动时启动您的 script/command,此外还确保您的 script/app 始终启动并且 运行。