start node and pm2 application from bat file - javascript

I have a chat-bot application running on node and I keep it always active thanks to pm 2.
I would like to improve the way I launch the application. Instead of running the start command from the console, it would be nice to double click a .bat file.
I am trying to develop the bat file, but I lack knowledge.
I am grateful for any help.
#echo off
SET PM2_HOME=C:\Users\Usuario\.pm2
pm2 start C:\Users\Usuario\Desktop\ROBOTs\Chatbot SCTR\app.js
echo servicio ejecutado
This bat file that I developed does not work. I know that I am not calling the variables, because I don't know how to include it, since I always execute the pm2 start app.js command.
my application does not use ports like 8080 and others, because the same library allows me to establish a connection and with pm 2 I keep it always active.

add the start command to your package.json for launching your app with pm2, then with your bat file just direct it to run with npm or yarn, whatever your default package manager is
edit:
here is a sample of a script in bash, but the concept will be the same for batch
#!/bin/bash
## detect operating system machine so we can setup some environment variables
UNAME="$(uname -s)"
case "${UNAME}" in
Linux*) OS='linux';;
Darwin*) OS='mac';;
CYGWIN*) OS='cygwin';;
MINGW*) OS='mingw';;
*) OS="UNKNOWN:${UNAME}"
esac
## if OS is Ubuntu (IE Production Box) set the path of variables
if [ $OS == 'linux' ]
then
YARN=/usr/bin/yarn
PM2=/usr/bin/pm2
fi
## if OS is Mac (IE Development Box) set the path of variables
if [ $OS == 'mac' ]
then
YARN=/usr/local/bin/yarn
PM2=/usr/local/bin/pm2
fi
## run the app
cd /var/www/application || exit
$YARN run productionStart
$PM2 save
exit $?
here is the line of code for starting the app on mac/linux we use from our package.json
"productionStart": "pm2 start ecosystem.config.js --env=production",
for more information about starting your app with an ecosystem file with pm2, see the docs here

Related

Http-server running in background with nohup doesn't remain live

I have an ubuntu server. On which I have transferred some files which have some js and html code.
I used http-server from node.js to start a web server so that I can display the html page on the server.
I used nohup so that it can remain running even if I disconnect or close my system.
Here is the command I used:
nohup http-server -p 8000 -a 10.4.145.182 &
Now this helps me visualize the files on 10.4.145.182:8000 but I am noticing after sometime the server goes down and one can't access the html page on that ip in their browser.
I thought nohup helps run things in background even if one closes their system or logs out of server.
How do I make this web server running always then and accessible to url to everyone
Thanks
EDIT:
As per the suggestion below of using pm2, I installed latest version of node and then started service with pm2.
It says service started for http-server but when I go the to ip with port 8000 on browser it doesn't open up.
Here is the command I ran in my directory which has the html and d3 files.
$ pm2 start $(which http-server) -p 8000
And here is the output in shell of the pm2
Try this:
http-server & exit
You can also specify a port number:
http-server -p 8082 & exit
Reference here
I suggest you use pm2 . Long story short , check the official link https://www.npmjs.com/package/pm2
Steps :
Install :
npm install pm2 -g
Run your app :
pm2 start app.js
In your case use :
pm2 start /usr/local/bin/http-server -- -p 8080
for reboot to work run :
pm2 startup systemd
take the last line , change the user and home path and run the modified line as SUDO !!! you need sudo access for this !!!

Bot shuts down when putty window is closed

I created a discord bot and am now attempting to run it off an Ubuntu Machine.
I installed the folders of the bot and NodeJs, here is what I used to install NodeJS:
sudo apt-get install -y nodejs
Then I used cd to select the directory, and started my bot using node index.js
The bot started, however when I went to close the putty and keep it running on the VPS the bot shutdown. Here is what the directory looks like.
I think the problem is that when you start the app in the putty window, that process is linked to the window and gets terminated when that is closed.
To avoid that you can use a host service like screen, tmux, nohup, bg and so on...
If you want to know which is the best, try looking at this question from the askUbuntu Stack Exchange.
The key concept is that you open a new window using the tmux command (or screen, ...), then run your bot like you always do. When you want to leave but keep the process runing, you can detach the session with a key combination, that changes from service to service.
If you want to access that window again, you can run a command that will "restore" your session, like
tmux list-sessions
tmux attach-session -t 0
The NodeJS instance is terminated when putty is closed. You need something to keep the instance alive. Try:
PM2: http://pm2.keymetrics.io/
or,
Forever: https://github.com/foreverjs/forever#readme
Recommended though is to run the node instance as a service that can reboot on startup. Try looking at this:
https://stackoverflow.com/a/29042953/7739392
The shell runs in the foreground. This means any scripts you start there will end once you end your session. A simple solution would be to run your script in the background by adding the & after the call:
node index.js &
A better solution would be to create a service you can ask the service daemon to run for you. However, adding the & should get you what you want for now.
I recommend using one of these two node modules - ForeverJS or PM2. I'll show you how to quickly get started with ForeverJS but PM2 would be very similar.
You can easily install ForeverJS by typing the following in your terminal:
$ npm install forever -g
You may need to use SUDO depending on your user's privileges to get this working properly. It is NOT recommended to use it in production due to the security risks.
Once installed CD to your projects file directory and like you typed 'node index.js' you will do something similar with ForeverJS.
$ forever start index.js
Now when you exit the terminal your NodeJS application will remain as a running process.

