Cron job works when run manually, but not from crontab

余生长醉 提交于 2019-12-24 05:51:18

问题


I made a script that uses forever to control node.js, its nothing fancy, but it works just the way I want it to when run manually, however when run from the Cron job @reboot nothing happens, I have the cron set to redirect stderr and stdout to a log file so I can try to figure it out, but the file never changes.

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

Content of Script:

#!/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

Any help is appreciated!


回答1:


Typical cron mistake.

You must use absolute paths for everything. Even for grep, ps, awk, etc.




回答2:


could you try this: @reboot /sbin/ghost boot > /home/mary/.ghost.log 2>&1



来源:https://stackoverflow.com/questions/19992841/cron-job-works-when-run-manually-but-not-from-crontab

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!