分享好友 系统运维首页 频道列表

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

Debian教程  2023-02-10 00:560

 

原文: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.

查看更多关于【Debian教程】的文章

展开全文
相关推荐
反对 0
举报 0
评论 0
图文资讯
热门推荐
优选好物
更多热点专题
更多推荐文章
使用apt-mirror建立本地debian仓库源
 先介绍一下环境:主机:Win7虚拟机:VirtualBox + Debian7由于软件源的体积比较大,所以我又给虚拟机添加了一块50GB的虚拟硬盘(给虚拟机添加虚拟硬盘的方法参见:http://www.cnblogs.com/pengdonglin137/p/3366589.html , 其中介绍了如何在Vmware和Virtua

0评论2023-03-08841

Debian其实有提供附带了各种桌面的安装镜像
我之前试着装Debian,但它的安装程序我感觉很难用,装上去了之后也有许许多多的问题,比如中文不显示。今天我发现带Live CD的Debian镜像有带了各个桌面的版本,于是我就试着下载KDE版本的Debian。由于我房间的WLAN质量不佳,用500kb/s的速度下了几个小时,那

0评论2023-02-10935

Debian 11 安装Nvidia闭源驱动
目录通过APT安装Nvidia驱动为Nvidia驱动注册Secure Boot参考文档本人的系统是Debian11,最近一阵子在捣鼓用apt安装英伟达的闭源驱动,同时支持Secure Boot,查阅了Debian Wiki之类的资料之后,在这里整理一下。通过APT安装Nvidia驱动首先,需要确保你的Debian

0评论2023-02-10878

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

0评论2023-02-10415

debian/ubuntu系统vi无法删除字符的解决办法
之前在 Linux 下操作,一直使用的是 Centos 系统,使用 vi 编辑命令一直很顺畅。 最近,入手了一台 debian 操作系统的 vps。在操作 vi 命令时,发现当输入 i 要进行文件编辑时,上下左右的光标无法移动,屏幕上总会出现字符,而且 backspace 只能后退,无法

0评论2023-02-10670

Debian安装JAVA环境 debian安装jdk11
 http://blog.csdn.net/gongora/archive/2009/05/15/4190469.aspxDebian官方没有维护专门的Java软件包,所以不能直接用apt-get工具来安装。在Debian系统中要安装Java,有两种方式,一种是用传统方式;一种是Debian方式。1. 传统方式在 sun 下载了最新的 JDK

0评论2023-02-10676

在阿里云主机的Debian操作系统上安装Docker
因为需要新搭建饭团网站,所以需要在阿里云的主机上跑数据库,java环境。考虑到可扩展性和模块化,所以准备最近流行的docker技术。Docker —— 从入门到实践阿里云主机1核1G,资源不多,所以就装debian了。欢迎捐助 ????## 下面命令都是以root用户执行## 查看

0评论2023-02-10342

debian下配置keepalived ha
抄袭自http://blog.51yip.com/server/1417.html,做了一些修改可以参考http://blog.linuxphp.org/archives/1615/ 备注:NAT模式在rs机器上不需要做关于ip之类的任何配置,以下为DR模式,ip tunnerl模式也需要在rs上配置vip和router,ip tunnel和DR模式不允许

0评论2023-02-10447

Debian6显卡驱动安装 debian n卡驱动
2011年2月6日,Debian6的stable版代号squeeze发布。 Debian6的默认显卡驱动是Nouveau,这是个开源的显卡驱动,就我在我这台机器运行看来,性能相当不错,移动窗口快速屏幕内容刷新都相当快,可惜的是,它不知道是不支持3D加速还是对3D加速的支持不好,我无法

0评论2023-02-10820

ubuntu12.04下使用qemu模拟mips处理器安装debian
注:ubuntu是不支持mips处理器的,只能在x86下安装运行第一步、安装qemusudo apt-get install qemu qemu-system 。执行 qemu-system-mips --version 发现版本太低,因为后面需要更高版本的qemu。 下载http://wiki.qemu-project.org/download/qemu-2.1.2.tar.

0评论2023-02-10367

debian7配置 debian7安装教程
输入法: apt-get install ibus ibus-pinyin 并执行ibus-setup进行配置,首选项-》输入法-》中文,然后按添加按钮即可。软件开发基本软件:apt-get install gcc gdb g++ automake autoconf libtool vim vim-gtk emacs git subversion git-svn flex bison嵌入式

0评论2023-02-10365

Debian 11 配置优化指南
原文地址: Debian 11 配置优化指南 - WindSpiritIT0x00 简介本文仅适用于配置 Debian 11 Bullseye文中同时包含 Gnome 桌面和 KDE 桌面配置,其中大部分相同,不同之处分别列出Gnome 桌面相关配置更新滞后,可能不再更新KDE 及 Gnome 优化配置部分请根据个人喜

0评论2023-02-10353

Debian rsyslog.conf Linux man page
NAME rsyslog.conf - rsyslogd(8) configuration fileDESCRIPTION The rsyslog.conf fileisthemainconfigurationfileforthe rsyslogd(8) which logs system messageson*nixsystems. Thisfile specifiesrules for logging.For special features see the rsyslo

0评论2023-02-10652

Debian 6.0 控制台爱好者的个性化设置
看到 Debian Squeeze 正式发布的消息,有点激动,马上下载了一个替换了虚拟机里的 Debian 5.0,由于很久没用Debian了,感觉有点不太适应了,把系统设置成自己习惯的模式都费了半天劲,特别记录一下。1.GRUB设置    还是不太喜欢图形化的GRUB菜单,简单处理

0评论2023-02-101000

更多推荐