【问题标题】:Cron job works when run manually, but not from crontabCron 作业在手动运行时有效,但不是从 crontab
【发布时间】:2013-11-28 08:38:17
【问题描述】:

我制作了一个永远用于控制 node.js 的脚本,它没有什么花哨的,但它在手动运行时按照我想要的方式工作,但是当从 Cron 作业@reboot 运行时没有任何反应,我有 cron设置为将 stderr 和 stdout 重定向到日志文件,以便我可以尝试找出它,但文件永远不会改变。

Cron 作业: @reboot /sbin/ghost boot &> /home/mary/.ghost.log

脚本内容:

#!/bin/bash

###########################################################
# This script is made to control Ghost using forever as   #
# if it were a service running on your system. Place in   #
# your path ie /usr/bin/                                  #
#                                                         #
# This script was created/tested on a Rasberry Pi         #
# running Raspbian, however it should be *NIX independent #
#                                                         #
# This script must be run as root, sudo will not work     #
# with forever                                            #
#                                                         #
# Make sure you alter the GhostDIR variable to your       #
# ghost directory, ie mine is /opt/ghost                  #
#                                                         #
# Created by Paul Williams                                #
# www.infinitepercent.com                                 #
###########################################################

# Variables:
GhostDIR=/opt/ghost #BE SURE TO ADJUST THIS TO YOUR GHOST DIRECTORY


# Functions:

# start function will test if Ghost is running, if not it will start it with forever
start()
{
    PID=$(ps -ef | grep monitor\ index.js | grep -v grep | grep -v ps | awk '{print $2}')
    if [ "$PID" = "" ]; then
        NODE_ENV=production forever start index.js
    else
        echo "Ghost is already running!"
    fi
}

# stop function will test if Ghost is running, it if is it will stop it with forever
stop()
{
    PID=$(ps -ef | grep monitor\ index.js | grep -v grep | grep -v ps | awk '{print $2}')
    if [ "$PID" = "" ]; then
        echo "Ghost isn't running!"
    else
        forever stop index.js
    fi
}

# restart function calls stop function and then calls start function
restart()
{
    stop
    start
}

# WARNING, DO NOT EVER MANUALLY ENTER BOOT
# YOU MAY END UP WITH MULTIPLE INSTANCES OF GHOST
# THIS OPTION IS MEANT TO BE USED FOR A CRON @REBOOT
boot()
{
    NODE_ENV=production forever start index.js
}

# status function is used to check on the status of Ghost
status()
{
    forever list
}

cd $GhostDIR

if [ "$1" = "start" ]; then
    start
elif [ "$1" = "stop" ]; then
    stop
elif [ "$1" = "restart" ]; then
    restart
elif [ "$1" = "status" ]; then
    status
elif [ "$1" = "boot" ]; then
    boot
else
    echo "$0 {start|stop|restart|status}"
fi

感谢任何帮助!

【问题讨论】:

    标签: bash cron crontab


    【解决方案1】:

    典型的 cron 错误。

    您必须对所有内容使用绝对路径。即使是 grep、ps、awk 等。

    【讨论】:

    • face palm 好吧,我添加了绝对路径,但仍然无法正常工作,我似乎无法永远开始使用 cron 作业,所以我选择了不同的无需永远使用即可启动 index.js 的方法。感谢您的协助!
    【解决方案2】:

    你可以试试这个:@reboot /sbin/ghost boot > /home/mary/.ghost.log 2>&1

    【讨论】:

    • 感谢您尝试帮助 slayedbylucifer,我决定使用另一种方法来启动 index.js,并且该方法适用于 cron 作业。
    猜你喜欢
    • 2010-12-23
    • 2021-03-12
    • 1970-01-01
    • 2014-04-04
    • 1970-01-01
    • 2012-09-18
    • 1970-01-01
    • 2014-09-06
    • 1970-01-01
    相关资源
    最近更新 更多