Hey everyone we are aware of one thing Node js apps runs only after we start the node services but still it will stop once we close the terminal. Moreover this happens even in the local system itself and what will happen if this happens when ur using the Virtual Machines.
Have you ever faced the issues like the Virtual Machine throws up error like
“ client_loop: send disconnect: Broken pipe “
Yes, this happens always while working with cloud and this is where the package Forever Js comes in handy.
This is going to be where I will uncover all the useful commands which are required for Forever Js. Now let’s get started, but before getting started let install forever js.
Code is as follows,
sudo apt-get update
npm i forever -g
The next step is to create a test node js application which we will use for this forever application.
Guys here I am using an EC2 Server instance and as usual I just grabbed whatever is under the free tier limit i.e. ubuntu or any linux based OS.
Here is the code, first create a server.js file
Once you do this just verify whether it is working fine or not
Now visit the browser with the IP which is assigned to your EC2 instance so that you can verify that as shown below
Alright, I guess we now have our beloved server running successfully.
Now all that is left is just to make use of the forever js codes.
Usually, we start our server by running the code as node server.js but here there will be a small change using the code while running and the code is as follows,
forever start server.js
Alright now we can verify using the forever list command
This command is actually used for listing all the scripts which are running forever and it doesn’t stop here. It also shows us the location, process id, up-time and also the details of the log file name of each script that’s running.
The next command is the stop, yes if there is a start then there will be a stop defenitely.
We can stop using the file name of the script you want to stop.
Code is as follows,
forever stop server.js
With this command you have to reach the specific location of the file where the node application is running and only then stop or else it will not work properly.
Alright, what if you want to stop a file based on uid.
Forever stop 0
Well, whenever you start or else list using the forever command you will be displayed with an output of whichever script is running with a uid. So we can also stop using those commands rather than typing out the name of the script file. You can verify by using the start or list command. Now type the following commands,
forever stop o
Even here you can see the uid which I spoke about.
Alright if you wish to stop everything that’s running simple stopall, but there are two commands one is forever command and the other one is usual node command.
If you wish to find the logs then you can just use the following code
It will list the path of each and every script which is running along with the uid.
Now that we stop the forever script and verify our browser and it should throw up the following screen,
For this, you can use any commands i.e. the normal node command or forever command,
Now you can use the forever start command and close the EC2 instance terminal and then verify for yourself.
I guess I have given details of most of the Forever js Commands and the rest of the few other commands I will give you guys and do try it out.
forever restart server.js
forever restart 0 –> restarts uid o script alone
forever restartall –> restarts all running forever scripts
forever -h –> -h, –help: Command line help information
forever config –> lists all forever user configurations
forever clear –> clears the specified forever config by its key
forever cleanlogs –> deletes all historical forever log files
forever set –> defines a forever config value for a unique key
You can find the server.js file in my GitHub.