Sifars - Top Mobile, Web and Software Development Company
Best way to handle background processes in linux

Best way to handle background processes in linux

sifars
sifars
,5 years ago
0
< 1 mins
2241

Every programmer soon comes to a point where he requires to setup several jobs to run in the background with 100% uptime. For example a website server, email server, job schedulers etc.

These processes are more like a daemon process. And as like any other job, these tend to stop due to mishandling or uncatchable exceptions/errors. Rather to put a person to sit and constantly monitor the jobs, we make use of the Supervisor.

A Supervisor is a process control system that will start, stop and monitor all our processes and will restart any, that fails.

To install Supervisor on Ubuntu:

sudo apt-get install supervisor

Configuring Supervisor

Let say we have a command that should run indefinitely:

python run_this_command

Supervisor configuration files are stored in the /etc/supervisor/conf.d directory. Let’s create a new configuration file as monitor-my-process.conf.

Inside monitor-my-process.conf write:

[program:my-worker]
process_name=%(program_name)s_%(process_num)02d
command=python run_this_command
autostart=true
autorestart=true
user=linux_user
numprocs=8
redirect_stderr=true
stdout_logfile=/home/linux_user/app.com/worker.log

In this example, the numprocs directive will instruct Supervisor to run 8 processes of my command and monitor all of them, automatically restarting them if they fail. Also change the ‘linux_user’ to the user you want the process to be run as.

Starting Supervisor

Once the configuration file has been created, you may update the Supervisor configuration and start the processes using the following commands:

sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start my-worker:*

Cheers! Your processes will run indefinitely with auto restart.

Bonus!

Few more commands that are likely to be used sometime later:

Stop all supervisor workers:

sudo supervisorctl stop my-worker:*

Restart all supervisor workers:

sudo supervisorctl restart my-worker:*

Get the current status of workers:

sudo supervisorctl status my-worker:*

Related Posts

testimonial_svg

Great Work Speaks for Itself

Time to Craft Brilliance Together

Collaborate with Sifars to turn your ideas into the brilliant digital applications for your business.

Time to add wings to your dreams and make them fly high!

Fill up the simple Form Below

Let's Connect

Thanks for reaching us!

Shaping your ideas and Adding value to your concepts, We aspire to sustain cordial relations by providing utmost satisfaction !!! Get in touch to partner with us.

Name is required
Email is required
Budget range is required
Phone is required
Talk goal is required
Synopsis is required