Upstart 脚本不起作用

Upstart 脚本不起作用

我正在尝试创建自己的脚本,该脚本在启动时执行并在关机时停止(参考经典的 System-V init 样式),但它不起作用!

我已经创建了这个脚本(/etc/init.d/mlogscript):

#!/bin/bash
### BEGIN INIT INFO
# Provides:             logging down time and date of your start/shutdown behaviour
# Required-Start:       $all
# Required-Stop:        $all
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    logs down your start/shutdown behaviour
# Description:          script for logging when you start or shutdown the operating system
### END INIT INFO
# Author: x

# log-file info
FILENAME="logscript.log"
FILEPATH="$HOME/myScripts/"

# other files
UPTIMEFILE=${FILEPATH}"LASTUPTIME.DONOTDELETE"

# fetch specific data
DATE=`date +%m.%d.%Y`
CLOCK=`date +%X`

case "$1" in
    start)
    if [ ! -e "${FILEPATH}${FILENAME}" ]; then
        echo "creating log-file"
        echo -ne "mLog: start/shutdown behaviour\n" >> "${FILEPATH}${FILENAME}"
        echo -e "-------------------------------\n" >> "${FILEPATH}${FILENAME}"
    fi
    echo -e '[' $DATE "]\nuser:\t" $USER "\nstart:\t" $CLOCK >> "${FILEPATH}${FILENAME}"

    # create/update uptime-file
    UPTIME=$(</proc/uptime)
    UPTIME=${UPTIME%%.*}
    echo -ne $UPTIME > $UPTIMEFILE

    echo "log-script started at" $DATE "."
        ;;
    stop)
    if [ -e "${FILEPATH}${FILENAME}" ]; then
        # get uptime from /proc/uptime
        UPTIME=$(</proc/uptime)
        UPTIME=${UPTIME%%.*}

        # 
        if [ -e "${UPTIMEFILE}" ]; then
            LASTUPTIME=$(head -n 1 "${UPTIMEFILE}")
            UPTIME=`expr $UPTIME - $LASTUPTIME`
            echo $UPTIME
        fi

        SECONDS=$((UPTIME%60))
        MINUTES=$((UPTIME/60%60))
        HOURS=$((UPTIME/60/60%24))
        DAYS=$((UPTIME/60/60/24%365))
        YEAR=$((UPTIME/60/60/24/365))
        # write to file
        echo -ne "end:\t" $CLOCK "\nuptime:\tY" $YEAR ' D' $DAYS ' H' $HOURS ' M' $MINUTES ' S' $SECONDS "\n\n" >> "${FILEPATH}${FILENAME}"
        echo "log-script stopped"
    else
        echo "log-file doesn't exist"
    fi
        ;;
    restart)
    if [ -e "${FILEPATH}${FILENAME}" ]; then
        exec $(readlink -f "$0") stop
        $(readlink -f "$0") start
        echo -e "log-script restarted"
    fi
        ;;
    reset)
    echo -e "log-script reset"
    if [ -e "${FILEPATH}${FILENAME}" ]; then
        rm "${FILEPATH}${FILENAME}"
        exec $(readlink -f "$0") start
    fi
    ;;
esac
exit 0

之后我通过执行将文件的所有者和组设置为“root”

sudo chown root:root /etc/init.d/mlogscript

最后但同样重要的一点是,我在 rc[0-6].d 文件夹中创建了特定于运行时的符号链接:

sudo update-rc.d mlogscript defaults 98

我重新启动系统后什么也没发生,为什么?

答案1

  1. 打开 Dash
  2. 搜索“启动应用程序”并打开
  3. 点击“添加”按钮。
  4. 该对话框允许您设置名称、要执行的命令和注释。
  5. 单击“添加”将该命令保存到列表中。

然后,Ubuntu 将在启动时运行此列表中的所有命令。我使用它在启动时运行我自己的壁纸更改脚本,事实证明它是可靠的。

&我想你一定知道这一点,但仍然记得在命令末尾添加一个,以便继续在后台运行脚本,并防止不必要的调试和挫败感。

抱歉,但我不知道如何从终端执行此操作。

相关内容