脚本从命令行运行,但失败的crontab [英] The script runs from command line but crontab fails

查看:132
本文介绍了脚本从命令行运行,但失败的crontab的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还在学习Bash和我有我的剧本有问题。我想过滤某些调用这个脚本正在分析呼叫日志,每2分钟为的cronjob。问题是,我可以手动运行,但是当从cron自动运行失败。我不知道为什么。它喊一些有关权限,所以我还挺补丁脚本,所以如果它看起来脏我很抱歉。

 #!/斌/庆典YESTERDAY = $((`日期+'%s'`-86400))
AYER =`日期-d1970-01-01 $ YESTERDAY秒+%Y%M%D`
出生日期=`日期+%Y%M%D`FILENAME =$ FECHA.log
FILE_LINE =$ FECHA.last
FILE =/应用程序/ sittel / rawdata中/敏迪。$出生日期#Limpiar carpeta TMP如果[-eTMP / $ AYER.lnum];然后
    RM TMP / $ {} AYER *
科幻#硅existe EL archivo CON EL NUMERO德laultima LINEA本身PROCESA如果[-eTMP / $ FECHA.lnum];然后    #硒李EL NUMERO德拉LINEAŸ本身extrae联合国archivo CON拉斯lineas apartir
    #德拉ULTIMA busqueda阙本身hizo,posteriormente本身莱HACE联合国的grep    而读线
    做
            尾-n + $ $行文件> TMP / $ FECHA.hal
    完成< TMP / $ FECHA.lnum
    CD TMP    grep的-n00 [0 | 2-9] [0-9] \\ {4 \\}TMP / $ FECHA.hal> TMP / $ {}出生日期。新型    grep的-n900 [0 | 2-9] [0-9] \\ {4 \\}TMP / $ FECHA.hal>> TMP / $ {}出生日期。新型    CD ..
    回声`pwd`
    猫TMP / $ {}出生日期:。新GT;>日志/ $ FILENAME其他
    #埃斯特卡索ES LA西甲VEZ阙本身ejecuta,verifica SI EL登录雅existe
    #德SER ASI,罗elimina第evitar duplicados    如果[-e日志/ $文件名];然后
            RM日志/ $ FILENAME
    科幻    #硒realiza联合国grep的连接埃尔archivo indicadoŸ本身马卡报archivo德萨利达
    #本身布斯卡待办事项洛杉矶numerosķempiecen CON 00 seguidos日0 9 execpto报1    CD TMP    grep的-n00 [0 | 2-9] [0-9] \\ {4 \\}$文件>$ {}出生日期。新型    grep的-n900 [0 | 2-9] [0-9] \\ {4 \\}$文件>>中$ {}出生日期。新型    猫$ {}出生日期:。新GT;> $ FILENAME    MV $ FILENAME ../logs    CD ..
科幻    CPTMP / $ {}出生日期。新型TMP / message.txt    回声Mensaje|邮件-s$ SUBJECT$电子邮件< TMP / message.txt科幻如果[-eTMP / $ {}出生日期。新型];然后
    RMTMP / $ {}出生日期。新型
科幻尾-n1日志/ $ FILENAME> TMP / $ FILE_LINEIFS = $':'
而读线

    DATOS =($行)
    LINE_NUMBER = $ {DATOS [0]}
    回声$ LINE_NUMBER> TMP / $ FECHA.lnum
完成< TMP / $ FILE_LINE
未设置IFS

这是什么系统打印:

  /应用/ sittel / Alarma / callAlarm:第56行:CD:TMP:没有这样的文件或目录
MV:不能移动`20110712.log'到`../logs':权限被拒绝
/应用/ sittel / Alarma / callAlarm:第69行:TMP / 20110712.last:没有这样的文件或目录
/应用/ sittel / Alarma / callAlarm:第77行:TMP / 20110712.last:没有这样的文件或目录


解决方案

您脚本假设它正在从一个特定的目录中运行(注意,几乎每一个路径是相对路径,而不是绝对路径)。 的cron 恰好从另一个目录中运行它。

的修复

如果脚本的工作,当你从它所处的目录中运行它,以下内容添加到你的脚本的顶部:

  MYDIR = $(目录名称$ 0)及和放大器; CD$ {} MYDIR|| 1号出口

