简体   繁体   English

wget完成下载文件后运行MySQL查询

[英]Run MySQL Query after wget finishes downloading file

I need to run a specific mysql query upon wget, which is running in the background, finishes downloading a file. 我需要在后台运行的wget上运行特定的mysql查询,以完成文件下载。 For example... 例如...

wget http://domain/file.zip wget http://domain/file.zip

then run: 然后运行:

UPDATE table SET status = 'live' WHERE id = '1234' UPDATE table SET status ='live'WHERE id ='1234'

How would I go about doing this? 我将如何去做呢?

I would recommend launching the wget command in a subshell so you can ensure it completes before you run your mysql command. 我建议在子外壳中启动wget命令,以便在运行mysql命令之前确保它已完成。 In bash, this is done with parentheses. 在bash中,这是用括号完成的。

Also use && so that the mysql command runs only if the wget command has a success exit status. 还使用&&以便仅在wget命令具有成功退出状态时才运行mysql命令。

#!/bin/sh
(
  wget "http://domain/file.zip" && mysql -u user -ppassword database -e "UPDATE..."
) &

Putting the password in plaintext in the script is not necessarily a good idea because it can be viewed in a ps listing. 在脚本中以纯文本形式输入密码不一定是一个好主意,因为可以在ps列表中进行查看。

Instead, you can rely on the [client] section in your ~/.my.cnf file. 相反,您可以依赖~/.my.cnf文件中的[client]部分。 You can also use the MYSQL_PWD environment variable. 您也可以使用MYSQL_PWD环境变量。 Either of these solutions means there's no password visible in the command listing. 这些解决方案中的任何一个都意味着在命令列表中没有可见的密码。

wget http://domain/file.zip && mysql -u user -ppassword database -e "UPDATE table SET status = 'live' WHERE id = '1234'"

In bash it would likely be as simple as: 在bash中,它可能很简单:

#!/bin/bash
wget -q http://domain.tld/file.zip || exit 0
/usr/bin/php somefile.php

With the .php file containing your MySQL query. 使用包含您的MySQL查询的.php文件。

You can also run the MySQL statement direct via the MySQL client (that's already been posted), but you should be wary of having your MySQL password in your syslog/history. 您也可以直接通过MySQL客户端(已发布)运行MySQL语句,但是您应该避免在syslog / history中输入MySQL密码。

Well, you could just put it in a shell script. 好吧,您可以将其放在shell脚本中。

I don't know enlough about Shell to tell you how though 我不了解壳牌如何告诉你

Why not use file_get_contents or curl to download the file from within PHP? 为什么不使用file_get_contentscurl从PHP内下载文件?

$data = file_get_contents('http://domain/file.zip');
file_put_contents('file.zip', $data);
mysql_query("UPDATE table SET status = 'live' WHERE id = '1234'");

You can script it in Python thus... 您可以使用Python编写脚本,从而...

#!/usr/bin/python
import sys, MySQLdb, urllib

urllib.urlretrieve(sys.argv[1], sys.argv[2])
db = MySQLdb.connect(host="localhost", user="username", passwd="password", db="database")
cursor = db.cursor()
cursor.execute("UPDATE table SET status = 'live' WHERE id = '1234'")

This takes the source URL as a first argument and the destination filename as the second. 这将源URL作为第一个参数,将目标文件名作为第二个参数。 Do note this uses Pythons own URL retrieval library urllib, if for any reason you have to use wget specifically you you could import the 'os' module and use os.system('your command line'). 请注意,它使用Python自己的URL检索库urllib,如果出于某种原因必须专门使用wget,则可以导入'os'模块并使用os.system('your command line')。

In unix you can run the script in the background by typing 'python scriptname.py &' 在Unix中,您可以通过键入“ python scriptname.py&”在后台运行脚本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM