Reputation: 413
I have a nodejs app that I want to restart automatically after a server reboot. I have created a script to start the app with forever, as shown below.
#!/bin/sh
export PATH=/usr/bin:$PATH
forever start --command node --minUptime 1000 --spinSleepTime 10000 --sourceDir /etc/csc server.js >> /etc/csc/log.txt 2>&1
I've also created a crontab entry to run this script after a reboot:
@reboot /etc/csc/csc-starter.sh
Although, I don't think the crontab part is really relevant, as I will explain below. The problem is that while the nodejs app does get started after a reboot, it does not respond correctly to client requests. For example, invocation of a URL that should result in the main view of the application being displayed (in a browser), results in the following output:
{
code: "ResourceNotFound",
message: "/"
}
Interestingly enough, the same output is observed when I invoke the starter script manually. However, if I run the nodejs app maually with:
node /etc/csc/server.js
it works perfectly well. I am a relative newcomer to Linux and you may safely assume that I might not be aware of some accepted truths about working in Linux, such as perhaps the propriety of putting the app under /etc.
Any ideas why starting the app with forever would change it's internal behavior? The app in question is pretty simple. It's based on restify and has a couple of REST routes, as well as hosting static content, but it's really nothing fancy.
Thanks.
Upvotes: 3
Views: 1511
Reputation: 476
/etc/csc server.js >>
Remove the space in the path.
/etc/csc/server.js >>
Upvotes: 0
Reputation: 38821
/etc probably isn't the best place for your app. Although that's not directly related to your problem.
When you start your app from the command line manually you're probably already in the /etc/csc directory.
Try adding a directory change to your startup script:
#!/bin/sh
export PATH=/usr/bin:$PATH
cd /etc/csc
forever start --command node --minUptime 1000 --spinSleepTime 10000 --sourceDir /etc/csc server.js >> /etc/csc/log.txt 2>&1
The app is probably configured to use a subdirectory off the current directory for its static resources.
--
You should consider putting your app in /opt/csc. It's a more generic place for third party apps. /etc is more for system configuration.
Upvotes: 3