2

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

定时任务: @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

任何帮助表示赞赏!

4

2 回答 2

1

典型的 cron 错误。

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

于 2013-11-15T15:57:06.210 回答
0

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

于 2013-11-15T03:31:19.150 回答