Reputation: 1433
I added jobs to crontab
.
20 2 * * * sh /home/tomcat/bin/test3.sh
20 2 * * * sh /home/tomcat/bin/test.sh
I checked the permissions..
-rwxr-xr-x 1 root root 58 May 19 02:04 test3.sh
-rwxr-xr-x 1 root root 3223 May 16 08:20 test.sh
And test3.sh
is working from both command line and crontab
.
#!/bin/sh
echo "test test test..." > /test3_sh.txt
But test.sh
is not working from crontab
. The code seems little complicated and messy but the purpose is simple. It retrieves data from MYSQL and send a mail via sendmail
. When I run test.sh
from command line it's working, but from crontab
it's not.
#!/bin/sh
results=`mysql -N -h mysqlhost.com -u user -pmypassword -e "USE MYDB;
SELECT
column1,
column2,
column3,
column4
FROM MYTABLE
ORDER BY REG_DT DESC
LIMIT 10;"`
IFS=$'
'
x="<style type="text/css">
table.gridtable {
font-family: verdana,arial,sans-serif;
font-size:11px;
color:#333333;
border-width: 1px;
border-color: #666666;
border-collapse: collapse;
}
table.gridtable th {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #666666;
background-color: #dedede;
}
table.gridtable td {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #666666;
background-color: #ffffff;
}
</style><table class="gridtable">
<tr>
<th>column 1</th><th>column 2</th><th>column 3</th><th>column 4</th>
</tr>"
for i in $results;
do
x+="<tr>"
x+="$i
"
x+="</tr>"
done
x+="</table>"
cat - /home/tomcat/bin/test.log << EOF | sendmail -t -f [email protected]
to:[email protected]
from:[email protected]
subject:test mail
Mime-Version: 1.0
Content-Type: text/html
<html>
<body>
<h3>my recent articles..</h3>
$x
<br><hr>
</body>
</html>
EOF
I googled this problem and some said, some pahts and ENV variables work differently from command line and crontab. But there's nothing like ENV variables in my sh. There's a log path in my sh. It's little suspicious, but I'm not sure this could be a problem.
cat - /home/tomcat/bin/test.log << EOF | sendmail -t -f [email protected]
Upvotes: 0
Views: 442
Reputation: 69
You can also navigate to the path and run the script as>>
20 2 * * * cd /home/tomcat/bin/ && sh test3.sh
It will work
Upvotes: 1
Reputation: 1433
It was a path. I added this paths to crontab
and works well!
# path
SHELL=/bin/bash
PATH=/usr/local/bin/:/sbin:/bin:/usr/sbin:/usr/bin
LANG=en_US.UTF-8
Upvotes: 1
Reputation: 53545
Simply remove the sh
from the beginning of the command:
20 2 * * * /home/tomcat/bin/test3.sh
20 2 * * * /home/tomcat/bin/test.sh
Upvotes: 0