Cron作业在手动运行时有效,但不能从crontab运行

时间:2013-11-15 03:13:22

标签: bash cron crontab

我制作了一个永远使用来控制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

感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

典型的cron错误。

您必须使用绝对路径。即使是grep,ps,awk等

答案 1 :(得分:0)

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