The following Plesk Scheduled Task PHP script can be used to run SQL stored in an external text file via the Plesk Scheduled Tasks option for a specified domain hosted on your Plesk server.
The script accepts an argument which is the name of the SQL script text file to execute. Arguments are passed to the PHP binary in Plesk via the $argv array. An interesting note is that the first element in this array is always the PHP script name.
The script makes the assumption that it is being run from a folder called mw which is in the root of the domain and outside of the public httpdocs folder, but this can be changed to whatever you like.
Currently, the database along with the database username and password are hardcoded but these could easily be parameterised and passed into the code via the Plesk arguments field / $argv array discussed above; It is my intention to modify the code to include this change in a future iteration.
Plesk scheduled task PHP script presented here is useful if you want to run batch data processing jobs on your web server at a pre-defined interval.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
#----------------------------------------------------------------------------# # Program Copyright : Mike Wilcock. # Program Name : sql_file_import.php #----------------------------------------------------------------------------# # Program Created : 3rd February 2024. # Program Code Type : PHP. # Program Author : Michael Wilcock. # Program Version : 1.10 #----------------------------------------------------------------------------# # Purpose : Execute a SQL script stored in an external file from PHP. #----------------------------------------------------------------------------# # Notes : Script to execute is passed into PHP as a parameter # named sql_file. # # Plesk passes arguments to the PHP binary as the array: # $argv, the first element of which is always the script name. #----------------------------------------------------------------------------# $db_host = "localhost"; $db_database = "<your_db>"; $db_user = "<your_username>"; $db_password = "<your_password>"; // Check that a parameter has been provided. if(isset($argv[1])) { // It has so store it in memory. $sql_file = "mw/" . $argv[1]; } // No parameter passed so terminate script. else { echo "Parameter sql_file not passed"; // Exit here. return; } // Parameter passed but file does not exist. if(!file_exists("$sql_file")) { echo "Parameter sql_file is not valid"; // Exit here. return; } // If we get here we are good to go! // Create new PDO database connection. $db = new PDO("mysql:host=$db_host;dbname=$db_database", $db_user, $db_password); // Enable PDO error output. This gives us a clue when things go wrong! $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Load our script into memory. $query = file_get_contents($sql_file); // Prepare SQL statement. $stmt = $db->prepare($query); // Execute SQL code. if($stmt->execute()) { echo $sql_file . " Executed Successfully :-)"; } // Problem! else { echo $sql_file . " Failed with errors! :-0"; } |
Documentation
Click the Webhelp button to view the application help documentation.