今天尝试将自己的python程序以服务的形式部署到linux下
主要步骤如下:
1.code:
__author__ = 'xxx'
import os
import datetime
import time
def run():
fp = open(os.path.dirname(__file__)+"/log.txt","a")
fp.write("hello test service %s \r\n" % str(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))))
fp.close()
if __name__ == "__main__":
print "hello service"
print(str(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))))
while True:
run()
time.sleep(5)
import os
import datetime
import time
def run():
fp = open(os.path.dirname(__file__)+"/log.txt","a")
fp.write("hello test service %s \r\n" % str(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))))
fp.close()
if __name__ == "__main__":
print "hello service"
print(str(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))))
while True:
run()
time.sleep(5)
2.rz 上传到自己的linux服务器上
3.在服务器上的/etc/init.d 创建一个服务脚本
touch helloworld
vim helloworld
编辑里面的内容:
# @author:luis
# chkconfig:35 85 15
# description:this is a test service
#!/bin/python /var/testf/mypys.py
# chkconfig:35 85 15
# description:this is a test service
#!/bin/python /var/testf/mypys.py
注意上面的冒号一定要,要不然会出现
service myservice does not support chkconfig 的错误
上面是linux下服务脚本的格式
4.添加服务到系统
chkconfig --add helloworld
5.检查服务是否添加成功
chkconfig --list |grep helloworld
chkconfig --level 35 helloworld on # 修改服务的默认启动等级