Crontab - 不记录?我该怎么做?

Crontab - 不记录?我该怎么做?

我一直在寻找问题的答案,但找不到正确的答案。所以我自己尝试了一下。

我有一台 Ubuntu 服务器,并尝试创建一个 TeamSpeak3 服务器 - 带有登录功能的检查文件。此脚本应每小时由 cronjob 自动启动一次。

我的检查脚本(selfcheck.sh):

#!/bin/bash

cd /home/teamspeak/s01
LOGFILE='/home/teamspeak/s01/mylog.selfcheck'

function logg(){
        echo -n `date +\%d.\%m.\%Y` `date +\%H:\%M:\%S` >>$LOGFILE
        echo " | `basename $0` $1" >>$LOGFILE
}


#COMMANDLINE_PARAMETERS="${1}" #add any command line parameters you want to pass here
DIR=/home/teamspeak/s01/
USER=teamspeak

logg "### GESTARTET ###"

if [ -e ts3server.pid ]; then
    if ( kill -0 $(cat ts3server.pid) 2> /dev/null ); then
        echo ""
        echo "      ###################"
        echo "      #Server is running#"
        echo "      ###################"
        echo ""
        status=1

        echo "`date +\%d.\%m.\%Y` `date +\%H:\%M:\%S` | Server is running" >>$LOGFILE

    else
        echo ""
        echo "      ###########################"
        echo "      #Server seems to have died#"
        echo "      ###########################"
        echo ""
        status=2

        echo "`date +\%d.\%m.\%Y` `date +\%H:\%M:\%S` | Server seems to have died" >>$LOGFILE

    fi
else
    echo ""
    echo "      ##########################"
    echo "      #The server isn't running#"
    echo "      ##########################"
    echo ""
    status=3

    echo "`date +\%d.\%m.\%Y` `date +\%H:\%M:\%S` | The server isn't running" >>$LOGFILE

fi

if [ $status = "2" ]; then
    echo ""
    echo "      ##############"
    echo "      #Status ist "$status"#"
    echo "`date +\%d.\%m.\%Y` `date +\%H:\%M:\%S` | Status ist "$status >>LOGFILE
    echo "      ###RE-START###"
    echo "`date +\%d.\%m.\%Y` `date +\%H:\%M:\%S` | RE-START" >>LOGFILE
    echo "      ##############"
    echo ""
    echo ""
    echo "      ######################"
    echo "      #Stopping the Server!#"
    echo "`date +\%d.\%m.\%Y` `date +\%H:\%M:\%S` | Stopping the Server!" >>LOGFILE
    echo "      ######################"
    echo ""
    ./ts3server_startscript.sh stop inifile=ts3server.ini
    echo ""
    echo "      ######################"
    echo "      #Starting the Server!#"
    echo "`date +\%d.\%m.\%Y` `date +\%H:\%M:\%S` | Starting the Server!" >>LOGFILE
    echo "      ######################"
    echo ""
    ./ts3server_startscript.sh start inifile=ts3server.ini


fi

logg "###  BEENDET  ###"

exit 0

现在我的 crontab 条目:

SHELL=/bin/bash
PATH=/usr/local/bin:/usr/local/sbin:/sbin:/usr/sbin:/bin:/usr/bin:/usr/bin/X11
# m h  dom mon dow   command
  */1 *   *   *   *    /home/teamspeak/s01/selfchek.sh >> /home/teamspeak/s01/cronlog

此 crontab 正在加载,但“cronlog”文件中没有输出。它是空的。我的“mylog.selfcheck”也没有输出。但如果我手动执行脚本 (selfcheck.sh),则会有输出到“mylog.selfchek”。

我很抱歉我的语言不当......你知道这句话......

我希望有人能帮助我。

答案1

如果您设置了 cron,*/1 * * * *cron 将每分钟运行一次。将 cron 设置* */1 * * *为每小时运行一次。

编辑后重新启动 cron

sudo service cron restart

检查 cron 工作如何运行

crontab -l | grep -v '^#' | cut -f 6- -d ' ' | while read CMD; do eval $CMD; done

就像您已知的日志可以检查一样

less /var/log/syslog | grep cron

编辑1

根据你的评论,cron 工作。你还说,当你运行脚本时,你会LOGFILE='/home/teamspeak/s01/mylog.selfcheck'在脚本中定义的日志文件中输出。好的。

mylog.selfcheck您将不需要从中获取相同的内容cronlog。然后像您已经做的那样设置 cron 来运行脚本selfchek.sh,并在 cron 中添加第二个命令以将内容从 添加mylog.selfcheckcronlog

 * */1 * * * cat /home/teamspeak/s01/mylog.selfcheck >> /home/teamspeak/s01/cronlog

最终的 cron 代码

SHELL=/bin/bash
PATH=/usr/local/bin:/usr/local/sbin:/sbin:/usr/sbin:/bin:/usr/bin:/usr/bin/X11
# m h  dom mon dow   command
  * */1 * * *    /home/teamspeak/s01/selfchek.sh
  * */1 * * *    cat /home/teamspeak/s01/mylog.selfcheck >> /home/teamspeak/s01/cronlog

相关内容