Question

I have a very simple bash script which should launch my ghost blog. I am using crontab to launch the script on startup, here is the crontab command I am running:

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

The script has the following code:

#!/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

When I sudo reboot the server, and use forever list to find the processes running, I see the following:

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

When I nano to that log file, the log says the following:

^[[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

It appears to be looking in /home/webadmin/, but ghost is installed at /var/www/ghost????

When I run the exact same script in the terminal manually after the sever has started up by ssh-ing into the server, the script works fine. I run: cd /var/www/ghost/ and then ./launch.sh and the ghost blog appears and is working fine. The log for that forever process says the following:

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

What is wrong with my script or crontab that it cannot launch the script properly?

Was it helpful?

Solution

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

That's the thing, your cron job is not doing the same:

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

This script is executed from your home directory. One way to fix is to change your crontab:

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

Another way is to add this line near the top of launch.sh, anywhere before launching forever:

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

OTHER TIPS

Just an FYI for anybody that runs across this I would highly suggest looking into pm2 to start Ghost and to monitor Ghost. It will monitor Ghost like Forever and has a built in feature to generate a init script to start pm2 when your server restarts. Also has better features to monitor Ghost while it is running. Check out my how to here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top