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

   2023-02-10 学习力0
核心提示: 原文:https://www.digitalocean.com/community/tutorials/how-to-install-and-manage-supervisor-on-ubuntu-and-debian-vps-----------UbuntuDebianSystem ToolsPublished onJuly 23, 2013 514kviewsIntroductionIn many VPS environments, it is often t

 

原文:https://www.digitalocean.com/community/tutorials/how-to-install-and-manage-supervisor-on-ubuntu-and-debian-vps

-----------

UbuntuDebianSystem Tools

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.

 
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • 【树莓派】树莓派(Debian)- root用户无法使用SSH登录
    【树莓派】树莓派(Debian)- root用户无法使用
    在树莓派4B上安装了最新的Debian64位系统默认账户密码:pi/raspberryroot/    ------无密码(通过sudo passwd root修改root密码后)问题root修改密码后还是无法登录解决这个系统默认不允许root使用SSH登录登录root用户su 打开配置文件 nano /etc/ssh/sshd_
    03-08
  • windows10环境下安装Linux子系统---debian
    windows10环境下安装Linux子系统---debian
    windows10环境下安装Linux子系统---debian一、前提1、在控制面板-程序-启用与关闭Windows功能中,勾选“适用于Linux的Windows子系统”  2、首先需要创建一个文件夹,用来存放子系统,在需要的位置创建即可,文件夹名任意3、手动下载Windows子系统发行版包,
    03-08
  • Debian 环境安装新版 nginx
    Debian 环境安装新版 nginx
    在 Debian 系统中,我们可以通过 apt-get 安装系统自带的 nginx,这样安装的 nginx 版本略旧。Nginx 官网提供了一些编辑绎好的 deb 安装包,我们只需更新安装源,就可以通过 apt-get 来安装最新的稳定版 Nginx 了。 加载安装源并导入key$ echo deb http://ng
    03-08
  • 使用apt-mirror建立本地debian仓库源
     先介绍一下环境:主机:Win7虚拟机:VirtualBox + Debian7由于软件源的体积比较大,所以我又给虚拟机添加了一块50GB的虚拟硬盘(给虚拟机添加虚拟硬盘的方法参见:http://www.cnblogs.com/pengdonglin137/p/3366589.html , 其中介绍了如何在Vmware和Virtua
    03-08
  • Debian其实有提供附带了各种桌面的安装镜像
    我之前试着装Debian,但它的安装程序我感觉很难用,装上去了之后也有许许多多的问题,比如中文不显示。今天我发现带Live CD的Debian镜像有带了各个桌面的版本,于是我就试着下载KDE版本的Debian。由于我房间的WLAN质量不佳,用500kb/s的速度下了几个小时,那
    02-10
  • Debian镜像使用帮助 Debian镜像下载
    Debian镜像使用帮助 Debian镜像下载
    http://mirrors.163.com/.help/debian.html
    02-10
  • Debian 11 安装Nvidia闭源驱动
    目录通过APT安装Nvidia驱动为Nvidia驱动注册Secure Boot参考文档本人的系统是Debian11,最近一阵子在捣鼓用apt安装英伟达的闭源驱动,同时支持Secure Boot,查阅了Debian Wiki之类的资料之后,在这里整理一下。通过APT安装Nvidia驱动首先,需要确保你的Debian
    02-10
  • Debian时区和时间自动同步
    时区和时间自动同步(1)时间设置及其同步#date  -s 07/26/2005 //2005年7月26日    //修改系统日期时间为当前正确时间#date -s 11:12:00     //11点12分0秒#vim /etc/default/rcS  //设定 BIOS 时间使用 UTC 时区将选项 UTC 的值设定成 yes
    02-10
  • debian/ubuntu系统vi无法删除字符的解决办法
    之前在 Linux 下操作,一直使用的是 Centos 系统,使用 vi 编辑命令一直很顺畅。 最近,入手了一台 debian 操作系统的 vps。在操作 vi 命令时,发现当输入 i 要进行文件编辑时,上下左右的光标无法移动,屏幕上总会出现字符,而且 backspace 只能后退,无法
    02-10
  • Debian安装JAVA环境 debian安装jdk11
     http://blog.csdn.net/gongora/archive/2009/05/15/4190469.aspxDebian官方没有维护专门的Java软件包,所以不能直接用apt-get工具来安装。在Debian系统中要安装Java,有两种方式,一种是用传统方式;一种是Debian方式。1. 传统方式在 sun 下载了最新的 JDK
    02-10
点击排行