I am running into an issue. I have deployed my nodejs websocket app to Amazon elastic beanstalk. I am trying to run app as daemon process, that's why I have globally installed pm2 using configfile in ebextension:
container_commands:
01_node_symlink:
command: "ln -sf `ls -td /opt/elasticbeanstalk/node-install/node-* | head -1`/bin/node /bin/node"
02_npm_symlink:
command: "ln -sf `ls -td /opt/elasticbeanstalk/node-install/node-* | head -1`/bin/npm /bin/npm"
03_pm2_install:
command: "if [ ! -e /bin/pm2 ]; then npm install pm2 -g; fi"
ignoreErrors: true
As elastic beanstalk start server automatically, to use pm2 I have written start command in my package.js
start:"pm2 start server.js -i 0 --name="hub""
But when elastic beanstalk uses this command to start server it goes in start-stop loop and all cpus is used.
Thanks in advance
That's because pm2 process exits just after it starts the server, so eb run it again. add " && pm2 logs" to the command to keep it on.
Related
I have a Docker container running in Azure App Service. In my Azure App Service on Configuration section I have a list of defined variables for database connection. I want to use them in my docker container in wp-config.php file to connect to Azure MySQL database. However, the variables are not available in container at all.
I have tried reviewing them with 'env' or 'printenv' commands after adding the following line in my start script:
eval $(printenv | sed -n "s/^\([^=]\+\)=\(.*\)$/export \1=\2/p" | sed 's/"/\\\"/g' | sed '/=/s//="/' | sed 's/$/"/' >> /etc/profile)
No success, unfortunately. I have also tried getting them via PHP with getenv('variable_name') function, also no success. All of my environment variables are available in the Kudu container which is created alongside the actual container running my app within App Service, however, I cannot use them at all in my application container. The container itself is Apache + WordPress image and I am attaching my Dockerfile and startup script below:
Dockerfile:
FROM wordpress:4.9.1-apache
RUN apt-get update
RUN apt-get -y install openssh-server \
&& echo "root:Docker!" | chpasswd
COPY sshd_config /etc/ssh/
RUN mkdir -p /tmp
COPY ssh_setup.sh /tmp
RUN chmod +x /tmp/ssh_setup.sh \
&& (sleep 1;/tmp/ssh_setup.sh 2>&1 > /dev/null)
COPY . /var/www/html
RUN chown -R www-data:www-data /var/www/html/
ENV PORT 80
EXPOSE 80 2222
ADD start.sh /
RUN chmod +x /start.sh
ENTRYPOINT ["/start.sh"]
Start.sh:
#!/bin/bash
/usr/sbin/sshd
apache2-foreground
eval $(printenv | sed -n "s/^\([^=]\+\)=\(.*\)$/export \1=\2/p" | sed 's/"/\\\"/g' | sed '/=/s//="/' | sed 's/$/"/' >> /etc/profile)
UPD: The issue was due to the app setting that enabled persistent storage. Once I changed value from 'true' to 'false', for some reason, application settings started to work as environment variables.
Basically I want to:
git --work-tree=/home/aero/server --git-dir=/home/aero/server/.git pull;
ps aux | grep node | grep server-aero-static | awk '{print $2}' | xargs kill -9;
cd /home/aero/server;
npm start;
I don't think this would work in post-receive, why? And how can I get it to work?
The Git part should work, provided the remote "origin" is correctly set in /home/aero/server, in order for that repo to work properly (as in "Git post-receive not working correctly").
The npm start part might be a problem if that command blocks.
In that case (meaning execution in a Git hook), you might consider using pm2 to start your application, as explained in "A Friendly Guide To Automate Deployment For Node.js Apps With Git Hooks" by aunnnn.
pm2 start npm --name 'my-app' — start \
&& echo "post-receive: app started successfully with pm2".
I am deploying an app to AWS EC2 instance and I am using a config file in the .ebextensions folder to execute a bash script.
In the script I am detecting if a particular service is running, if it's not running, install the package using rpm.
#!/bin/bash
service --status-all | grep -q 'MyService'
if [ $? -ne 0 ];
then
install my package
else
do nothing
fi
But the script is not working and it always go to install my service again.
It is because
service --status-all
is returning nothing at all which confuses me! But after the deployment when I go to the EC2 instance and try out the script, it does work. Just that it does not work during deployment.
There should be at least some services running but empty?
Am I doing this correctly?
Figured out what's wrong, putting it here for for everyone's reference.
service --status-all
works while in the bash prompt, but does not seem to return anything during my AWS EC2 deployment.
I have changed it to something like
if ps ax | grep -v grep | grep -q 'My Service'
from here: http://www.akamaras.com/linux/linux-script-to-check-if-a-service-is-running-and-start-it-if-its-stopped/
and it works perfectly.
I want to use -e and -o flags with pm2 to customize logs.
I tried:
pm2 start index.js -e="err.log" -o="out.log"
pm2 start index.js -e "err.log" -o "out.log"
pm2 start index.js --error="err.log" --output="out.log"
pm2 start index.js --error "err.log" --output "out.log"
Have anyone tried this? Any of those options doesn't work for me.
My package.json has:
"scripts": {
"start": "node_modules/.bin/coffee server.coffee",
"test": "NODE_ENV=test node test/runner.js",
"coverage": "NODE_ENV=test COVERAGE=1 node test/runner.js -R html-cov test/ > ./test/coverage.html",
"testw": "fswatch -o test src | xargs -n1 -I{} sh -c 'coffeelint src server.coffee ; npm test'",
"db:drop": "node scripts/drop-tables.js",
"encryptConfig": "node_modules/.bin/coffee config/encrypt.coffee",
"decryptConfig": "node_modules/.bin/coffee config/decrypt.coffee",
"postinstall": "npm run decryptConfig"
},
When I deploy to Elastic Beanstalk, I'd like to run the postinstall, but apparently it doesn't do that. Okay, no problem.
I created a file called .ebextensions/00.decrypt.config which has:
commands:
00-add-home-variable:
command: sed -i 's/function error_exit/export HOME=\/root\n\nfunction error_exit/' /opt/elasticbeanstalk/hooks/appdeploy/pre/50npm.sh
container_commands:
02-decrypt-config:
command: $NODE_HOME/bin/npm run decryptConfig
However this doesn't seem to run either. What am I doing incorrectly?
I figured out a workaround for this issue. The npm binary on an EB instance is located in /opt/elasticbeanstalk/node-install/node-{version}. You should make sure this is present in your PATH first.
00_setpath.config
commands:
01_set_path:
command: echo 'export PATH=$PATH:`ls -td /opt/elasticbeanstalk/node-install/node-* | head -1`/bin' >> /root/.bash_profile
02_set_path:
command: export PATH=$PATH:`ls -td /opt/elasticbeanstalk/node-install/node-* | head -1`/bin
As you can see I am appending to .bash_profile and also adding PATH to the current shell. The former should be sufficient for your purpose. I added the second one as I'm using the npm command within a script in my package.json, and it seems these scripts are run inside the same shell. TL/DR: You should now be able to use npm in both places.
As for your npm scripts, try using prestart instead of postinstall.
A few suggestions:
Try to enclose your commands in quotes, that's a requirement
Also, not sure if $NODE_HOME is working - could you run simple test like echo $NODE_HOME > /tmp/test.txt?