一、脚本服务化目的

1、python 在 文本处理中有着广泛的应用,为了满足文本数据的获取,会每天运行一些爬虫抓取数据。但是网上买的服务器会不定时进行维护,服务器会被重启。这样我们的爬虫服务就无法运行。这个时候我们可以把python脚本服务化,服务器重启后,脚本就会自动运行。解决服务器维护后需要手动运行python脚本。

2、实现方法:

给编写好的python脚本开头加上

  1. #!/usr/bin/python

3、启动shell 脚本 编写

vi  pystock.sh

  1. #vim /etc/init.d/httpd
  2. #!bin/bash
  3. lock="py_stock.py"
  4.  
  5. #启动服务方法
  6. start(){
  7. echo "service start...."
  8. su root -c "python /root/python/py_stock/src/crawler/py_stock.py &"
  9. }
  10.  
  11. #停止服务方法
  12. stop(){ echo "service stop...." pkill -f $lock}
  13.  
  14. #查看服务状态
  15. status(){
  16. if [ -e $lock ];then
  17. echo "$0 service start"
  18. else
  19. echo "$0 service stop"
  20. fi
  21. }
  22.  
  23. #重新启动
  24. restart(){
  25. stop
  26. start
  27. }
  28. case "$1" in
  29. "start")
  30. start
  31. ;;
  32. "stop")
  33. stop
  34. ;;
  35. "status")
  36. status
  37. ;;
  38. "restart")
  39. restart
  40. ;;
  41. *)
  42. echo "$0 start|stop|status|restart"
  43. ;;
  44. esac

3、复制脚本到/etc/init.d/目录下:cp pystock.sh /etc/init.d/

4、给shell脚本赋予执行权限 :chmod +x /etc/init.d/pystock.sh

5、添加服务:chkconfig --add pystock.sh

6、设置服务为开机启动:chkconfig --level 35 pystock.sh on

出现的问题:

