无法启动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 

我的脚本有问题吗? 在启动时需要做些什么来执行这个脚本? 即使系统在崩溃或任何故障后重新启动,我也希望执行该脚本。

问候

/etc/init.d中的脚本默认情况下不会在启动时执行。 这只是启动脚本所在的位置(或者现在的位置)。

在放置脚本之后,需要在/etc/rc[1-6].d创build符号链接。 您可以手动执行该操作,也可以运行:

 update-rc.d <scriptname> enable 

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

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

为了完成我的答案,我build议您使用下面的shebang而不是实际的正确加载bash env正确的path等: #! /usr/bin/env bash #! /usr/bin/env bash