Auto startup by service

Auto startup by service

#

Example

  • Notes
    • ### BEGIN INIT INFO + ... + ### END INIT INFO 最好有,
      否则会有一个警告
    • 如果不在主分区, 注意等待分区或者文件系统挂载!
  • E.g. https://yuiwong.org/gitlab/pkgman/debrepo + ubuntu xenial
#!/bin/sh
### BEGIN INIT INFO
# Provides:          yuiwonguploadinotify
# Required-Start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     1 2 3 4 5
# Default-Stop:      0 6
# Short-Description: My upload inotify
# Description:       My upload inotify.
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HAS_DEBIAN=true
HAS_UBUNTU=true
prog="/opt/debrepo/bin/yuiwonguploadinotify"
#if [ -f /etc/default/rcS ]; then
#  . /etc/default/rcS
#fi
#. /lib/lsb/init-functions
case "$1" in
start)
  #killall yuiwonguploadinotify
  # Wait mount
  i=0
  echo "Wait mount .."
  #log_progress_msg "Wait mount .."
  while [ $i -lt 60 ]; do
    if [ -e "$prog" ]; then
      break
    fi
    sleep 1
    i=$(($i+1))
  done
  if [ ! -e "$prog" ]; then
    #log_progress_msg "Mount timed out"
    echo "Mount timed out" >&2
    exit 1
  fi
  #log_progress_msg "Mounted"
  echo "Mounted"
  if [ "true" = "${HAS_UBUNTU}" ]; then
    "$prog" yuiwong /my/debrepo/.ubuntuincoming \
      /my/debrepo/tools/reprepro-import_ubuntu /opt/debrepo/ubuntu-upload.log &
  fi
  if [ "true" = "${HAS_DEBIAN}" ]; then
    "$prog" yuiwong /my/debrepo/.debianincoming \
      /my/debrepo/tools/reprepro-import_debian /opt/debrepo/debian-upload.log &
  fi
  ;;
stop)
  killall yuiwonguploadinotify
  exit 0
  ;;
restart)
  $0 stop
  sleep 1
  $0 start
  ;;
*)
  echo "Usage: $0 {start|stop|restart}" >&2
  exit 1
  ;;
esac
exit 0

Install

E.g.

install_service: build
ifeq ($(REPO_ROOT_PATH),)
    @echo "No REPO_ROOT_PATH=..." > /dev/stderr
    @exit 1
endif
ifeq ($(IMPORTER_USER),)
    @echo "No IMPORTER_USER=..." > /dev/stderr
    @exit 1
endif
    @/bin/cp -f yuiwonguploadinotifyd.in yuiwonguploadinotifyd
    sed -i "s/REPO_ROOT_PATH/$$(echo -n $(REPO_ROOT_PATH) | sed 's/\//\\\//g')/g" yuiwonguploadinotifyd
    sed -i "s/IMPORTER_USER/$$(echo -n $(IMPORTER_USER) | sed 's/\//\\\//g')/g" yuiwonguploadinotifyd
    chmod a+x yuiwonguploadinotifyd
    cat yuiwonguploadinotifyd
    sudo cp yuiwonguploadinotifyd /etc/init.d/
    cd /etc/init.d/ && sudo update-rc.d yuiwonguploadinotifyd defaults 95

Uninstall

E.g.

uninstall_service:
    @test -n /etc/init.d/yuiwonguploadinotifyd \
    || sudo service yuiwonguploadinotifyd stop \
    && sudo update-rc.d -f yuiwonguploadinotifyd remove \
    && sudo rm -f yuiwonguploadinotifyd /etc/init.d/yuiwonguploadinotifyd \
    && sudo systemctl daemon-reload
    @sudo killall yuiwonguploadinotify || true

https://yuiwong.org/gitlab/computerscience/use-unix