PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, November 13, 2022

[FIXED] How to get arguments from PLESK Cron jobs

 November 13, 2022     cron, php, plesk, scheduled-tasks     No comments   

Issue

I have created a Cron Job/Scheduled Task in PLESK 12 which I am passing the arguments 33 and On through using the arguments box. I am struggling to pick these up in the PHP document on the end of the cron job.

PLESK and Arguments

In the PHP document I have tried a number of things including $arg[0] and $argv[0]

$arg returned as being an undefined variable whilst $argv[0] does not error but also does not pass the arguments through successfully as the desired changed has not been made.

I have checked to ensure the PHP script is working and it works fine when the arguments are hard coded into the program but I want this to be dynamic.

<?PHP
include_once('xxx/xxx/xxx/db.php');
include('xxx/xxx/xxx/xxx/db.php');
$query = "UPDATE SQLCommand SET argument1 = '$argv[1]' WHERE argument2= $argv[0]";
$result = mysqli_query($connection,$query);

Can anyone explain why these are still not passing the arguments through.

Thanks


Solution

$argv[0] always contains the name of the script file, as it passed to the PHP binary. As per screenshot, $argv[1] is '33' and $argv[2] is 'On'. You can easily check with:

echo $argv[1];

Or you can list all arguments as an array by:

var_dump($argv);

Basically, the following task is added to crontab, when scheduled via Plesk:

/usr/bin/php5 -f '/test.php' -- '33' 'On'

If test.php contains mentioned commands, the result of its' execution will be the following:

# cat /test.php
<?php
echo "The first argument is $argv[1]\n";
echo "Here the full list of arguments:\n";
var_dump($argv);
?>
# /usr/bin/php5 -f '/test.php' -- '33' 'On'
The first argument is 33
Here the full list of arguments:
array(3) {
  [0]=>
  string(6) "/test.php"
  [1]=>
  string(2) "33"
  [2]=>
  string(2) "On"
}


Answered By - Elvis Plesky
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing