MyBB Community Forums

Full Version: Task questions please
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
I searched for this and checked the Wiki, didn't see what I'm looking for.

'generic' cron example
6 3 * * * /usr/local/bin/php -q /home/sample/public_html/ftsy/runjob.php


'generic' MyBB task manager code sample
<?php
function task_sample($task)
{

sample code here;
    
    add_task_log($task, "My user queries successfully ran!");
}
?>

Questions
1) How can I use the task manager to trigger a PHP script, located at
/home/sample/public_html/ftsy/runjob.php ?

(Note: Please, help with question #1 even if #2 and #3 are left hanging)

2) Ideally the script needs a variable ie. runjob.php?jid=2
3) If it is possible to run a script from the task manager, and the variable is set
$jid= 2
then how to pass the variable in the task_sample.php script?

Thank you very much.
1). I don't think you can. The Task Manager only picks up files that are in the ./inc/tasks/ folder.

2). That's possible with cron jobs, no?

You could try it another way (I use this in a game). Basically I update the database from when the update last took place, then in my script I have:

$check = time() - 900;
if($mybb->origin['updatetime'] < $check){
define("RUN_UPDATE", 1);
include 'path/to/update/script'
}

(In the script, "RUN_UPDATE" needs to be defined so that the script can't be accessed manually).

The update will run when someone visits that page, just like the Task Manager.
(2009-02-05, 08:54 AM)Tom.M Wrote: [ -> ]1)...The Task Manager only picks up files that are in the ./inc/tasks/ folder...

Thank you, for the detailed answer.
1) The Task Manager only picks up files that are in the ./inc/tasks/ folder...

a) I know, but...
Can a "wget type" command be run from PHP code which is inside the task function?
or...
Can the code inside the task file somehow start a php script in another directory?

EDIT:
I think I found a way, but still need to figure out exactly how to use it in a task file...
/*
#
COMMAND TO EXECUTE THE PAGE USING WGET
#
YOU CAN SEND GET VARIABLES USING THIS
#
BE ABSOLUTELY SURE TO PROTECT AGAINST INJECTION
#
*/
$command = 'wget -q '.$page_to_execute;
(Oops: Not for shared hosting needs exec enabled)
####

b) Googling for alternatives, I found this which looks really useful:

Quote:phpJobScheduler - scheduling PHP scripts to run at set intervals your replacement for cron jobs.

Designed to automate tasks by scheduling PHP scripts to run at set intervals, a replacement for cron jobs on Unix or scheduled tasks using Microsoft Scheduler - PHP hosting is required (if you host multiple sites try the reseller hosting). phpJobScheduler is a scheduler that runs using PHP and MySQL (no root/admin access is required).

You can now add variables (arguments) when adding or modifying jobs to be fired, eg. "fire_this_script.php?myvar=22&this_count=4"
For further details please read:
dwalker.co.uk/forum/viewtopic.php?f=1&t=727&hilit=variables
Note within your PHP scripts you access the passed values in this manner:
$_POST['myvar'];


######
2) The easy way = "No, you are correct."
I've seen cron jobs which run a script and use an XML file for variables, but in that case the xml file is made in advance, not a dynamic variable.

######
You could try it another way...

$check = time() - 900;
if($mybb->origin['updatetime'] < $check){
define("RUN_UPDATE", 1);
include 'path/to/update/script'
}

I tried to google " define("RUN_UPDATE", 1); " and this thread already made #1 Smile
If you see this reply please explain that line.
``
EDIT #2: Defines a named constant at runtime.
define("GREETING", "Hello World!!", true);


Thanks again.
JobScheduler is a good option after looking into it... I haven't used wget in a long time, so I'm unsure whether that's possible.

You can create a php file in the tasks folder which runs another PHP script. You can use the include, require or require_once or whichever one you prefer to do this.

$check = time() - 900;
if($mybb->origin['updatetime'] < $check){
define("RUN_UPDATE", 1);
include 'path/to/update/script'
}

I have this code in a plugin, which runs a task (or cron job, I over complicate things sometimes). Basically you define a constant as a sort of security thing. For example, if your file was ./inc/foo.php anybody could go to that file if they knew what it was called and the code inside will execute. But, if we use constants, we can protect our files from being accessed directly by putting this at the top of foo.php:

if(!defined("RUN_UPDATE"))
{
die("A really horrible death");
}

If someone tries to access ./inc/foo.php directly (by typing in the address), then they will see "A really horrible death". By using define("RUN_UPDATE", 1), and then including the file, we're allowing the code to execute.

I'm pants at explaining PHP things, so hopefully you'll get what I mean. MyBB uses this (it uses IN_MYBB instead of RUN_UPDATE) to do the same thing.