一键制作程序启动脚本

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

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

一行命令:

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

全程示例:

# 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,其内容如下:

#!/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

Relative Articles