how to determine if gulp watch is running - javascript

I run gulp on server in background running
gulp &
but sometimes it fail down. So mi question is: Is there some command for gulp to ask if is running. Something like
gulp status
Thanks

There is nothing special in gulp to limit running multiple processes or alert you to an already running gulp process.
Use regular Unix techniques to check if a process is running. Use a supervisor like supervisord or runit to automatically restart processes.
#the program pidof will set the variable $? to either
#1 or 0 in bash depending on if it finds a process
#with that name. It will also print out all the matching
#process IDs to the command line
pidof gulp
echo $? #1 if there is no process called gulp
#0 if there is a process called gulp
if pidof gulp; then
echo 'gulp is alive';
else
echo 'we need some support over here!'
./node_modules/.bin/gulp watch &
sleep 3
fi

Related

start node and pm2 application from bat file

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

Forever.js doesn't restart my Node.js / Express app

I'm using forever.js to make sure my Node.js / Express app runs without a break. However, sometimes it crashes and forever doesn't restart it.
I'm using
forever start app.js
to start the app
and it starts fine, works, and then at some point crashes and when I do
forever list
it doesn't list anything, so it simply doesn't restart...
I also tried running it with a log file, using
forever start -l foreverlog.txt app.js
and the log file is fine, but it doesn't show any info about the end of the process - e.g. the crash or error report, which I usually have if I run the app.js from my console directly.
Do you know how I could make forever restart the app or at least get the errors into the log?
Thank you!
Forever splits the log
-l LOGFILE Logs the forever output to LOGFILE
-o OUTFILE Logs stdout from child script to OUTFILE
-e ERRFILE Logs stderr from child script to ERRFILE
Even if you didn't specify the -e file it should be somewhere. Users/{UserName}/.forever on windows.
You should check the ERRFILE. Maybe that will bring some light to why the process failed.
You need to use spinSleepTime:
forever --minUptime 1000 --spinSleepTime 1000 -w -l *.log -e *.log --port 1234 app.js
--minUptime not set. Defaulting to: 1000ms
--spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
Try killing the process manually by searching for the process with
ps axl | grep node
then
kill 24597
replace 24597 with your process number. Then reboot forever again.
http://blog.nodejitsu.com/keep-a-nodejs-server-up-with-forever/

grunt task: start mongod if not running

I want to write a grunt task to start the process mongod if the server is not already running. I need a mongod process running, but also need grunt-watch to work later in the task flow.
This question explains how to start mongod using grunt-shell ... the accepted answer is blocking, and the async version will spawn a new server even if one exists.
Is there a way (e.g. shell script) to start mongod only if it is not running, without blocking the rest of the grunt task flow?
Thanks
Here's a cleaner version
Store this as startMongoIfNotRunning.sh in same location as Gruntfile:
# this script checks if the mongod is running, starts it if not
if pgrep -q mongod; then
echo running;
else
mongod;
fi
exit 0;
And in your Gruntfile:
shell: {
mongo: {
command: "sh startMongoIfNotRunning.sh",
options: {
async: true
}
},
}
Edit - original version below
Ok - I think this is working properly...
create a shell script which will start mongod if it's not running... save it somewhere, probably in your project. I named it startMongoIfNotRunning.sh :
# this script checks if the mongod is running, starts it if not
`ps -A | grep -q '[m]ongod'`
if [ "$?" -eq "0" ]; then
echo "running"
else
mongod
fi
You may have to make it executable: chmod +x path/to/script/startMongoIfNotRunning.sh
Install grunt-shell-spawn : npm install grunt-shell-spawn --save-dev
Then in your Gruntfile add this:
shell: {
mongo: {
command: "exec path/to/script/startMongoIfNotRunning.sh",
options: {
async: true
}
},
}
(If you're using yeoman, using <%= yeoman.app %> didn't work because those paths are relative to the whole project, so you get something like 'app' instead of the whole path to the script. I'm sure you could get it working, I'm just not aware how to get the path to )
If you just execute the task grunt shell:mongo mongod will start but I wasn't able to close it using grunt shell:mongo:kill. However, assuming you're using a blocking task later (I'm using watch) then it should automatically be killed when you end that task.
Hope this helps someone!
I found your solution really helpful, but actually wanted to kill mongod when restarting grunt server. So I got this:
#!/bin/sh
# this script checks if the mongod is running, kills it and starts it
MNG_ID="`ps -ef | awk '/[m]ongod/{print $2}'`"
if [ -n "$MNG_ID" ]; then
kill $MNG_ID
fi
mongod
which works really nice on my mac. And my grunt file looks like this:
//used to load mongod via shell
shell: {
mongo: {
command: './mongo.sh',
options: {
async: true
}
}
}
So my mongo.sh is in the same location as my Grunfile.js
Cheers
The other two answers are correct. However for completeness here is the equivalent batch script on Windows. Save the following as startMongoIfNotRunning.bat:
tasklist /fi "imagename eq mongod.exe" |find "=" > nul
if errorlevel 1 mongod
If there is a task running called mongod.exe then the = character should appear in the output - hence if it is not running the = character will not be found and the errorlevel variable will be set to 1.
The rest is the same as #MaxBates answer.

Where do npm background scripts go?

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.

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