search

Home  >  Q&A  >  body text

linux - crontab定时任务不执行

系统是centos,发现crontab定时任务不执行
登录系统用手工执行脚本就可以
其它如crontab配置文件没有对用户做限制,
脚本权限也赋予执行,用root账户执行,但是最后还是不行,
请各位大神指点一下,下面是Log和脚本内容

Feb  7 14:10:01 1280859075761a8Z crond[12517]: (root) CMD (/usr/lib/sa/sa1 1 1)
Feb  7 14:20:01 1280859075761a8Z crond[12813]: (root) CMD (/usr/lib/sa/sa1 1 1)
Feb  7 14:30:01 1280859075761a8Z crond[13115]: (root) CMD (/usr/lib/sa/sa1 1 1)
Feb  7 14:40:01 1280859075761a8Z crond[13471]: (root) CMD (/usr/lib/sa/sa1 1 1)
Feb  7 14:41:02 1280859075761a8Z crontab[13510]: (root) BEGIN EDIT (root)
Feb  7 14:41:47 1280859075761a8Z crontab[13510]: (root) END EDIT (root)
Feb  7 14:41:51 1280859075761a8Z crontab[13534]: (root) BEGIN EDIT (root)
Feb  7 14:42:24 1280859075761a8Z crontab[13534]: (root) REPLACE (root)
Feb  7 14:42:24 1280859075761a8Z crontab[13534]: (root) END EDIT (root)
Feb  7 14:42:40 1280859075761a8Z crontab[13564]: (root) LIST (root)
Feb  7 14:43:01 1280859075761a8Z crond[18021]: (root) RELOAD (cron/root)
Feb  7 14:45:01 1280859075761a8Z crond[13638]: (root) CMD (/backuptoqiniu/backuptoqiniu.sh )
Feb  7 14:50:01 AY1311280859075761a8Z crond[13791]: (root) CMD (/usr/lib/sa/sa1 1 1)
Feb  7 15:00:01 AY1311280859075761a8Z crond[14091]: (root) CMD (/usr/lib/sa/sa1 1 1)
Feb  7 15:01:01 AY1311280859075761a8Z crond[14120]: (root) CMD (run-parts /etc/cron.hourly)

shell code

#!/bin/bash
BACKUP_SRC="/home/wwwroot/tes" 
MYSQL_SERVER="127.0.0.1"
MYSQL_USER="test" 
MYSQL_PASS="tset" 
DATEBASE="tee" 
HOST="544" 
NOW=$(date +"%Y-%m-%d-%H")
echo "start dump mysql"
ases.sql" -u $MYSQL_USER -h $MYSQL_SERVER -p$MYSQL_PASS $DATEBASE > "$NOW-Databecho "dump ok"
echo "start tar"
tar -cPzf $HOST-$NOW-backup.tar.gz $NOW-Databases.sql "$BACKUP_SRC"
echo "tar ok"
python /root/backuptoqiniu/upload.py
rm -f $NOW-Databases.sql $HOST-$NOW-backup.tar.gz
echo "ALL ok"

crontab command

45 14 * * * /backuptoqiniu/backuptoqiniu.sh
怪我咯怪我咯2788 days ago777

reply all(5)I'll reply

  • 怪我咯

    怪我咯2017-04-17 11:38:11

    Problem solving ideas:
    1. To determine whether crontab has been executed, you can add a small script to write files that is executed every minute for testing. If there is a record, it means that there is no problem with the crontab itself.

    For example: Feb 7 14:45:01 1280859075761a8Z crond[13638]: (root) CMD (/backuptoqiniu/backuptoqiniu.sh )This should indicate that crontab has been successfully executed, so there is no problem with the crontab service.
    2. In fact, most experienced people will know that there are no relevant environment variables for the execution of crontab. The solution is to log in the script. In addition, by default, all commands will use the full path .

    Debugging method: add a log file, try to append all the original echoes to the log file, or append the log when crontab is writing.
    45 14 * * * /backuptoqiniu/backuptoqiniu.sh >> /tmp/out.log 2>&1

    ases.sql" -u $MYSQL_USER -h $MYSQL_SERVER -p$MYSQL_PASS $DATEBASE > "$NOW-Datab
    I don’t understand what you mean. Is your command not included in the default system commands?

    Test method: Test that the PATH of crontab is different from the PATH of manual execution.

    可以在crontab的脚本里面添加个echo $PATH > /tmp/1.log
    对比和你手动的终端下执行的echo $PATH
    

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 11:38:11

    The detailed troubleshooting process has been given upstairs. Most of the problems with general planned tasks are caused by environment variables.
    Either write the absolute path, or load /etc/profile

    at the beginning of the script

    reply
    0
  • 迷茫

    迷茫2017-04-17 11:38:11

    Add
    above in crontab MAILTO=""

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 11:38:11

    [root@mimvp-bj script]# service crond status
    crond is stopped
    [root@mimvp-bj script]# service crond start
    Starting crond: [ OK ]
    [root @mimvp-bj script]# service crond status
    crond (pid 24577) is running…

    Check if your crontab is open

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 11:38:11

    Add a situation

    If the system time is modified by UTC --> CST

    Need to restartcrontabservice

    sudo service cron restart

    Otherwisecrontab UTC time is still used, so if a specific hour is specified, it will not be executed

    reply
    0
  • Cancelreply