sh [启动脚本示例] #bash #linux

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh [启动脚本示例] #bash #linux相关的知识,希望对你有一定的参考价值。

The following is mostly taken from the example published at https://mkaz.com/2013/07/03/run-script-at-start-on-debian/

Write an init.d script according to the the dependency-booting specification (see at https://wiki.debian.org/LSBInitScripts).
, say it `foo.sh`. Place the script under `/etc/init.d`.

    ln -s /root/scripts/foo.sh /etc/init.d/foo 

Update the runlevel directories under `/etc/rc*`:

    update-rc.d foo defaults
    
#! /bin/bash

### BEGIN INIT INFO
# Provides:          foo
# Required-Start:    $local_fs $network
# Required-Stop:     $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: foo service
# Description:       Run Foo service
### END INIT INFO

# Carry out specific functions when asked to by the system
case "$1" in
  start)
    echo "Starting Foo..."
    sudo -u foo-user bash -c 'cd /path/to/scripts/ && ./start-foo.sh'
    ;;
  stop)
    echo "Stopping Foo..."
    sudo -u foo-user bash -c 'cd /path/to/scripts/ && ./stop-foo.sh'
    sleep 2
    ;;
  *)
    echo "Usage: /etc/init.d/foo {start|stop}"
    exit 1
    ;;
esac

exit 0

以上是关于sh [启动脚本示例] #bash #linux的主要内容,如果未能解决你的问题,请参考以下文章

docker~写个容器启动的bash脚本

linux脚本中执行命令

Linux拓展之调试脚本

Linux配置开机自启动执行脚本都有哪些方法

Linux配置开机自启动执行脚本都有哪些方法

shell命令中用source 和sh(或者bash)执行脚本的区别,以及export的作用