【问题标题】:Startup script in crontab doesn't work on rebootcrontab 中的启动脚本在重新启动时不起作用
【发布时间】:2020-01-01 06:08:48
【问题描述】:

在系统重启时init 6 我想为我的 rails 项目启动服务,脚本包含从 git 拉取更改,运行 bundle install 并运行延迟的工作人员

脚本在/etc/init.d/app-startup-script

#!/bin/sh

cd /home/deploy/source/myapp

git pull origin development

bundle install

bundle exec cap staging deploy

touch /home/deploy/hello.txt (added this to test if it will create file on reboot)

我在重启时在crontab -e 中运行这个脚本

@reboot /etc/init.d/app-startup-script

我通过手动运行确认 app-startup-script 正在运行 /etc/init.d/app-startup-script

但是当我使用

重新启动系统时
sudo su - 
init 6

然后再次 ssh 到我的服务器,它只会在 /home/deploy 中创建 hello.txt 文件,但没有启动我的 rails 服务

【问题讨论】:

  • /etc/init.d 用于init 启动脚本;你想为这个找到一个更好的地方。也许/usr/local/bin/app-startup-script
  • 这可能会意外修复脚本,使其仅在网络启动且稳定等情况下运行。
  • @tripleee 我将脚本从 /etc/init.d 移动到 /usr/local/bin/ 它创建 hello.txt 文件但不运行 rails 服务
  • 运行它并将错误输出到一个文件,这样你就可以看到出了什么问题。我猜它会在您的系统日志中产生错误,但确切的位置取决于平台。 @reboot /usr/local/bin/app-startup-script >>/home/you/app-startup.log 2>&1 应该让它们在你的主目录中可见(显然调整路径)。
  • 在你的 shell 脚本顶部添加一个set -e。这会导致它因错误而中止。另外,检查现有的输出。

标签: shell ubuntu cron


【解决方案1】:

如果您愿意切换到systemd 服务,那么,

您的启动脚本,

$ cat start.bash
#!/bin/bash

cd /home/deploy/source/myapp

git pull origin development
bundle install
bundle exec cap staging deploy
touch /home/deploy/hello.txt (added this to test if it will create file on reboot)

您的服务文件,

$ cat yourapp.service
[Unit]
Description=Your rail app
Requires=multi-user.target
After=multi-user.target

[Service]
User=root # if it should be run as root, otherwise specify the user
ExecStart=/path/to/start.bash

[Install]
WantedBy=multi-user.target

那就做吧,

$ chmod +x start.bash
$ cp yourapp.service /etc/systemd/system/

$ systemctl daemon-reload
$ systemctl enable yourapp.service # this should make sure, the service starts on boot

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-06
    • 1970-01-01
    • 2014-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多