views:

583

answers:

6

I need to run a specific mysql query upon wget, which is running in the background, finishes downloading a file. For example...

wget http://domain/file.zip

then run:

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

How would I go about doing this?

A: 

Well, you could just put it in a shell script.

I don't know enlough about Shell to tell you how though

Zachary Spencer
A: 

Why not use file_get_contents or curl to download the file from within 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'");
Tobias Cohen
The files are incredibly large and we're doing several downloads at a time. wget is required so that it downloads in the background and doesn't rely on a PHP script.
Don Wilson
A: 

You can script it in Python thus...

#!/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. 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').

In unix you can run the script in the background by typing 'python scriptname.py &'

+1  A: 

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

Max Kosyakov
Although, depending on the system, you might not want to put your password directly onto the command line.
mysql -u user --password=`cat my_secret_password.txt` database ...
Max Kosyakov
@Max Kosyakov: You're mislead as to what you're trying to keep safe. Putting the password in a script file or in a separate password file is really no different whatsoever. Both files can be properly secured with permissions. The PROBLEM is that you should NEVER put passwords in **arguments to commands**. And you're still doing that. All commands are visible to anyone with enough skill to convince a server to show them a process list (no, he really doesn't **need** ssh access).
lhunath
+1  A: 

In bash it would likely be as simple as:

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

With the .php file containing your MySQL query.

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.

AvatarKava
+2  A: 

I would recommend launching the wget command in a subshell so you can ensure it completes before you run your mysql command. In bash, this is done with parentheses.

Also use && so that the mysql command runs only if the wget command has a success exit status.

#!/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.

Instead, you can rely on the [client] section in your ~/.my.cnf file. You can also use the MYSQL_PWD environment variable. Either of these solutions means there's no password visible in the command listing.

Bill Karwin
lhunath
The problem isn't as much putting the password in the script (which can be properly secured using correct permissions), it's putting the password on the commandline for everyone to see (where everyone means, anyone able to run `ps` or find another way to query the process list for instance by exploiting some bug in your webserver or so). Word this important security issue stronger and you get a +1 from me.
lhunath
@lhunath: Yes, good point about the ps listing. Though from the OP's question, I'm not assuming there's any web server involved on this machine.
Bill Karwin