开源项目 `init-script-template` 使用教程

开源项目 init-script-template 使用教程

init-script-templateA template for simple System V init scripts项目地址:https://gitcode.com/gh_mirrors/in/init-script-template

项目介绍

init-script-template 是一个用于创建自定义初始化脚本的模板项目。该项目提供了一个基本的初始化脚本模板,可以帮助用户快速地创建自己的初始化脚本。用户可以根据需要修改这个模板,并将其应用于各种系统和应用中。

项目快速启动

要使用 init-script-template,您首先需要将该项目克隆到本地计算机上:

git clone https://github.com/fhd/init-script-template.git

然后,在项目目录中,您可以根据自己的需求修改 init.sh 文件中的代码。例如,您可以添加或删除变量、更改脚本的行为等等。

最后,您可以将修改后的 init.sh 脚本复制到您需要的地方,并运行该脚本来初始化您的系统或应用。

应用案例和最佳实践

应用案例

假设您需要在启动时自动运行一个 Node.js 应用,您可以使用 init-script-template 来创建一个初始化脚本。以下是一个简单的示例:

  1. 克隆项目并进入目录:

    git clone https://github.com/fhd/init-script-template.git
    cd init-script-template
    
  2. 修改 init.sh 文件,添加 Node.js 应用的启动命令:

    #!/bin/bash
    ### BEGIN INIT INFO
    # Provides:          mynodeapp
    # 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:       Start My Node.js App
    ### END INIT INFO
    
    dir="/path/to/your/node/app"
    cmd="node app.js"
    user="yourusername"
    
    name=$(basename "$0")
    pid_file="/var/run/$name.pid"
    log_file="/var/log/$name.log"
    
    get_pid() {
        cat "$pid_file"
    }
    
    is_running() {
        [ -f "$pid_file" ] && ps $(get_pid) > /dev/null 2>&1
    }
    
    case "$1" in
        start)
        if is_running; then
            echo "Already started"
        else
            echo "Starting $name"
            cd "$dir"
            if [ -z "$user" ]; then
                sudo $cmd >> "$log_file" 2>&1 &
            else
                sudo -u "$user" $cmd >> "$log_file" 2>&1 &
            fi
            echo $! > "$pid_file"
            if ! is_running; then
                echo "Unable to start, see $log_file"
                exit 1
            fi
        fi
        ;;
        stop)
        if is_running; then
            echo -n "Stopping $name.."
            kill $(get_pid)
            for i in {1..10}
            do
                if ! is_running; then
                    break
                fi
                echo -n "."
                sleep 1
            done
            echo
            if is_running; then
                echo "Not stopped; may still be shutting down or shutdown may have failed"
                exit 1
            else
                echo "Stopped"
                if [ -f "$pid_file" ]; then
                    rm "$pid_file"
                fi
            fi
        else
            echo "Not running"
        fi
        ;;
        restart)
        $0 stop
        if is_running; then
            echo "Unable to stop, will not attempt to start"
            exit 1
        fi
        $0 start
        ;;
        status)
        if is_running; then
            echo "Running"
        else
            echo "Stopped"
            exit 1
        fi
        ;;
        *)
        echo "Usage: $0 {start|stop|restart|status}"
        exit 1
        ;;
    esac
    
    exit 0
    
  3. 将修改后的脚本复制到 `/etc/init.d

init-script-templateA template for simple System V init scripts项目地址:https://gitcode.com/gh_mirrors/in/init-script-template

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

虞怀灏Larina

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值