Ghost博客的Bash脚本无法在服务器重启时启动

时间:2013-11-27 05:06:05

标签: node.js bash crontab forever ghost-blog

我有一个非常简单的bash脚本应该启动我的鬼博客。我正在使用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/,但幽灵安装在/var/www/ghost ????

当我在ssh服务器启动服务器后手动运行终端中完全相同的脚本时,脚本运行正常。我运行: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无法正确启动脚本有什么问题?

2 个答案:

答案 0 :(得分:3)

  

我运行:cd /var/www/ghost/然后./launch.sh并且出现了鬼博客并且工作正常。

就是这样,你的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")

答案 1 :(得分:1)

对于任何遇到此问题的人来说,我强烈建议调查pm2以启动Ghost并监控Ghost。它将像Forever一样监视Ghost,并具有内置功能,可以在服务器重新启动时生成启动pm2的init脚本。还有更好的功能来监视Ghost运行时。看看我如何here