Recently in my work I had this node.js script that I had to run persistently. Its basically a server that will generate images based on some JSON data that’s passed from the client side. So I did some searching and found Supervisor, a process control system. It allows you to run programs persistently.
You can install install Supervisor by executing the following command in your terminal:
Once the installation is done, you can now create the config file. This is where you specify which script you want to run, the directory in where you want to run it, and a log file in which the output is redirected.
Here’s what a config file looks like:
1 2 3 4 5
Breaking it down. This is where we set the name of the program. Yes the
program: is always there, only the thing that comes after it is updated. In this case the name of the program that I gave is
Next is the command that you execute when you’re running the program in the terminal. In this case were executing the script via the
Next is the directory where the program is stored. This can also be the directory where you want to execute the program:
This is where you specify the file where you want to redirect the output of the program:
Lastly, we specify whether to send back the
stderr output to supervisord on its stdout file descriptor:
That’s pretty much all we need for the configuration file. You can go ahead and save it. If you want to specify more settings, check out the docs on configuration
Adding the Process
Now that we have a configuration file in place we can now tell supervisor to add it to the list of processes that it currently manages. You can do that by using
Executing the command above will let you enter the supervisor program. Next execute the following commands in order:
1 2 3
Breaking it down:
rereadtells supervisor to read the configuration files that are available.
addtells supervisor to add the program into the list of programs that it will manage.
starttells supervisor to run the program.
That’s it! Supervisor is a neat little program that allows you to run programs persistently. Just be sure that errors are handled accordingly because supervisor wouldn’t continue running your program if an error occurs while its running.