• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

SQL Cronjob Question

Joined
Jul 21, 2008
Messages
5,177 (0.90/day)
System Name [Daily Driver]
Processor [Ryzen 7 5800X3D]
Motherboard [Asus TUF GAMING X570-PLUS]
Cooling [be quiet! Dark Rock Slim]
Memory [64GB Corsair Vengeance LPX 3600MHz (16GBx4)]
Video Card(s) [PNY RTX 3070Ti XLR8]
Storage [1TB SN850 NVMe, 4TB 990 Pro NVMe, 2TB 870 EVO SSD, 2TB SA510 SSD]
Display(s) [2x 27" HP X27q at 1440p]
Case [Fractal Meshify-C]
Audio Device(s) [Steelseries Arctis Pro]
Power Supply [CORSAIR RMx 1000]
Mouse [Logitech G Pro Wireless]
Keyboard [Logitech G512 Carbon (GX-Brown)]
Software [Windows 11 64-Bit]
I'm trying to figure out the best way to do a cronjob on a mysql table.

I have a column called CharID and I need to SELECT that for each row and use it as a variable in an XML query. The xml query is an API call that I'm using to get an attribute from the CharID. Depending on this attribute I want to update another row in the table.

My issue is I'm not sure how to get it to loop thru every row. I know how to do it for one row on its own but not for all the rows.

I'm most comfortable with PHP so that's preferred but willing to look at other options.
 

brandonwh64

Addicted to Bacon and StarCrunches!!!
Joined
Sep 6, 2009
Messages
19,542 (3.64/day)
Try creating a shell script like the one below:

#!/bin/bash

mysql --user=[username]--password=[password]--database=[db name]--execute="DELETE FROM tbl_message WHERE DATEDIFF( NOW( ) , timestamp ) >=7"

You can then add this to the cron
 
Joined
Aug 10, 2007
Messages
4,267 (0.70/day)
Location
Sanford, FL, USA
Processor Intel i5-6600
Motherboard ASRock H170M-ITX
Cooling Cooler Master Geminii S524
Memory G.Skill DDR4-2133 16GB (8GB x 2)
Video Card(s) Gigabyte R9-380X 4GB
Storage Samsung 950 EVO 250GB (mSATA)
Display(s) LG 29UM69G-B 2560x1080 IPS
Case Lian Li PC-Q25
Audio Device(s) Realtek ALC892
Power Supply Seasonic SS-460FL2
Mouse Logitech G700s
Keyboard Logitech G110
Software Windows 10 Pro
What does that one row query look like?
 

W1zzard

Administrator
Staff member
Joined
May 14, 2004
Messages
27,080 (3.71/day)
Processor Ryzen 7 5700X
Memory 48 GB
Video Card(s) RTX 4080
Storage 2x HDD RAID 1, 3x M.2 NVMe
Display(s) 30" 2560x1600 + 19" 1280x1024
Software Windows 10 64-bit
I'm most comfortable with PHP so that's preferred but willing to look at other options.
Write a PHP script that does what you need and then call that from cron (php can run from the command line just fine)
 
Joined
Jul 21, 2008
Messages
5,177 (0.90/day)
System Name [Daily Driver]
Processor [Ryzen 7 5800X3D]
Motherboard [Asus TUF GAMING X570-PLUS]
Cooling [be quiet! Dark Rock Slim]
Memory [64GB Corsair Vengeance LPX 3600MHz (16GBx4)]
Video Card(s) [PNY RTX 3070Ti XLR8]
Storage [1TB SN850 NVMe, 4TB 990 Pro NVMe, 2TB 870 EVO SSD, 2TB SA510 SSD]
Display(s) [2x 27" HP X27q at 1440p]
Case [Fractal Meshify-C]
Audio Device(s) [Steelseries Arctis Pro]
Power Supply [CORSAIR RMx 1000]
Mouse [Logitech G Pro Wireless]
Keyboard [Logitech G512 Carbon (GX-Brown)]
Software [Windows 11 64-Bit]
Code:
<?php
function makeApiRequest($url) {

    // Initialize a new request for this URL
    $ch = curl_init($url);

    // Set the options for this request
    curl_setopt_array($ch, array(
        CURLOPT_FOLLOWLOCATION => true, // Yes, we want to follow a redirect
        CURLOPT_RETURNTRANSFER => true, // Yes, we want that curl_exec returns the fetched data
        CURLOPT_SSL_VERIFYPEER => false, // Do not verify the SSL certificate
    ));

    // Fetch the data from the URL
    $data = curl_exec($ch);

    // Close the connection
    curl_close($ch);

    // Return a new SimpleXMLElement based upon the received data
    try {
        return new SimpleXMLElement($data);
    }
    // In case of failure, simulate an error document to get a
    // SimpleXMLElement object in any case
    catch (Exception $e) {
        $time = gmdate('Y-m-d H:i:s');
        return new SimpleXMLElement("<?xml version='1.0' encoding='UTF-8'?>
            <eveapi version=\"2\">
             <currentTime>$time</currentTime>
             <error code=\"{$e->getCode()}\">{$e->getMessage()}</error>
             <cachedUntil>$time</cachedUntil>
            </eveapi>");
    }
}

$con=mysqli_connect("localhost","root","redacted","services");

$query = 'SELECT charid FROM members ORDER BY updatedAt DESC;';

$result = mysqli_query($con,$query);

// Corp Check

while( $row = mysqli_fetch_row($result) ) {
 
                                        $url = 'https://api.eveonline.com/eve/CharacterInfo.xml.aspx';
                                        $url .= '?characterID=' . $row[0];
                                     
                                        $xml = makeApiRequest($url);
                                        $corpid = $xml->xpath('//result/corporationID/text()')[0];
                                     
                                        $query2 = "UPDATE members SET corpid = $corpid WHERE charid = $row[0] LIMIT 1";

                                        $result2 = mysqli_query($con,$query2);

  if (!$result2) {
    die('Invalid query: ' . mysqli_error($con));
}

}
?>

works just fine when I call it in a terminal or as a webpage.. the issue is when I put it in crontab -e as..

*/30 * * * * /var/www/auth/includes/cron/accessupdate.php

when it runs (which it does according to the logs) it doesn't do anything.

UPDATE

fixed this, needed to include the path to my php install
 
Last edited:
Top