一段linux启动脚本(python版)

公司项目用到需要开机自动执行相关程序,奈何shell不是很精通,老大给了段python版的,我也分享下,供大家参考

#!/usr/bin/env python

# init.d script for the Mytut CS Repo Server; John Wang 14 Feb 2011

import sys,subprocess,time

cmmd    = '/home/fred/test.py'#启动/关闭执行命令

port    = '12600'

decp    = 'start process program....'

cmmdln  = ['nohup',cmmd,'-p',port]

def start():

print 'Starting %s...' % decp

subprocess.Popen(cmmdln)

print 'Started'

def stop():

print 'Stopping %s...' % decp

subprocess.Popen(cmmdln)

print 'Stopped'

return 0

def restart():

if stop()==1:

print 'Stopping error. Maybe already stopped ?. starting anyway ...'

else:

time.sleep(1)

start()

def run(argv):

fns={

    'start':start,

    'stop':stop,

    'restart':restart,

    'reload':restart,

}

fn=fns.get(argv[1],None) if len(argv)>1 else None

if not fn:

print >> sys.stderr,"Usage: %s {%s}" % (argv[0],'|'.join(fns.keys()))

sys.exit()

fn()

if __name__ == '__main__':

run(sys.argv)

比较简单~

前文介绍过,放在/etc/rc3.d 目录下即可,linux启动后会自动执行这个目录里面的脚本,默认使用start参数启动

评论关闭。