【问题标题】:Error in crontab to execute mysql query that exports an xlsx file in bash scriptcrontab 执行 mysql 查询时出错,该查询在 bash 脚本中导出 xlsx 文件
【发布时间】:2019-06-08 13:59:29
【问题描述】:

美好的一天

我使用 bash 脚本通过 conexion.cnf 文件的自动连接导出名为 QoS 的 mysql 数据库的内容,该文件包含我的 root 用户的凭据。当我手动执行我的脚本时,它会正确执行并将文件导出到指定位置,名称为 $fecha.xlsx 但是当 crontab 执行时没有任何反应。

#!/bin/bash
cd /home/andres/BD/Perdida

fecha=$(date +%Y-%m-%d' '%H:%M);

mysql --defaults-file=/etc/my.cnf.d/conexion.cnf -D QoS -e "SELECT * FROM Throughput_TX" > /home/andres/BD/Scripts/$fecha.xlsx

Crontab

* * * * * /bin/bash -lc /home/andres/BD/Scripts/ Throughputprueba.sh > /dev/null

我使用root用户。

非常感谢。

【问题讨论】:

    标签: mysql bash cron centos7


    【解决方案1】:

    还有两点:

    1. 您过于频繁地运行脚本。检查运行需要多少时间 脚本并使运行频率小于此值
    2. 您的脚本中有一个空格,这将导致永远不会执行 外壳脚本

      * * * * * /bin/bash -lc /home/andres/BD/Scripts/ Throughputprueba.sh ^

    而且你不需要再运行一次 bash,像这样运行 cron:

    * * * * * /home/andres/BD/Scripts/Throughputprueba.sh
    

    【讨论】:

    • 您好,我已经更正了您向我指出的内容,但它不起作用。
    • @AndresAndrade,您是否在脚本中添加了.bash_profile 和/或.bashrc
    • @AndresAndrade,并查看 cron 日志以获取更多信息。
    • Crontab */3 * * * * /home/andres/BD/Scripts/Throughputprueba.sh > /dev/null 代码 #!/bin/bash . /home/andres/.bash_profile cd /home/andres/BD/Scripts fecha=$(date +%Y-%m-%d' '%H:%M); mysql --defaults-file=/etc/my.cnf.d/conexion.cnf -D QoS -e "SELECT * FROM (SELECT * FROM Throughput_TX ORDER BY fecha DESC LIMIT 5) sub ORDER BY fecha ASC" > /home/ andres/BD/Scripts/$fecha.xlsx
    • 解决方法是:*/10 * * * * sh /home/andres/Proyecto/Scripts/Perdida/QoSperdida.sh > /dev/nul
    【解决方案2】:

    您的环境变量可能未设置。

    您首先需要设置适当的环境变量,然后执行您的脚本。

    试试

    #!/bin/bash
    . /home/andres/.bash_profile #<-- this part need to address your bash_profile.
    cd /home/andres/BD/Perdida
    
    fecha=$(date +%Y-%m-%d' '%H:%M);
    
    mysql --defaults-file=/etc/my.cnf.d/conexion.cnf -D QoS -e "SELECT * FROM Throughput_TX" > /home/andres/BD/Scripts/$fecha.xlsx
    

    或者你给出mysql的完整路径。

    whereis mysql
    

    获取路径并在前面添加/&lt;full-path&gt;/mysql --defaults-file=...

    编辑:你的 cron 有语法错误

    * * * * * /bin/bash -lc /home/andres/BD/Scripts/ Throughputprueba.sh > /dev/null
    

    * * * * * /bin/bash -lc /home/andres/BD/Scripts/Throughputprueba.sh > /dev/null
    

    【讨论】:

    • 输入代码时whereis mysql回复mysql:/usr/bin/mysql/usr/lib/mysql/usr/share/mysql/usr/share/man/man1/mysql.1.gz因此将其添加到进行查询的行的开头并且它不起作用。 #!/bin/bash . /home/andres/.bash_profile cd /home/andres/BD/Scripts fecha=$(date +%Y-%m-%d' '%H:%M); //bin/mysql /usr/lib/mysql /usr/share/mysql /usr/share/man/man1/mysql.1.gz>/mysql --defaults-file=/etc/my.cnf. d/conexion.cnf -D QoS -e "SELECT * FROM Perdida" > /home/andres/BD/Scripts/$fecha.xlsx
    • /usr/bin/mysql -defaults-file=/etc/my.cnf.d/conexion.cnf -D QoS -e "SELECT * FROM Throughput_TX" &gt; /home/andres/BD/Scripts/$fecha.xlsx
    • 它不起作用。 /usr/bin/mysql --defaults-file=/etc/my.cnf.d/conexion.cnf -D QoS -e "SELECT * FROM (SELECT * FROM Throughput_TX ORDER BY fecha DESC LIMIT 5) sub ORDER BY fecha ASC " > /home/andres/BD/Scripts/$fecha.xlsx 进入 cront 日志,但我对此不太了解。
    • 解决方法是:*/10 * * * * sh /home/andres/Proyecto/Scripts/Perdida/QoSperdida.sh > /dev/null
    猜你喜欢
    • 2012-11-04
    • 2011-07-27
    • 1970-01-01
    • 2018-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多