ubuntu自定义服务模板

根据他人代码修改:

 1 #!/bin/sh
 2 ### BEGIN INIT INFO
 3 # Provides:          <pragram name>
 4 # Required-Start:    $local_fs $network $named $time $syslog
 5 # Required-Stop:     $local_fs $network $named $time $syslog
 6 # Default-Start:     2 3 4 5
 7 # Default-Stop:      0 1 6
 8 # Description:       shrpx proxy
 9 ### END INIT INFO
10 
11 PROG= #<pragram name>
12 PIDFILE= #<pid file full path>
13 LOGFILE= #<log file full path>
14 
15 start() {
16   if [ -f $PIDFILE ]; then
17     if ! pgrep $PROG; then
18         echo "Terminate abnormaly last time!"
19         rm -f $PIDFILE
20     else
21         echo 'Service already running' >&2
22         return 1
23     fi
24   fi
25 
26   if [ -n "$PIDFILE" ];then
27     PIDFILE="&1"
28   fi
29 
30   echo 'Starting service...'
31 
32   if $PROG >$LOGFILE; then
33     echo 'Service started'
34   else
35     echo 'Start failed!' >&2
36   fi
37 }
38 
39 stop() {
40   if [ ! -f "$PIDFILE" ]; then
41     echo 'Service not running' >&2
42     return 1
43   fi
44   echo 'Stopping service…' >&1
45   kill -KILL $(cat "$PIDFILE")
46   rm -f "$PIDFILE"
47   echo 'Service stopped' >&1
48 }
49 
50 status(){
51     if [ -f "$PIDFILE" ];then
52         echo "$PROG is running"
53     else
54         echo "$PROG is stopped"
55     fi
56 }
57 
58 uninstall() {
59   echo "Are you really sure you want to uninstall this service?"\
60    "That cannot be undone. [yes|No] "
61   local SURE
62   read SURE
63   if [ X"$SURE" = "Xyes" ]; then
64     stop
65     if [ -f $PIDFILE ]; then
66         rm -f "$PIDFILE"
67     fi
68     echo "Notice: log file is not be removed: '$LOGFILE'" >&1
69     update-rc.d -f $PROG remove
70     rm -fv "$0"
71   fi
72 }
73 
74 case "$1" in
75   start)
76     start
77     ;;
78   stop)
79     stop
80     ;;
81   uninstall)
82     uninstall
83     ;;
84   retart)
85     stop
86     start
87     ;;
88   status)
89     status
90     ;;
91   *)
92     echo "Usage: $0 {start|stop|status|restart|uninstall}"
93 esac

 使用方法:cp到/etc/init.d/下面,修改<>里面的内容,然后使用update-rc.d管理服务;

添加一个服务:sudo update-rc.d srv_name defaults
删除一个服务 :sudo update-rc.d–f srv_name remove

default表示的是服务的运行优先级,默认是20,数字越小优先级越高。

以后就会开机自启动了,使用sudo service srv_name start|stop|status|restart|uninstall来做临时性的管理。

posted @ 2014-01-25 14:02  生无所息  阅读(940)  评论(0编辑  收藏  举报