LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-14-2019, 11:48 AM   #1
andres_fever
LQ Newbie
 
Registered: Sep 2018
Posts: 14

Rep: Reputation: Disabled
Unhappy Error in crontab exporting mysql query in bash script


Good day

I use a bash script to export the contents of a mysql database called QoS through an automatic connection through the conexion.cnf file that contains the credentials of my root user. When I execute my script manually it executes correctly and exports the file in the specified location, with name $fecha.xlsx but when the crontab executes nothing happens.

Code:
#!/bin/bash
. /home/andres/.bash_profile
cd /home/andres/BD/Scripts

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

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

crontab:
*/5 * * * * /home/andres/BD/Scripts/Throughputprueba.sh > /dev/null

Thanks very much.
 
Old 01-14-2019, 12:00 PM   #2
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,708

Rep: Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210
Quote:
Originally Posted by andres_fever View Post
Good day

I use a bash script to export the contents of a mysql database called QoS through an automatic connection through the conexion.cnf file that contains the credentials of my root user. When I execute my script manually it executes correctly and exports the file in the specified location, with name $fecha.xlsx but when the crontab executes nothing happens.

Code:
#!/bin/bash
. /home/andres/.bash_profile
cd /home/andres/BD/Scripts

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

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

crontab:
*/5 * * * * /home/andres/BD/Scripts/Throughputprueba.sh > /dev/null

Thanks very much.
Why are you directing the output of the script to /dev/null ??

It's probably working just fine, but as you're sending the output to the bit-bucket, you'll never see it.
 
Old 01-14-2019, 01:34 PM   #3
andres_fever
LQ Newbie
 
Registered: Sep 2018
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by scasey View Post
Why are you directing the output of the script to /dev/null ??

It's probably working just fine, but as you're sending the output to the bit-bucket, you'll never see it.

To exit the file, change from / dev / null to a .log. I have added set -xv to the script to debug and it responds in the log that the redirect is ambiguous.

*/2 * * * * /home/andres/BD/Scripts/Throughputprueba.sh > /home/andres/BD/Scripts/cron_error.log 2>&1
 
Old 01-14-2019, 05:03 PM   #4
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,708

Rep: Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210
Quote:
Originally Posted by andres_fever View Post
To exit the file, change from / dev / null to a .log. I have added set -xv to the script to debug and it responds in the log that the redirect is ambiguous.

*/2 * * * * /home/andres/BD/Scripts/Throughputprueba.sh > /home/andres/BD/Scripts/cron_error.log 2>&1
Which it is...you've coded two redirects, one to a log and one for STDERR to STDOUT

What are you trying to accomplish with a redirect in crontab at all?

Your script contains a redirect. Your crontab entry should just be
Code:
*/2 * * * * /home/andres/BD/Scripts/Throughputprueba.sh
Then the script will do from cron what it does from the command line.
 
1 members found this post helpful.
Old 01-15-2019, 11:34 AM   #5
andres_fever
LQ Newbie
 
Registered: Sep 2018
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by scasey View Post
Which it is...you've coded two redirects, one to a log and one for STDERR to STDOUT

What are you trying to accomplish with a redirect in crontab at all?

Your script contains a redirect. Your crontab entry should just be
Code:
*/2 * * * * /home/andres/BD/Scripts/Throughputprueba.sh
Then the script will do from cron what it does from the command line.

Redirecting the crontab's output was to rule out certain things. The solution was to leave the crontab like this

*/10 * * * * sh /home/andres/Proyecto/Scripts/Perdida/QoSperdida.sh > /dev/null

dev/null, it's optional.
 
Old 01-15-2019, 01:03 PM   #6
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,708

Rep: Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210
Quote:
Originally Posted by andres_fever View Post
Redirecting the crontab's output was to rule out certain things. The solution was to leave the crontab like this

*/10 * * * * sh /home/andres/Proyecto/Scripts/Perdida/QoSperdida.sh > /dev/null

dev/null, it's optional.
Um. What?? That's the line in your OP that you said didn't work! Now it works?
 
Old 01-15-2019, 10:27 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,348

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
1. There are 2 completely different scripts mentioned here ....

2.
Code:
prog >prog.log 2>&1
is perfectly fine re-direction - o/p (stdout/chan 1) goes to the log, stderr/chan 2 goes to same target...
 
1 members found this post helpful.
Old 01-16-2019, 01:44 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,685

Rep: Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274
so do a redirection like in post #3 and check what is in the error log.
 
Old 01-17-2019, 03:27 PM   #9
andres_fever
LQ Newbie
 
Registered: Sep 2018
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
so do a redirection like in post #3 and check what is in the error log.
Thanks for the information.
 
Old 01-17-2019, 03:30 PM   #10
andres_fever
LQ Newbie
 
Registered: Sep 2018
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by scasey View Post
Um. What?? That's the line in your OP that you said didn't work! Now it works?
The script was executed from the crontab adding sh before the directory address in the crontab. Regards
 
  


Reply

Tags
bash, crontab, mysql, script


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Exporting the output of a TSQL query to a CSV file - SQL server anooprerna Linux - Server 2 08-18-2017 02:41 AM
[SOLVED] Problem in exporting variable from bash script to expect script uk.engr Linux - Newbie 3 06-14-2012 01:57 AM
Can MySQL log on via SSH/bash? mysql:x:27:101:MySQL Server:/var/lib/mysql:/bin/bash Ujjain Linux - Newbie 2 04-24-2009 02:21 PM
help with mysql query: return nth rows in query hawarden Programming 2 07-31-2006 06:36 PM
exporting one NFS folder to another NFS and exporting it.... stellarmarine1 Linux - Networking 3 09-20-2004 06:54 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 05:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration