Ubuntu add custom service(daemon)

Task

需要在系统启动的时候自动启动一个服务(后台程序),在系统关闭的时候关闭服务。

比如在部署某个应用之前,需要将某个任务设置成后台程序(daemon),而这些* 任务 *可以是Liunx command, 或者自己写的脚本。

比如在部署我的Django应用之前,需要启动MQ服务,一种可能的方式是:需要执行python manage.py runMQServer

Why dont use /etc/init.d

/etc/init.d是陈旧的方式,目前已经被淘汰,取而代之的是upstart

使用upstart方式添加后台程序配置更容易

/etc/init/run_mq.conf

以task中的需求为例,创建daemon的配置文件run_mq.conf:

# TMS MQ Service

description "TMS message queue server"
author "xxx <xxx@xxx.com>" #the job is started automatically when the machine is first started (without writable filesystems or networking).
start on startup #the job is stoped automatically when the machine is shut down
stop on shutdown #a job that exits quietly transitions into the stop/waiting state, no matter how it exited.
respawn #respawn the job up to 2 times within a 5 second period.
#If the job exceeds these values, it will be stopped and
#marked as failed.
respawn limit 2 5 #controls where a job's output goes, and where its input comes from
console ouput #additional shell code can be given to be run before the binary or script,it is intended for preparing the environment
pre-start script
#prepare environment
end script script
if [ -f /home/tms/TMS/tmsApp/management/commands/runMSGServer.py ]; then
python /home/tms/TMS/manage.py runMSGServer
fi
end script #additional shell code can be given to be run after the binary or script,it is intended for cleaning up afterwards
post-stop script
#clean up or else
logger "TMS message queue server started..."
end script

references

How to correctly add a custom daemon to init.d?

Upstart getting started

Add custom daemon on Linux System的更多相关文章

  1. Linux System and Performance Monitoring

    写在前面:本文是对OSCon09的<Linux System and Performance Monitoring>一文的学习笔记,主要内容是总结了其中的要点,以及加上了笔者自己的一些理解 ...

  2. Add custom and listview web part to a page in wiki page using powershell

    As we know, Adding list view web part is different from custom web part using powershell, what's mor ...

  3. The type 'Expression<>' is defined in an assembly that is not referenced.You must add a reference to assembly 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

    在我将一个.net framework 4.0+mvc4+ef5的项目,升级到.net framework 4.6.1+mvc5+ef6之后,解决了所有的升级带来的问题,唯独在razor的cshtml ...

  4. Howto Reboot or halt Linux system in emergency (ZT)

    http://www.cyberciti.biz/tips/reboot-or-halt-linux-system-in-emergency.html Linux kernel includes ma ...

  5. [webgrid] – header - (How to Add custom html to Header in WebGrid)

    How to Add custom html to Header in WebGrid MyEvernote Link Posted on March 30, 2013by mtryambake Ho ...

  6. (copy) Shell Script to Check Linux System Health

    source: http://linoxide.com/linux-shell-script/shell-script-check-linux-system-health/ This article ...

  7. The frequent used operation in Linux system

    The frequently used operation in Linux system    2017-04-08 12:48:09  1. mount the hard disk:  #: fd ...

  8. How To Add Custom Build Steps and Commands To setup.py

    转自:https://jichu4n.com/posts/how-to-add-custom-build-steps-and-commands-to-setuppy/ A setup.py scrip ...

  9. The package 'MySql.Data' tried to add a framework reference to 'System.Runtime' which was not found in the GAC

    最近在学习Visual Studio连接mysql EF模型.在nuget中安装mysql.data时总是提示The package 'MySql.Data' tried to add a frame ...

随机推荐

  1. 【刷题】BZOJ 2190 [SDOI2008]仪仗队

    Description 作为体育委员,C君负责这次运动会仪仗队的训练.仪仗队是由学生组成的N * N的方阵,为了保证队伍在行进中整齐划一,C君会跟在仪仗队的左后方,根据其视线所及的学生人数来判断队伍是 ...

  2. 【BZOJ4006】【JLOI2015】管道连接

    Description 传送门 Solution 题目要求相同颜色的点必须在一个连通块中,但会有多个颜色同属一个连通块使得解更优的情况. 想一想DP能否行得通:设\(g_i\)表示已考虑颜色状态为\( ...

  3. debian修改默认编辑器

    刚才在一台机器上打开 crontab -e,跳出来的编辑器是nano,太难使... 在debian下是使用 update-alternatives 命令修改默认编辑器. 先查看一下使用帮助 # upd ...

  4. Ld, -rpath, -rpath-link

    http://blog.csdn.net/xph23/article/details/38157491

  5. Python之旅:数据类型、字符编码、文件处理

    一 引子 1 什么是数据? x=10,10是我们要存储的数据 2 为何数据要分不同的类型 数据是用来表示状态的,不同的状态就应该用不同的类型的数据去表示 3 数据类型 以下每个类型都是有详细介绍链接的 ...

  6. flask-migrate库的使用

    在使用flask-SQLAlchemy库的时候,经常苦恼于该库的creat_all()方法不能对项目中的新建库进行修改,drop_all()又会对数据进行删除,这非常的不方便,万幸,Flask-SQL ...

  7. kubernetes 1.9部署实践

    目录 简要说明 环境说明 安装前的约定 配置etcd 生成相关证书 证书类型说明 cfssl配置 证书相关配置 生成ca证书 生成kubernetes证书 生成kubectl证书 生成kube-pro ...

  8. HTML常用标签-<head>内常用标签

    HTML常用标签-<head>内常用标签 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.HTML是什么     1>.超文本标记语言(Hypertext Ma ...

  9. Lena与数字图像处理

    在数字图像处理中,Lena(Lenna)是一张被广泛使用的标准图片,特别在图像压缩的算法研究中. 黑白Lena图   标准Lena (为什么用这幅图,是因为这图的各个频段的能量都很丰富:即有低频(光滑 ...

  10. 对硬盘进行分区时,GPT和MBR有什么区别?

    在Windows 8或8.1中设置新磁盘时,系统会询问你是想要使用MBR还是GPT分区.GPT是一种新的标准,并在逐渐取代MBR. GPT带来了很多新特性,但MBR仍然拥有最好的兼容性.GPT并不是W ...