Crontab在系统重新启动时未运行Shell脚本

时间:2019-02-26 14:09:20

标签: linux ubuntu unix cron reboot

所以我正在运行Ubuntu 16.04,并尝试在启动时运行cronjob。我已经确认如果我也更改@reboot,脚本便可以运行* * * * *。我还尝试过以其他用户和root身份运行作业,但均无济于事。

当我为CRON复制系统日志时,我得到:

Feb 26 05:59:22 embarks1 cron[738]: (CRON) INFO (pidfile fd = 3)
Feb 26 05:59:22 embarks1 cron[738]: (CRON) INFO (Skipping @reboot jobs -- not system startup)

我的cronjob看起来像这样。 (文件名已更改)

@reboot /etc/init.d/example.sh

我不知道如何或为什么。任何帮助,将不胜感激。

编辑1-

这是我的init.d文件:


### BEGIN INIT INFO
# Provides:         StartJenkins
# Required-Start:
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      1 0 6
# Short-Description: ...
# Description: ...
### END INIT INFO

APP_NAME="StartJenkins APP"
APP_DIR="~/Linux"

USER="ci"


USAGE="Usage: $0 {start}"

start_app() {
    echo "Starting $APP_NAME ..."
    su - $USER -c "cd $APP_DIR && ./startJenkins.sh"
}

case "$1" in
    start)
        start_app
    ;;

    *)
        echo $USAGE
        exit 1
    ;; 

1 个答案:

答案 0 :(得分:-1)

1)创建具有以下内容的ProgressCallbackFunc脚本:

/etc/init.d/example

或简单地:

#!/bin/sh

### BEGIN INIT INFO
# Provides:          Example APP
# Required-Start:
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      1 0 6
# Short-Description: ...
# Description: ...
### END INIT INFO

APP_NAME="Example APP"
APP_DIR="/path/to/example"

USER="example"
GROUP="example"

USAGE="Usage: $0 {start|stop|restart|status}"

start_app() {
    echo "Starting $APP_NAME ..."
    su - $USER -c "cd $APP_DIR && ./example"
}

stop_app() {
    echo "NOT IMPLEMENTED"
}

status_app() {
    echo "NOT IMPLEMENTED"
}

restart_app() {
    echo "NOT IMPLEMENTED"
} 

case "$1" in
    start)
        start_app
    ;;

    stop)
        stop_app
    ;;

    restart)
        restart_app
    ;;

    status)
        status_app
    ;;

    *)
        echo $USAGE
        exit 1
    ;;
esac

2)设置默认值并启用:

#!/bin/sh

### BEGIN INIT INFO
# Provides:          Example APP
# Required-Start:
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      1 0 6
# Short-Description: ...
# Description: ...
### END INIT INFO

APP_NAME="Example APP"
APP_DIR="/path/to/example"

USER="example"
GROUP="example"

USAGE="Usage: $0 {start}"

start_app() {
    echo "Starting $APP_NAME ..."
    su - $USER -c "cd $APP_DIR && ./example"
}

case "$1" in
    start)
        start_app
    ;;

    *)
        echo $USAGE
        exit 1
    ;;
esac