node.js - Ghost 博客的 Bash 脚本在服务器重启时不启动

标签 node.js bash crontab forever ghost-blog

我有一个非常简单的 bash 脚本,可以启动我的 ghost 博客。我正在使用 crontab 在启动时启动脚本,这是我正在运行的 crontab 命令:

@reboot /var/www/ghost/launch.sh

脚本有以下代码:

#!/bin/sh

ps auxw | grep apache2 | grep -v grep > /dev/null

if [ $? != 0 ]
then
        NODE_ENV=production forever start --sourceDir /var/www/ghost index.js
fi

当我sudo reboot 服务器,并使用forever list 查找正在运行的进程时,我看到以下内容:

data:    [0] sHyo /usr/bin/nodejs index.js 1299    1314 /home/webadmin/.forever/sHyo.log 0:0:1:25.957

当我nano 到那个日志文件时,日志显示如下:

^[[31m
ERROR:^[[39m ^[[31mCould not locate a configuration file.^[[39m
^[[37m/home/webadmin^[[39m
^[[32mPlease check your deployment for config.js or config.example.js.^[[39m

Error: Could not locate a configuration file.
    at checkTemplate (/var/www/ghost/core/config-loader.js:16:36)
    at Object.cb [as oncomplete] (fs.js:168:19)

error: Forever detected script was killed by signal: null

它似乎在 /home/webadmin/ 中查找,但 ghost 安装在 /var/www/ghost????

当服务器通过 ssh-ing 启动到服务器后,我在终端手动运行完全相同的脚本时,脚本工作正常。我运行:cd/var/www/ghost/ 然后是 ./launch.sh 幽灵博客出现并且工作正常。 forever 进程的日志如下所示:

^[[32mGhost is running...^[[39m
Your blog is now available on http://blog.example.com ^[[90m
Ctrl+C to shut down^[[39m

我的脚本或 crontab 有什么问题导致它无法正确启动脚本?

最佳答案

I run: cd /var/www/ghost/ and then ./launch.sh and the ghost blog appears and is working fine.

就是这样,您的 cron 作业没有做同样的事情:

@reboot /var/www/ghost/launch.sh

此脚本从您的主目录执行。一种修复方法是更改​​您的 crontab:

@reboot cd /var/www/ghost; ./launch.sh

另一种方法是在 launch.sh 顶部附近添加此行,在启动 forever 之前的任何位置:

# change to the directory of this script
cd $(dirname "$0")

关于node.js - Ghost 博客的 Bash 脚本在服务器重启时不启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20234325/

相关文章:

linux - 我的 Bash 代码发生了什么?

linux - 如何通过脚本创建 crontab

node.js - 在 Web 应用程序的内存中存储复杂对象?

javascript - Node js - 在 mongoose pre ('save' ...)中散列密码不会存储到数据库

node.js - 我可以指示nightmare.js转到本地主机上的快速路由吗?

bash - 感叹号是否代表 Bash IF 条件中的否定?

node.js + socket.io 立即进行身份验证

linux - 当 var=$(... | grep "value") 为空时,脚本退出并出现错误,但当 grep 有结果时,脚本可以工作

windows - 如何在 Windows 上的 Cygwin 中运行 crontab?

audio - 如何从crontab发出铃声