windows及linux下手动添加删除服务
很多时候进入某些目录去执行命令是非常麻烦的事情,特别是一些需要加大量参数的命令,而且当你想加入开机或者其他之类的任务时会发现更要命,这时候手动将这些带参数的命令添加为服务会使你轻松许多,步入正题。
一、windows
1. 手动添加服务
使用系统的sc命令(详情参见microsoft官方:http://msdn.microsoft.com/zh-cn/bb490995),语法
sc [<ServerName>] create [<ServiceName>] [type= {own | share | kernel | filesys | rec | interact type= {own | share}}] [start= {boot | auto | demand | disabled}] [error= {normal | severe | critical | ignore}] [binpath= <BinaryPathName>] [group= <LoadOrderGroup>] [tag= {yes | no}] [depend= <dependencies>] [obj= {<AccountName> | <ObjectName>}] [displayname= <DisplayName>] [password= <Password>]
参数简要说明(仅选取常用的作说明,详情见上面的官方文档):
[ServiceName]为服务名,为必选项;
[type]为服务类别,可选,默认own
[start]为启动类型,默认为demand,boot延迟启动,auto自动启动,demand手动启动,disabled为禁用
[binpath]为执行文件路径,默认为空,可选
[depend]为依存关系,默认为空,可选
[displayname]为显示名称,可选,如果不填写默认为服务名
示例
sc create mongodb binpath= "D:\mongodb\bin\mongod.exe --config d:\mongodb\mongodb.cfg --service" start= auto displayname= "MogoDB"
*注意等号和值之间必须有一个空格
完成后你就可以使用net [stop|start|pause] mongodb进行管理了
补充说明:
对于windows而言可能我们还希望添加服务描述,因为时间长了保不定就忘记了这个是干嘛的,如图
可以使用
sc [ServerName] description [ServiceName] [Description]
示例
sc descritption mongodb "MongoDB服务"
2. 手动修改服务
同样使用sc命令,语法
sc [<ServerName>] config [<ServiceName>] [type= {own | share | kernel | filesys | rec | interact type= {own | share}}] [start= {boot | auto | demand | disabled}] [error= {normal | severe | critical | ignore}] [binpath= <BinaryPathName>] [group= <LoadOrderGroup>] [tag= {yes | no}] [depend= <dependencies>] [obj= {<AccountName> | <ObjectName>}] [displayname= <DisplayName>] [password= <Password>]
参数说明同上
示例
sc config mongodb binpath= "E:\mongodb\bin\mongod.exe --config E:\mongodb\mongodb.cfg --service" start= demand displayname= "MongoDB服务"
3. 手动删除服务
还是sc命令,语法
sc [ServerName] delete [ServiceName]
示例
sc delete mongodb
二、linux
1. 手动添加服务
使用chkconfig命令,当然前提还需要自己去编写服务脚本(linux的服务本来就都是脚本文件,可以是shell也可以是python、perl、php等),语法
chkconfig --add <name>
参数说明
<name> 为服务名,其实就是上面说的服务脚本名,必选
示例
以下使用Python示例,添加mongodb-server服务:
#! /usr/bin/python # # chkconfig: - 84 16 # description: MongoDB Service import sys,subprocess,os prog = 'mongodb-server' pidfile = '/var/run/%s.pid' % prog lockfile = '/var/mongodb/%s.lock' % prog def start(): if os.path.exists(lockfile): print 'Sorry,%s is lock!'% prog return print "Starting %s: " % prog, cmd = '/usr/local/mongodb/bin/mongod --config /etc/mongodb/mongodb.conf' process = subprocess.Popen(cmd,shell = True,stdin = subprocess.PIPE,stdout = subprocess.PIPE,stderr = subprocess.PIPE) if 'started successfully' in process.stdout.read(): dir = os.path.dirname(lockfile) if not os.path.exists(dir): os.makedirs(dir) open(lockfile, 'w+').close() print "\033[22;32m[ok]\033[0m" else: print "\033[22;31m[fail]\033[0m" def stop (): print "Stop %s: " % prog, if os.path.exists(pidfile): fp = open(pidfile,'r+') pid = fp.read() fp.close() cmd = 'kill %s' % pid subprocess.Popen(cmd,shell = True,stdin = subprocess.PIPE,stdout = subprocess.PIPE,stderr = subprocess.PIPE) try: os.unlink(pidfile) os.unlink(lockfile) except Exception,e: pass print "\033[22;32m[ok]\033[0m" else: print "\033[22;31m[fail]\033[0m" def status(): print "Service %s status: " % prog, try: fp = open(pidfile,'r') pid = fp.readline() fp.close() state = 'unknow' fp = open('/proc/%s/status' % int(pid),'r') for f in fp: if 'State' in f: state = f.replace('State:','') break print state except Exception,e: print 'stop' def restart (): stop () start() def help (): print 'Usage: %s {start|stop|status|restart}' % prog def main (): arg = '' for v in sys.argv[1:]: if v: arg = v break if arg in ('start','stop','status','restart'): fun = eval(arg) fun() else: help() if __name__ == '__main__': main()
*上面#chkconfig(服务运行等级别设置)和#description(服务描述信息)是必须的,否则加入chkconfig会提示不支持
将上面的文件保存为mongodb-server(没错不需要.py后缀,本来就是将其作为shell脚本运行),然后将该脚本放置 /etc/rc.d/init.d/目录下,添加执行权限
chmod +x mongodb-server
然后加入系统服务
chkconfig --add mongodb-server
完成后你就可以使用chkconfig和service管理了,例如修改为开机自动启动
chkconfig mongodb-server on
补充说明:
如果你是windows下编辑的运行时可能会提示env: /etc/init.d/mongodb-server : no such file or directory
排除文件不存在和可执行权限的情况,可以断定是由于windows和linux换行差异造成脚本无法识别,如果使用的是Editplus可以点击右下角的pc切换为Unix换行方式,如下图:
如果使用的是UltraEdit可以选择 文件->转换->DOS TO Unix;Notepad++可以选择 编辑->空白操作->转换为UNIX格式。
更改后重新上传覆盖即可。
2. 手动修改服务
这个不用多说,因为本来就是脚本执行,直接修改相应服务的脚本即可
3. 手动删除服务
使用chkconfig命令,语法
chkconfig --del <name>
然后手动删除服务脚本即可,例如删除mongodb-server服务
chkconfig --del mongodb-server rm -f mongodb-server