一键制作程序启动脚本

VPS 上程序的启动脚本,就是放在/etc/init.d 下的文件,随系统启动,能用 service xxx {start|stop|status|restart|uninstall} 控制程序

一键制作程序启动脚本

简单的一键制作是建立在别人的努力上:

一行命令:

1
wget 'https://raw.githubusercontent.com/jasonblewis/sample-service-script/master/new-service.sh' && bash new-service.sh

运行如下,需要填写的信息:

  • Service name : myapp
  • Description : "myapp des"
  • Command : /root/bin/myapp
    
  •    User : root
    

全程示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# wget 'https://raw.githubusercontent.com/jasonblewis/sample-service-script/master/new-service.sh' && bash new-service.sh
--2019-06-16 01:37:47--  https://raw.githubusercontent.com/jasonblewis/sample-service-script/master/new-service.sh
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.0.133, 151.101.64.133, 151.101.128.133, ...
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.0.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2980 (2.9K) [text/plain]
Saving to: 'new-service.sh'

new-service.sh                                     100%[================================================================================================================>]   2.91K  --.-KB/s    in 0s      

2019-06-16 01:37:47 (63.8 MB/s) - 'new-service.sh' saved [2980/2980]

--- Download template ---
I'll now download the service.sh, because is is not downloaded.
...
I donloaded the tmplate sucessfully

--- Copy template ---

--- Customize ---
I'll now ask you some information to customize script
Press Ctrl+C anytime to abort.
Empty values are not accepted.

Service name : myapp
 Description : "myapp des"
     Command : /root/bin/myapp
        User : root

--- Installation ---
1. mv "/tmp/file5Sipy2" "/etc/init.d/myapp"
'/tmp/file5Sipy2' -> '/etc/init.d/myapp'
2. touch "/var/log/myapp.log" && chown "root" "/var/log/myapp.log"
3. update-rc.d "myapp" defaults
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = (unset),
	LANG = "zh_CN.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
4. service "myapp" start

---Uninstall instructions ---
The service can uninstall itself:
    service "myapp" uninstall
It will simply run update-rc.d -f "myapp" remove && rm -f "/etc/init.d/myapp"

--- Terminated ---

此时, /etc/init.d/ 下有一个可实行文件 myapp,其内容如下:

Bash:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/bin/sh
### BEGIN INIT INFO
# Provides:          myapp
# Required-Start:    $local_fs $network $named $time $syslog
# Required-Stop:     $local_fs $network $named $time $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Description:       \"myapp\ des\"
### END INIT INFO

SCRIPT="/root/bin/myapp"
RUNAS=root

PIDFILE=/var/run/myapp.pid
LOGFILE=/var/log/myapp.log

start() {
  if [ -f $PIDFILE ] && [ -s $PIDFILE ] && kill -0 $(cat $PIDFILE); then
    echo 'Service already running' >&2
    return 1
  fi
  echo 'Starting service…' >&2
  local CMD="$SCRIPT &> \"$LOGFILE\" & echo \$!"
  su -c "$CMD" $RUNAS > "$PIDFILE"
 # Try with this command line instead of above if not workable
 # su -s /bin/sh $RUNAS -c "$CMD" > "$PIDFILE"
 
  sleep 2
  PID=$(cat $PIDFILE)
    if pgrep -u $RUNAS -f $NAME > /dev/null
    then
      echo "$NAME is now running, the PID is $PID"
    else
      echo ''
      echo "Error! Could not start $NAME!"
    fi
}

stop() {
  if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then
    echo 'Service not running' >&2
    return 1
  fi
  echo 'Stopping service…' >&2
  kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"
  echo 'Service stopped' >&2
}

uninstall() {
  echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] "
  local SURE
  read SURE
  if [ "$SURE" = "yes" ]; then
    stop
    rm -f "$PIDFILE"
    echo "Notice: log file was not removed: $LOGFILE" >&2
    update-rc.d -f $NAME remove
    rm -fv "$0"
  else
    echo "Abort!"
  fi
}

status() {
    printf "%-50s" "Checking myapp..."
    if [ -f $PIDFILE ] && [ -s $PIDFILE ]; then
        PID=$(cat $PIDFILE)
            if [ -z "$(ps axf | grep ${PID} | grep -v grep)" ]; then
                printf "%s\n" "The process appears to be dead but pidfile still exists"
            else    
                echo "Running, the PID is $PID"
            fi
    else
        printf "%s\n" "Service not running"
    fi
}


case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
    status
    ;;
  uninstall)
    uninstall
    ;;
  restart)
    stop
    start
    ;;
  *)
    echo "Usage: $0 {start|stop|status|restart|uninstall}"
esac

本文网址: https://pylist.com/topic/180.html 转摘请注明来源

Suggested Topics

在GAE 上正确使用缓存优化程序

缓存在应用中经常会用到,为了避免一些需要长时间才能得到的结果多次重复获取。GAE 是一个分布式平台,数据操作和网络访问都需要很长的时间,更应该在这样的操作里添加缓存。...

Leave a Comment