【问题标题】:Linux Script & MySql operations not working, When Invoked from other script从其他脚本调用时,Linux 脚本和 MySql 操作不起作用
【发布时间】:2014-12-18 10:26:04
【问题描述】:

我已经编写了这段代码来将行插入到表中(如果不存在),否则,将“cnt”再更新一次。这很好用,当我从命令行调用它时,当我从 shell 脚本调用它时,这不起作用。

file=$1
echo "$0: Path to store $file" ;
res=`mysql -e "use spam_sending_scripts; select * from spamemailcount where path = '$file' limit 0,1"`
echo $res
if [ -z "$res" ]
then
    mysql -e "use spam_sending_scripts; INSERT INTO spamemailcount (cnt,path) VALUES(1,'$file');"
    echo "Inserting into DB $file , res $res" ;
    exit ;
fi
mysql -e "use spam_sending_scripts; update spamemailcount SET cnt=cnt+1 where path = \"$file\"" ;
echo "Updating into DB $file" ;
#mysql -e "use spam_sending_scripts; select * from spamemailcount" >> /var/log/sendmail.log
mysql -e "use spam_sending_scripts; select * from spamemailcount"

root@server [/home]# insertintodb.sh AAA ==> 这工作正常。

当我从另一个脚本调用时,这个文件被执行,但是插入不起作用。

我像这样调用它:/home/insertintodb.sh $path

$path 变量正在正确地传递给 insertintodb.sh

我收到以下错误:

++ mysql -e 'use spam_sending_scripts; select * from spamemailcount where path = '\''hackerssquadron.com/wp-comments-post.php'\'' limit 0,1'
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) 

【问题讨论】:

  • /home/insertintodb.sh "$path" 呢?
  • 在调试模式下调用你的脚本:bash -x /home/insertintodb.sh "$path"
  • 您是否可能从其他用户调用?因为你总是需要~/.my.cnf[mysql] 文件来配置用户名和密码等,如果你在全局配置中没有默认登录/服务器。
  • 感谢您的意见。

标签: mysql linux bash shell


【解决方案1】:

使用chmod命令使其可执行

chmod ugo+x insertinodb.sh

然后尝试像@Jdamian 建议的那样称呼它

bash -x /home/insertintodb.sh "$path"

【讨论】:

  • chmod 777 使其可执行..... ?? -100,如果我能做到的话。这将使系统上的每个人都可以读写文件,这真的是我们想要的吗?不。要使其可执行,请使用chmod a+x,或者chmod 755750 可能更好,因为您可能也不希望这个世界可读)。
  • 已经提到兄弟@DanFromGermany 只是因为我没有给予太多关注。对不起
  • 感谢您的输入,我从命令行调用此脚本 bash -x insertintodb.sh "about.in/wp-cmets-post.php" ==> 工作正常。我能够调试脚本。 /bin/bash -x /home/insertintodb.sh $fpath >> $LOGFILE (像这样我从另一个脚本调用,但不起作用)
  • Jdamian & Skynet 感谢您的意见。我收到以下错误。 ++ mysql -e '使用垃圾邮件发送脚本; select * from spamemailcount where path = '\''hackerssquadron.com/wp-cmets-post.php'\'' limit 0,1' 错误 1045 (28000): Access denied for user 'root'@'localhost' (using密码:否)
  • @Mani 试试这个bash -x /home/insertintodb.sh $fpath >> $LOGFILE
【解决方案2】:

这个问题解决了。

  1. 当我尝试从命令行调用脚本时,它工作正常。 原因 => 我以 root 身份运行此脚本。所以没问题

  2. 当我使用另一个 shell 脚本调用该脚本时,它不起作用。 原因:这个主脚本正在被apache调用,它正在尝试访问根数据库,所以权限被拒绝。

根据 Skynet 和 Jdamian 的输入,我能够调试并解决它。

非常感谢您的支持,像往常一样,这是让专家快速解决技术问题的最佳场所之一。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-12
    • 1970-01-01
    • 1970-01-01
    • 2014-01-05
    • 1970-01-01
    • 1970-01-01
    • 2014-04-18
    • 1970-01-01
    相关资源
    最近更新 更多