当前位置:首页 > CN2资讯 > 正文内容

[转]How To Install and Manage Supervisor on Ubuntu and Debian VPS

2天前CN2资讯
How To Install and Manage Supervisor on Ubuntu and Debian VPS UbuntuDebianSystem Tools
  • Published onJuly 23, 2013 514kviews

Introduction

In many VPS environments, it is often the case that you will have a number of small programs that you want to run persistently, whether these be small shell scripts, Node.js apps, or any large-sized packages.

Conventionally, you may write a init script for each of these programs, but this can quickly become time consuming to manage and isn't always particularly transparent for newer users.

Supervisor is a process manager which makes managing a number of long-running programs a trivial task by providing a consistent interface through which they can be monitored and controlled.

This tutorial assumes that you are familiar with the command line, installing packages, and basic server management.

Installation

Installation of Supervisor on both Ubuntu and Debian is incredibly simple, as prebuilt packages already exist within both distributions' repositories.

As the root user, run the following command to install the Supervisor package:

apt-get install supervisor

Once this has completed, the supervisor daemon should already be started, as the prebuilt packages come with an init script that will also ensure the Supervisor is restarted after a system reboot. You can ensure this is the case by running:

service supervisor restart

Now that we have Supervisor installed, we can look at adding our first programs.

Adding a Program

New programs are given to Supervisor through configuration files, which inform it of the executable to run, any environmental variables, and how output should be handled.

Note: All programs run under Supervisor must be run in a non-daemonising mode (sometimes also called 'foreground mode'). If, by default, the program forks and returns on startup, then you may need to consult the program's manual to find the option to enable this mode, otherwise Supervisor will not be able to properly determine the status of the program.

For the sake of this article, we'll assume we have a shell script we wish to keep persistently running that we have saved at /usr/local/bin/long.sh and looks like the following:

#!/bin/bash while true do # Echo current date to stdout echo `date` # Echo 'error!' to stderr echo 'error!' >&2 sleep 1 done chmod +x /usr/local/bin/long.sh

In a practical sense, this script is clearly rather pointless, but it will allow us to cover the fundamentals of Supervisor configuration.

The program configuration files for Supervisor programs are found in the /etc/supervisor/conf.d directory, normally with one program per file and a .conf extension. A simple configuration for our script, saved at /etc/supervisor/conf.d/long_script.conf, would look like so:

[program:long_script] command=/usr/local/bin/long.sh autostart=true autorestart=true stderr_logfile=/var/log/long.err.log stdout_logfile=/var/log/long.out.log

We'll look at the significance of each line and some of the tweaks that may be desirable for your program below:

[program:long_script] command=/usr/local/bin/long.sh

The configuration begins by defining a program with the name 'long_script' and the full path to the program:

autostart=true autorestart=true

The next two lines define the basic automatic behaviour of the script under certain conditions.

The autostart option tells Supervisor that this program should be started when the system boots. Setting this to false will require a manual start command following any system shutdown.

autorestart defines how Supervisor should manage the program in the event it exits and has three options:

  • false' tells Supervisor not to ever restart the program after it exits
  • 'true' tells Supervisor to always restart the program after it exits
  • 'unexpected' tells Supervisor to only restart the program if it exits with an unexpected error code (by default anything other than codes 0 or 2).
stderr_logfile=/var/log/long.err.log stdout_logfile=/var/log/long.out.log

The final two lines define the locations of the two main log files for the program. As suggested by the option names, stdout and stderr will be directed to the stdout_logfile and stderr_logfile locations respectively. The specified directory specified must exist before we start the program, as Supervisor will not attempt to create any missing directories.

The configuration we have created here is a minimal reasonable template for a Supervisor program. The documentation lists many more optional configuration options that are available to fine tune how the program is executed.

Once our configuration file is created and saved, we can inform Supervisor of our new program through the supervisorctl command. First we tell Supervisor to look for any new or changed program configurations in the /etc/supervisor/conf.d directory with:

supervisorctl reread

Followed by telling it to enact any changes with:

supervisorctl update

Any time you make a change to any program configuration file, running the two previous commands will bring the changes into effect.

At this point our program should now be running and we can check this is the case by looking at the output log file:

$ tail /var/log/long.out.log Sat Jul 20 22:21:22 UTC 2013 Sat Jul 20 22:21:23 UTC 2013 Sat Jul 20 22:21:24 UTC 2013 Sat Jul 20 22:21:25 UTC 2013 Sat Jul 20 22:21:26 UTC 2013 Sat Jul 20 22:21:27 UTC 2013 Sat Jul 20 22:21:28 UTC 2013 Sat Jul 20 22:21:29 UTC 2013 Sat Jul 20 22:21:30 UTC 2013 Sat Jul 20 22:21:31 UTC 2013

Success!

Managing Programs