当我运行shell 脚本启动python脚本时,提示我一下错误

  1. syntax error near unexpected token `$'{\r''

这是因为window 下换行是\r\n,linux 下换行是\n。我在window下编写的shell 脚本拷贝到linux上。shell命令解读时会先解读/r,在解读后面的脚本导致报错。而且这个\r 在Linux上是看不到的。

解决方法:

  1. sed 's/\r//' 原文件 >转换后文件

二、参考:http://0pointer.de/public/abrtd

  1. #!/bin/bash
  2. # Starts the abrt daemon
  3. #
  4. # chkconfig: 35 82 16
  5. # description: Daemon to detect crashing apps
  6. # processname: abrtd
  7. ### BEGIN INIT INFO
  8. # Provides: abrt
  9. # Required-Start: $syslog $local_fs
  10. # Required-Stop: $syslog $local_fs
  11. # Default-Stop: 0 1 2 6
  12. # Default-Start: 3 5
  13. # Short-Description: start and stop abrt daemon
  14. # Description: Listen to and dispatch crash events
  15. ### END INIT INFO
  16.  
  17. # Source function library.
  18. . /etc/rc.d/init.d/functions
  19. ABRT_BIN="/usr/sbin/abrtd"
  20. LOCK="/var/lock/subsys/abrtd"
  21. OLD_LOCK="/var/lock/subsys/abrt"
  22. RETVAL=0
  23.  
  24. #
  25. # Set these variables if you are behind proxy
  26. #
  27. #export http_proxy=
  28. #export https_proxy=
  29.  
  30. #
  31. # See how we were called.
  32. #
  33.  
  34. check() {
  35. # Check that we're a privileged user
  36. [ "`id -u`" = 0 ] || exit 4
  37.  
  38. # Check if abrt is executable
  39. test -x $ABRT_BIN || exit 5
  40. }
  41.  
  42. start() {
  43.  
  44. check
  45.  
  46. # Check if it is already running
  47. if [ ! -f $LOCK ] && [ ! -f $OLD_LOCK ]; then
  48. echo -n $"Starting abrt daemon: "
  49. daemon $ABRT_BIN
  50. RETVAL=$?
  51. [ $RETVAL -eq 0 ] && touch $LOCK
  52. echo
  53. fi
  54. return $RETVAL
  55. }
  56.  
  57. stop() {
  58.  
  59. check
  60.  
  61. echo -n $"Stopping abrt daemon: "
  62. killproc $ABRT_BIN
  63. RETVAL=$?
  64. [ $RETVAL -eq 0 ] && rm -f $LOCK
  65. [ $RETVAL -eq 0 ] && rm -f $OLD_LOCK
  66. echo
  67. return $RETVAL
  68. }
  69.  
  70. restart() {
  71. stop
  72. start
  73. }
  74.  
  75. reload() {
  76. restart
  77. }
  78.  
  79. case "$1" in
  80. start)
  81. start
  82. ;;
  83. stop)
  84. stop
  85. ;;
  86. reload)
  87. reload
  88. ;;
  89. force-reload)
  90. echo "$0: Unimplemented feature."
  91. RETVAL=3
  92. ;;
  93. restart)
  94. restart
  95. ;;
  96. condrestart)
  97. if [ -f $LOCK ]; then
  98. restart
  99. fi
  100. # update from older version
  101. if [ -f $OLD_LOCK ]; then
  102. restart
  103. fi
  104. ;;
  105. status)
  106. status abrtd
  107. RETVAL=$?
  108. ;;
  109. *)
  110. echo $"Usage: $0 {start|stop|status|restart|condrestart|reload|force-reload}"
  111. RETVAL=2
  112. esac
  113.  
  114. exit $RETVAL

三、参考系统示例

  1. [root@docker-server ~]# cd /etc/init.d/
  2. [root@docker-server init.d]# ll
  3. total 32
  4. -rw-r--r--. 1 root root 15301 Aug 30 2016 functions
  5. -rwxr-xr-x. 1 root root 2989 Aug 30 2016 netconsole
  6. -rwxr-xr-x. 1 root root 6834 Aug 30 2016 network
  7. -rw-r--r--. 1 root root 1160 Oct 12 2016 README
  8. [root@docker-server init.d]# cvat README
  9. -bash: cvat: command not found
  10. [root@docker-server init.d]# cat README
  11. You are looking for the traditional init scripts in /etc/rc.d/init.d,
  12. and they are gone?
  13.  
  14. Here's an explanation on what's going on:
  15.  
  16. You are running a systemd-based OS where traditional init scripts have
  17. been replaced by native systemd services files. Service files provide
  18. very similar functionality to init scripts. To make use of service
  19. files simply invoke "systemctl", which will output a list of all
  20. currently running services (and other units). Use "systemctl
  21. list-unit-files" to get a listing of all known unit files, including
  22. stopped, disabled and masked ones. Use "systemctl start
  23. foobar.service" and "systemctl stop foobar.service" to start or stop a
  24. service, respectively. For further details, please refer to
  25. systemctl(1).
  26.  
  27. Note that traditional init scripts continue to function on a systemd
  28. system. An init script /etc/rc.d/init.d/foobar is implicitly mapped
  29. into a service unit foobar.service during system initialization.
  30.  
  31. Thank you!
  32.  
  33. Further reading:
  34. man:systemctl(1)
  35. man:systemd(1)
  36. http://0pointer.de/blog/projects/systemd-for-admins-3.html
  37. http://www.freedesktop.org/wiki/Software/systemd/Incompatibilities

四、利用systemctl添加自定义系统服务

参考:https://www.cnblogs.com/saneri/p/7778756.html

linux systemctl service examples的更多相关文章

  1. 时隔两年最近再次折腾opensuse 的一些笔记 - opensuse linux java service shell

    时隔两年最近再次折腾opensuse 的一些笔记 - opensuse linux java service shell opensuse 一些常用命令:    service xxx start/s ...

  2. linux systemctl 常用用法简介

    主要介绍systemctl的几个功能如下: 1.查看某个服务的状态 2.关闭某个服务 3.开启某个服务 4.设置某个为开机自启动 5.关闭某个服务为开机不启动 6.查看所有开启启动的服务 1.查看某个 ...

  3. 在 CentOS7 上将自定义的 jar 包注册为 linux 服务 service

    在 CentOS7 上将自定义的 jar 包注册为 linux 服务 service 1.在 /etc/rc.d/init.d/ 目录下创建一个名字和服务名完全相同的 shell 脚本文件 joyup ...

  4. 将Apache加入到linux系统service

    将Apache加入到linux系统service 将apache加入到linux系统服务,用service命令来控制apache的启动和停止. 本文由乌合之众瞎写http://www.cnblogs. ...

  5. Linux: 20 Iptables Examples For New SysAdmins

    Linux comes with a host based firewall called Netfilter. According to the official project site: net ...

  6. Linux: service network/Network/NetworkManager

    Linux:service network/Network/NetworkManager start 这三种有什么不同? 1.network service的制御网络接口配置信息改动后,网络服务必须从 ...

  7. Linux中service命令和/etc/init.d/的关系

    Linux中service命令和/etc/init.d/的关系   service xxx启动 /etc/init.d/ 目录下的xxx脚本 如一个脚本名为 mysvc保存在/etc/init.d/下 ...

  8. Linux命令service - 系统服务管理(转)

    用途说明 service命令用于对系统服务进行管理,比如启动(start).停止(stop).重启(restart).查看状态(status)等.相关的命令还包括chkconfig.ntsysv等,c ...

  9. Azure的CentOS上安装LIS (Linux Integration Service)

    Azure上虚拟化技术都是采用的Hyper-v,每台Linux虚拟机都安装了LIS(Linux Integration Service).LIS的功能是为VM提供各种虚拟设备的驱动.所以LIS直接影响 ...

随机推荐

  1. Python阶段复习 - part 2 - Python序列/持久化

    1. 把一个数字的list从小到大排序,然后写入文件,然后从文件中读取出来文件内容,然后反序,在追加到文件的下一行中 >>> import json >>> imp ...

  2. python实战===石头剪刀布,简单模型

    #石头剪刀布 import random import time win_list = [("石头","剪刀"),("布","石头 ...

  3. python基础===python内置函数大全

    python python内建函数 一.数学运算类 abs(x) 求绝对值1.参数可以是整型,也可以是复数2.若参数是复数,则返回复数的模 complex([real[, imag]]) 创建一个复数 ...

  4. TortoiseSVN安装使用【转】

    转自:http://www.cnblogs.com/rushoooooo/archive/2011/04/29/2032346.html TortoiseSVN是windows平台下Subversio ...

  5. (十二)Linux内核驱动之poll和select

    使用非阻塞 I/O 的应用程序常常使用 poll, select, 每个允许一个进程来决定它是否可读或者写一个或多个文件而不阻塞. 这些调用也可阻塞进程直到任何一个给定集合的文件描述符可用来读或写.  ...

  6. git 提示 Please move or remove them before you can merge 解决办法

    解决Git冲突造成的Please move or remove them before you can merge git clean -d -fx其中x -----删除忽略文件已经对git来说不识别 ...

  7. [ 总结 ] Linux 下文件描述符

    1.概述: 文件描述符是内核为了高效管理已被打开的文件所创建的索引.是一个非负整数,用于代指被打开的文件.所有通过I/O操作的系统调用都通过文件描述符. 文件描述符用以表明每一个被进程所打开的文件和s ...

  8. 【 LVS 】类型及算法

    一.概念: LVS( linux virtual server ) : Linux虚拟服务器 lvs是一个负载均衡设备,它不提供任何服务,用户请求到这里的时候,它将客户需求转发至后端的realserv ...

  9. Selenium2+python自动化65-js定位几种方法总结【转载】

    前言 本篇总结了几种js常用的定位元素方法,并用js点击按钮,对input输入框输入文本 一.以下总结了5种js定位的方法 除了id是定位到的是单个element元素对象,其它的都是elements返 ...

  10. apache httpd反向代理配置

    apache httpd 2.4.6反向代理的配置,用户访问A server的8080端口,后台会自动请求Bserver的一个端口. 例如,用户访问ip-172-31-28-175的8080端口,后台 ...