Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execution of a php function in crontab

Tags:

linux

php

cron

I want to execute a php function in crontab like it is possible in a URL:

yourdomain.com/yourphp.php?function=pwdTimeout

I have tried something like this but it doesn't work

* * * * * /var/..../webpage/yourphp.php?function=pwdTimeout

How can I execute a php function with the help of crontab. Help is highly appreciated.

My php-File looks like:

<?php 
define ('database', $database); // the variables are defined and tested 
define ('host', $host); 
define ('db_username', $db_username); 
define ('db_password', $db_password); 
if(isset($_GET['function'])){ 
$view = new functions(); 
$view->$_GET['function]();
}else{ 
echo 'Function not found'
} 

class functions{ 
function getUsername(){....} 
function setPassword(){...} 
function pwdTimeout(){...} 
} 
?>

where should I make changes in my code? thanks:)

like image 618
Dominik Avatar asked Oct 22 '25 04:10

Dominik


1 Answers

Call your script in the CRON tab with your function name as an argument:

* * * * * /path/to/php /var/www/webpage/yourphp.php pwdTimeout

And retrieve the value of your argument with $argv:

$functionName = $argv[1]; // outputs pwdTimeout ([0] is the filename)

Please note that you can also use a key for your argument ($ php yourphp.php function=pwdTimeout) but you would need to explode() it, like so:

$parameters = explode('=', $argv[1]); // outputs array('function' => 'pwdTimeout')

Finally, you can use getopt() with the following call:

* * * * * /path/to/php /var/www/webpage/yourphp.php -f pwdTimeout

And use it like so:

$parameters = getopt('f:'); // outputs array('f' => 'pwdTimeout')
like image 54
D4V1D Avatar answered Oct 24 '25 18:10

D4V1D



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!