Once our programs are running, there will undoubtedly be a time when we want to stop, restart, or see their status. The supervisorctl program, which we first used above, also has an interactive mode through which we can issue commands to control our programs.

To enter the interactive mode, start supervisorctl with no arguments:

$ supervisorctl long_script RUNNING pid 12614, uptime 1:49:37 supervisor>

When started, supervisorctl will initially print the status and uptime of all programs, followed by showing a command prompt. Entering help will reveal all of the available commands that we can use:

supervisor> help default commands (type help ): ===================================== add clear fg open quit remove restart start stop update avail exit maintail pid reload reread shutdown status tail version To start in a simple manner, we can start, stop and restart a program with the associated commands followed by the program name: supervisor> stop long_script long_script: stopped supervisor> start long_script long_script: started supervisor> restart long_script long_script: stopped long_script: started

Using the tail command, we can view the most recent entries in the stdout and stderr logs for our program:

supervisor> tail long_script Sun Jul 21 00:36:10 UTC 2013 Sun Jul 21 00:36:11 UTC 2013 Sun Jul 21 00:36:12 UTC 2013 Sun Jul 21 00:36:13 UTC 2013 Sun Jul 21 00:36:14 UTC 2013 Sun Jul 21 00:36:15 UTC 2013 Sun Jul 21 00:36:17 UTC 2013 supervisor> tail long_script stderr error! error! error! error! error! error! error!

Using status we can view again the current execution state of each program after making any changes:

supervisor> status long_script STOPPED Jul 21 01:07 AM

Finally, once we are finished, we can exit supervisorctl with Ctrl-C or by entering quit into the prompt:

supervisor> quit

And that's it! You've mastered the basics of managing persistent programs through Supervisor and extending this to your own programs should be a relatively simple task. If you have any questions or further advice, be sure to leave it in the comments section.

    你可能想看:

    扫描二维码推送至手机访问。

    版权声明:本文由皇冠云发布,如需转载请注明出处。

    本文链接:https://www.idchg.com/info/18429.html

    分享给朋友:

    “[转]How To Install and Manage Supervisor on Ubuntu and Debian VPS” 的相关文章

    国内VPS安装Docker的详细步骤与优化技巧

    在决定开始安装Docker之前,首先需要为你的国内VPS做好一些准备工作。准备工作不仅可以帮助我们顺利完成Docker的安装,还能让过程更加高效。 首先,选择一个适合的VPS服务提供商至关重要。目前市场上有很多VPS服务商,例如阿里云、腾讯云、Linode等。在选择时,可以根据自己的需求考虑价格、性...

    探索诸暨市:地理特征、气候与经济发展全面分析

    我发现诸暨市,这个位于浙江省中北部的县级市,真是一个令人着迷的地方。它东靠嵊州市,南面与东阳、义乌和浦江相邻,西面与桐庐和富阳相接,北边则与柯桥和萧山为界。这样的地理位置赋予了诸暨市独特的区域特色,方便了与周边城市的交流与发展。 在谈到诸暨的地理特征时,不得不提其独特的地形地貌。诸暨市位于浙东南和浙...

    香港低价服务器:经济实惠的选择与优势解析

    在如今数字化迅猛发展的时代,香港低价服务器凭借其独特优势,吸引了无数创业者、站长和企业用户的青睐。何为香港低价服务器?这类服务器主要是指在香港地区提供的,价格相对较低的服务器租用服务。由于其经济实惠的特性,许多小型企业和个人用户在选择服务器时,都会优先考虑这种选项。 在选择网络服务时,速度和价格往往...

    选择合适的国外域名与邮箱服务指南

    在现代社会,跨国沟通和商务往来变得愈加频繁,国外域名与邮箱成为不可或缺的工具。当我开始接触这些服务时,首先意识到国外域名的定义与特点,它们不仅代表着一个网站的身份,还是全球互联网上各类信息交流的桥梁。国外的域名通常以.com、.net等后缀结尾,这些域名能够覆盖广泛的用户群体,使得沟通更为流畅。 我...

    CMI香港:助力企业洞察市场与消费者需求的关键工具

    在了解CMI香港之前,首先需要弄清楚CMI的定义与作用。CMI,即客户市场信息(Customer Market Insight),专注于帮助企业深入理解市场动态与消费者需求。简单来说,CMI就像是企业在市场中找到导航指南,确保它们能够精准地把握客户的期望、习惯及其变化。 当我们把视角转向香港,相信大...

    黑五VPS促销: 省钱又省心的选择指南

    在这个数字化的时代,VPS(虚拟专用服务器)成为越来越多业务和个人的选择。VPS其实是一种独立的虚拟服务器,它通过物理服务器上的虚拟化技术进行资源分隔。因此,不同用户可以在同一台物理服务器上,拥有独立的操作空间。想象一下,你可以像使用传统的独立服务器一样,享受更高的性能和安全性,同时又不需要承担独立...