Where do npm background scripts go? - javascript

I have a program which relies on another program being run so I added this build command to my package.json file
"scripts": {
"start": "brunch watch --server",
"build": "coffee server/modules/events/book_server.coffee &"
}
so before I run my main script with npm start I run npm run-script build which makes my main script work, however when I quit my main script and then look for background processes with bg I get the message
> -bash: bg: current: no such job
so I decided to run npm run-script build again however the previous process must still have been running because I got the error message
events.js:66
throw arguments[1]; // Unhandled 'error' event
^
Error: listen EADDRINUSE
at errnoException (net.js:776:11)
at Server._listen2._connectionKey (net.js:917:26)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)
and when I tried to run my main program again I got the error
> TypeError: Cannot read property 'port' of null
My question is, where are these background scripts?
So far I have looked in all the obvious places including package.json's directory the directory for book_server.coffee and in node_modules but with no luck.

So the background processes go into the background just running inside the OS but not attached to your shell's tty. To find them, use jobs -l or the ps command with something like ps -ef. You can use grep to filter the output, but what you need to find is the process ID (PID) of your build process so you can stop it with kill <pid>. You may also want to read up on pgrep and pkill which are handy for this process.
Note that in your example you use bg when it's not appropriate. bg is for this sequence: 1. start a job in your shell's foreground, 2. suspend that job with CTRL-Z, 3. use bg to tell the shell "allow this job to continue executing, but detached from my tty in the background". (again, jobs is what you are looking for here).
For the bigger picture, there's no need or benefit of running that coffee command in the background as it is just a simple compliation step that should take on the order of a few milliseconds.
For an amazingly-detailed "reread every year" level of depth, check out The TTY demystified.

Related

How to concat multiple bash commands?

Having this bash_profile commands, trying to run backend and frontend server in one single alias command i.e ins
alias is='ivui && npm run start:backend'
alias ib='ivbe && npm run start:dev'
alias ins='ib && is'
is referring to a different project folder and it starts the server and ib is also referring to a different folder and server. Trying to concat both, but the first one only triggers and concat of && is not executed.
Concat npm helps in combining both servers from single project folder, but wondering how we can get this done using bash_profile? so that by executing just ins, it must start backend server first and frontend also.
The reason your double alias command (ib && is) does not work is because, as per man bash:
Aliases allow a string to be substituted for a word when it is used as
the first word of a simple command.
Stands to reason that if you run ib && is where both ib and is are aliases, it will only run ib.
With that out of the way, there is have a different and, I believe, better solution to your problem. You can use screen to run those 2 commands in the background and also, as a bonus, have the ability to watch their terminal output any time you want.
The idea is this: 1. Start a screen session; 2. Open two windows inside that session; 3. Run npm run start:backend in 1st window and run npm run start:dev in the second window.
Here is what you need:
screen -S Servers -t backend_window -A -d -m
screen -S Servers -X screen -t dev_window
This will start a screen session with backend_window and dev_window inside it. Now you just need to run your 2 commands inside those windows:
screen -S Servers -p backend_window -X stuff $'npm run start:backend\n'
screen -S Servers -p dev_window -X stuff $'npm run start:dev\n'
Now both your npm commands are running in the background at the same time. You can just put these 4 lines into your .bashrc file and it will launch on log in.
But the best part about this approach is you can visually see what's going on with those npm commands by attaching the screen and looking inside those windows. Just run:
screen -rx Servers
This will show you your first window by default. Let's split the screen and show both windows side by side:
Ctr-A + | <- will split the screen into 2 sections
Ctr-A + Tab <- will shit cursor to the new section
Ctr-A + " <- will show you your 2 windows, just pick the dev
All this will give you this view
Keep in mind that both your npm commands will continue to run even after you log out. To kill them, either attach the screen as described above and then Ctrl-C both processes. Or just run killall screen and they will just die.
You could create a function and add your aliases inside, for example,
stopdev () (
cd "$HOME/website" && {
make website_stop
ret=$?
make backend_stop && return $ret
}
)
This example also has a return code, and has a subshell called to run the function, so the filepath is not $HOME/website after the function is called.
More information on the following webpage,
https://unix.stackexchange.com/questions/266063/why-cant-i-call-two-aliases-with

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.

WebStorm keeps crashing my locally ran server because of Git's index.lock

Error: EPERM: operation not permitted, lstat 'C:\ProjectDirectory\.git\index.lock'
at Error (native)
I'm using WebStorm and everytime I run the local server for testing purposes using npm start, it crashes inevitably sometimes after doing nothing, and sometimes after making a change or so.
I'm using this React boilerplate and the actual author responded to a bug issue I brought up about this saying "Based on the error, it looks like either your editor or your source control system is locking files."
I'm a bit tired restarting the server every time I make a couple changes and would love to fix this.
Full error log
Only IDE have reason to watch .git/ folder. So if something else tries then it's a bug in configuration.
npm start is an alias for npm-run-all --parallel test:watch open:src lint:watch.
Make sure that .git/ is exempted in their configuration.

MEAN.io application not starting

I am trying to teach myself how to use the MEAN stack and I am working through the docs from the MEAN.io site. I am at the point where I run the following commands:
$ npm install -g mean-cli
$ mean init <myApp>
$ cd <myApp> && npm install
and these run just fine. But when I get to the next step which is to either just run
gulp
or
node server
and nothing happens. If I run gulp then after it gets to the part where it says
Finished 'development' after 6.15 μs
in the logs it just hangs there. If I run node server it just hangs and nothing else happens. In either case I see nothing at
http://localhost:3000/
Is this a common sticking point for people?

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