DroidEdit Termux integration

DroidEdit allows you to run commands if you have sl4a installed and the am command works wonderfully... I was wondering if there is a way to use am to launch an app with params .. specifically I want a command that will tell termux to run a command on a file that I specificy ... so far I found this snippet that launches the termux app .. now how do I get it to run node or webpack from npm as well?
am start --user 0 -n com.termux/com.termux.app.TermuxActivity

Running Xcode from terminal cant open javascript script (Error: Instruments cannot open files in the “TextWrangler text document” format.")

I'm in the process of setting up our CI environment for one of our iOS projects. So far I have got Jenkins building and packaging the app from the command line. I have written a automation test in Instruments (Automation) and I want to fire this from the command line/terminal. I have followed the posts on here describing the way to do this (Start Instruments from the command line & Can the UI Automation instrument be run from the command line?) so I know this is possible.
Unfortunately, when I try this I get an alert with:
"The document “AutomatedTests.js” could not be opened. Instruments cannot open files in the “TextWrangler text document” format."
Here is the command i'm using:
/Applications/Xcode4.6.3.app/Contents/Applications/Instruments.app/Contents/MacOS/Instruments \
-t /Applications/Xcode4.6.3.app/Contents/Applications/Instruments.app/Contents/PlugIns/AutomationInstrument.bundle/Contents/Resources/Automation.tracetemplate -a "/Users/[CI USERNAME]/workspace/[IOS APP FOLDER]/IOS/build/Debug-iphoneos/[IOS APP NAME].app" \
-e UIASCRIPT /Users/[CI USERNAME]/workspace/[IOS APP FOLDER]/IOS/[FOLDER CONTAINING TEST FILE]/AutomatedTests.js
https://github.com/bendyworks/bwoken
Try this instead of using xcode directly, might save you some time

Ubuntu 8.04 Hardy and node.js upstart script

I am trying to write an upstart script for my ubuntu machine, which is version 8.04 "Hardy". I have followed the instructions on this site: upstart for node.js but it seems like these instructions are for a current version of ubuntu.
I noticed that the /etc/init directory does not exist on my machine, first I tried putting the script in the /etc/init.d directory and then I created the /etc/init dir and placed it there.
I will post my upstart script below (which is basically the same as from the website above with some path changes), but when I run start jobname, I just get an error "start: Unknown job: jobname". So then I changed the script around to a slimmed down version, posted below, and still I get the same result.
For now, I am using the 'nohup' command to run my node server but I would like a more permanent solution.
Please, any help?
SCRIPT 1:
description "node.js chat server"
author "iandev ith3"
# used to be: start on startup
# until we found some mounts weren't ready yet while booting:
start on started mountall
stop on shutdown
# Automatically Respawn:
respawn
respawn limit 99 5
script
# Not sure why $HOME is needed, but we found that it is:
export HOME="/root"
exec /root/local/node/bin/node /home/ian/chat.js >> /var/log/node.log 2>&1
end script
post-start script
# optionally put a script here that will notifiy you node has (re)started
# /root/bin/hoptoad.sh "node.js has started!"
end script
SCRIPT 2:
description "node.js chat server"
author "iandev ith3"
script
exec /root/local/node/bin/node /home/ian/chat.js >> /var/log/node.log 2>&1
end script
Just use Forever. https://github.com/indexzero/forever
From looking at the website you provided I'd say that the /etc/init was just a typo and it should be /etc/init.d/. Some things you may want to check:
executable flag on your scripts. With most versions of Ubuntu executable files show up green when running 'ls' from the command line. If you want to check if your file is executable run 'ls -l /etc/init.d/YOUR_SCRIPT' from the command line. You will see something like this:
-rwxr-xr-x 1 root root 1342 2010-09-16 10:13 YOUR_SCRIPT
The x's mean that it is executable.
To set the executable flag if it is not set, run chmod u+x YOUR_SCRIPT
I'm pretty sure for older versions of ubuntu you need to have the script in /etc/rc.d/rc3.d or /etc/rc3.d. What linux does is run through rc0.d to rc5.d and execute every script in there. From what it looks like, ubuntu is moving away from this to something simpler so if you have rc directories you may need to edit your script a little.
Anyway I think i'm getting a little over complicated here. Check your executable flag and if you have rc directories and we'll move on from there.
May not be the best thing to start a process with sudo, but here's what I have setup on my local pc:
#!upstart
description "node.js server"
author "alessio"
start on startup
stop on shutdown
script
export HOME="/ubuntu"
exec sudo -u ubuntu /usr/bin/node /home/ubuntu/www/test.js 2>&1 >> /var/log/node.log
end script
Hope this helps.

Categories