Guyver
Guyver

Reputation: 165

php cron function?

I was told there is a php cron function, but I've been looking around and I cant seem to find the name of it. Does it really exist? If so what is the name of it?

Upvotes: 1

Views: 2244

Answers (5)

aftamat4ik
aftamat4ik

Reputation: 718

I have my own functions to manage cront tasks with php. Add crontab function looks like this:

if(!function_exists('add_crontask')){
    function add_crontask($time = "0 */3 * * *", $command = "www.mysite.com/cron.php"){
        $cstring = $time." wget -qO --timeout=30 /dev/null ".$command;
        // exec in shell our task
        $otxt = print_r(shell_exec("crontab -l > mycron

        #echo new cron into cron file

        echo \"".$cstring."\" >> mycron

        #install new cron file

        crontab mycron

        rm mycron"),true);


        $l = print_r(shell_exec("crontab -l"),true);
        return $l; // it sould return all available crontasks
    }
}

to remove crontab use this:

if(!function_exists('remove_crontask')){
    function remove_crontask($command = "www.mysite.com/cron.php"){
        $cstring = "wget -qO --timeout=30 /dev/null ".$command;

        $otxt = print_r(shell_exec("crontab -l | grep -v '".$cstring."' | crontab -"), true); // removing by name


        $l = print_r(shell_exec("crontab -l"),true);
        return $l; // it sould return all available crontasks
    }
}

I used here shell_exec function to add php command in crontab file. This function is unsave! Be carefull. Backup your cronjobs before test this code! If this code not working that means:

  1. crontab is not installed on your server
  2. shell_exec functions disabled on your server by default. To enable it see php.ini file with line disable_functions.

If you fix this, my code should work. But in php it's bad to use exec, eval and shell_exec functions. I don't know why in php don't exist extension for sheduling tasks!

Upvotes: 0

Edoardo Pirovano
Edoardo Pirovano

Reputation: 8334

As far as I know there is no cron function in PHP. Perhaps what your host meant was that somewhere in your hosting control panel there is a way to schedule a PHP script to run at a specific time. You should probably ask your host to explain what they meant with it.

Upvotes: 0

Christophe
Christophe

Reputation: 4828

You would still have to create the job in your panel.

Wordpress has a cron system (because you can schedule posts) that does not require you to setup anything in your panel but I think it only works when someone actually visit your website. (could be wrong thought...), I'm not saying it's what you are looking for but it's having a look at

http://codex.wordpress.org/Function_Reference/wp_cron

Upvotes: 1

DampeS8N
DampeS8N

Reputation: 3621

There is no cron function in php, but you can run php scripts as cron jobs.

http://www.htmlcenter.com/blog/running-php-scripts-with-cron/

Upvotes: 0

simshaun
simshaun

Reputation: 21466

None that I know. You can set a PHP script to run in a cronjob. Perhaps that's what he/she meant?

Upvotes: 2

Related Questions