ubuntu 启动时无法启动脚本

ubuntu 启动时无法启动脚本

我正在尝试在 ubuntu 16.04 LTS 桌面上启动时执行一个脚本。我已将脚本添加到/etc/init.d位置。但重启后,我无法使用以下命令看到我的脚本正在运行

pidof -s pgd

一旦我执行此命令,sudo service run_pgd start它就会以 pid 启动。我希望此脚本在后台执行。

我通过添加脚本实现了同样的效果,rc.local运行良好。但我在/etc/init.d位置方面遇到了问题这是脚本文件

#!/bin/bash

###
# Configuration section
# 

# Specify full path to directory with pgd executable and config files here:
# For example: PGD_DIR="/usr/local/Neurotechnology/Activation"
PGD_DIR="/opt/Neurotec_Biometric_10_0_SDK/Bin/Linux_x86_64/Activation"

#
# End of Configuration section
###


# If PGD_DIR is not set use the directory of current script
if [ ! "${PGD_DIR}" ]
then
    PGD_DIR=`dirname "$0"`

    # If we were called through relative path, make absolute one
    if [ "${PGD_DIR:0:1}" != "/" ]
    then
        PGD_DIR="$PWD/$PGD_DIR"
    fi
fi

NAME=pgd
PROGRAM="${PGD_DIR}/${NAME}"
if [ "`uname -s`" = "Darwin" ]
then
    LOG=/Library/Logs/pgd.log
else
    LOG=/tmp/pgd.log
fi

###
# Common routines section
#

echo_()
{
    echo "run_pgd.sh:" "$*"
}

get_pid()
{
    if which pidof &> /dev/null
    then
        echo `pidof -s $NAME`
    else
        ps axc|awk "{if (\$5==\"$NAME\") print \$1}"
    fi
}

start_pgd()
{
    echo_ "starting $NAME..."
    PRESERVE_DIR="$PWD"
    cd "$PGD_DIR"
    "$PROGRAM"
    cd "$PRESERVE_DIR"
    sleep 1
    if [ -s "$LOG" ]
    then
        echo_ "$NAME run log ($LOG):"
        echo  "----------------------------------------"
        cat   "$LOG"
        echo  "----------------------------------------"
    fi
}

stop_pgd()
{
    PID=`get_pid`
    if [ $PID ]
    then 
        echo_ "stopping $NAME..."
        kill $PID
        echo_ "$NAME (pid=$PID) was sent a signal."
    else
        echo_ "$NAME processs not running!"
        exit 1
    fi
}

###
# Body...
#

if ! test -d "$PGD_DIR"
then
    echo_ "Wrong PGD_DIR variable value specified: $PGD_DIR"
    echo_ "Please point it to correct place with $NAME application."
    echo_ "Please also ensure that needed configuration files are there."
    exit 1
fi 
if ! test -f "$PROGRAM"
then
    echo_ "$PROGRAM application not found!"
    exit 1
fi
if ! test -x "$PROGRAM"
then
    echo_ "$PROGRAM application not runable!"
    exit 1
fi


case "$1" in
  start)
    PID=`get_pid`
    if [ $PID ]
    then
        echo_ "$NAME is already running (pid: $PID)"
        exit 1
    fi

    [ ! -f $LOG ] || rm $LOG
    start_pgd
    ;;
  stop)
    stop_pgd
    ;;
  restart)
    stop_pgd

    sleep 4

    if [ `get_pid` ]
    then
        echo_ "$NAME has not stopped!"
        echo_ "restart failed."
        exit 1
    fi

    [ ! -f $LOG ] || rm $LOG
    start_pgd
    ;;
  log)
    PID=`get_pid`
    if [ $PID ]
    then 
        echo_ "$NAME is running with pid: $PID"
    else
        echo_ "$NAME processs not running!"
    fi

    if [ -s "$LOG" ]
    then 
        echo_ "log ($LOG):"
        echo  "----------------------------------------"
        cat   "$LOG"
        echo  "----------------------------------------"
    else
        echo_ "log file (\"$LOG\") is empty."
    fi
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|log}"
    exit 1
esac

exit 0

我的脚本有问题吗?我需要做哪些更改才能在启动时执行此脚本?我希望即使系统在崩溃或发生任何故障后重新启动,也能执行此脚本。

问候

答案1

默认情况下,启动时不会执行中的脚本/etc/init.d。这只是启动脚本所在的位置(或者,到目前为止,曾经是)。

将脚本放在那里后,您需要在 中创建符号链接/etc/rc[1-6].d。您可以手动执行此操作,也可以通过运行以下命令执行此操作:

update-rc.d <scriptname> enable

注意:在 Ubuntu 16.04 中,旧的 init 脚本已被弃用。你应该创建 systemd 服务定义为您的脚本。

答案2

您可以使用@reboot 将您的脚本添加到 crontab: @reboot /path/to/my/script.sh

为了完成我的回答,我建议您使用以下shebang而不是实际的shebang来正确加载具有正确路径等的bash环境:#! /usr/bin/env bash

答案3

从 Ubuntu 16.04 开始,init 脚本已弃用。你应该创建一个系统服务来实现你想要的。

给它一个名字myservice.service,并将其放入/lib/systemd/system/,然后使用systemctl enable myservice.service

相关内容