Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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:

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

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

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?

$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...

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top