2013-11-27 102 views
3

我有一个非常简单的bash脚本,它应该启动我的幽灵博客。我使用crontab到启动时启动脚本,这里是我运行的crontab命令:Ghost博客的Bash脚本无法在服务器重启时启动

@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到该日志文件,日志说个E采用:

^[[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 -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中,它不能正常启动脚本?

回答

3

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

这东西,你的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") 
+0

非常感谢 – Neil

1

只是一个供参考的任何人跨越运行这个我强烈建议寻找pm2来启动Ghost并监控Ghost。它将像Forever一样监视Ghost,并且具有内置功能,可在服务器重新启动时生成启动脚本以启动pm2。还有更好的功能来监视Ghost正在运行。看看我的如何here