说明

$ 1,0 是shell脚本执行的(可能是相对)的文件名。给定一个文件名,目录名命令返回一个包含文件名的目录。

所以,该行改变包含脚本或退出目录,该目录有错误code如果目录名 CD 失败。

I'm still learning Bash and I'm having a problem with my script. I want to filter some calls with this script that is analyzing a call log, every 2 minutes as cronjob. The problem is that I can run it manually but it fails when run automatically from cron. I don't know why. It shouts something about permissions, so I kinda patched the script, so if it seems dirty I'm sorry.

#!/bin/bash

YESTERDAY=$((`date +'%s'`-86400))
AYER=`date -d "1970-01-01 $YESTERDAY sec" +"%Y%m%d"`
FECHA=`date +"%Y%m%d"`

FILENAME="$FECHA.log"
FILE_LINE="$FECHA.last"
FILE="/apps/sittel/rawdata/mitel.$FECHA"

# Limpiar carpeta tmp

if [ -e "tmp/$AYER.lnum" ]; then
    rm tmp/${AYER}.*
fi

# Si existe el archivo con el numero de laultima linea se procesa

if [ -e "tmp/$FECHA.lnum" ]; then

    # Se lee el numero de la linea y se extrae un archivo con las lineas apartir
    # de la ultima busqueda que se hizo, posteriormente se les hace un grep

    while read line
    do
            tail -n +$line $FILE > "tmp/$FECHA.hal"
    done < "tmp/$FECHA.lnum"
    cd tmp

    grep -n " 00[0|2-9][0-9]\{4,\}" "tmp/$FECHA.hal" > "tmp/${FECHA}.new"

    grep -n " 900[0|2-9][0-9]\{4,\}" "tmp/$FECHA.hal" >> "tmp/${FECHA}.new"

    cd ..
    echo `pwd`
    cat tmp/${FECHA}.new >> logs/$FILENAME

else
    # Este caso es la primera vez que se ejecuta, verifica si el log ya existe
    # de ser asi, lo elimina para evitar duplicados

    if [ -e "logs/$FILENAME" ]; then
            rm "logs/$FILENAME"
    fi

    # Se realiza un grep en el archivo indicado y se marca el archivo de salida
    # se busca todos los numeros k empiecen con 00 seguidos de 0 a 9 execpto el 1

    cd tmp

    grep -n " 00[0|2-9][0-9]\{4,\}" $FILE>"${FECHA}.new"

    grep -n " 900[0|2-9][0-9]\{4,\}" $FILE>>"${FECHA}.new"

    cat ${FECHA}.new >> $FILENAME

    mv $FILENAME ../logs

    cd ..
fi

    cp "tmp/${FECHA}.new" "tmp/message.txt"

    echo "Mensaje" | mail -s "$SUBJECT" "$EMAIL" < "tmp/message.txt"

fi

if [ -e "tmp/${FECHA}.new" ]; then
    rm "tmp/${FECHA}.new"
fi

tail -n1 "logs/$FILENAME" > "tmp/$FILE_LINE"   

IFS=$':'
while read line
do
    DATOS=($line)
    LINE_NUMBER=${DATOS[0]}   
    echo $LINE_NUMBER > "tmp/$FECHA.lnum"
done < "tmp/$FILE_LINE"
unset IFS

and this is what the system prints:

/apps/sittel/Alarma/callAlarm: line 56: cd: tmp: No such file or directory
mv: cannot move `20110712.log' to `../logs': Permission denied
/apps/sittel/Alarma/callAlarm: line 69: tmp/20110712.last: No such file or directory
/apps/sittel/Alarma/callAlarm: line 77: tmp/20110712.last: No such file or directory

解决方案

Your script assumes that it is being run from a particular directory (note that almost every path is a relative path, not an absolute path). cron happens to be running it from another directory.

The Fix

If the script works when you run it from the directory it lives in, add the following to the top of your script:

mydir=$(dirname "$0") && cd "${mydir}" || exit 1

Explanation

$0 is the (possibly relative) filename of the shell script being executed. Given a filename, the dirname command returns the directory containing the filename.

So, that line changes directories to the directory containing the script or exits with an error code if either dirname or cd fails.

这篇关于脚本从命令行运行,但失败的crontab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