Use php in php5.3 to manage crontab to schedule tasks _ PHP Tutorial

Source: Internet
Author: User
Use php in php5.3 to manage crontab tasks. For php5.3 or later, you can use php to manage crontab tasks. Next, let me try it out. if you need to learn more, please refer to it. 1. if you use php-crontab-manager to manage any php5.3 or later versions, you can use php to manage crontab scheduled tasks. Next, let's try it out. if you need to learn more, please refer to it.

1. use php-crontab-manager to manage scheduled tasks
PHP> = 5.3

Example

The code is as follows:

Use phpmanagercrontabCrontabManager;

$ Crontab = new CrontabManager ();
$ Crontab-> enableOrUpdate ('/tmp/my/crontab.txt ');
$ Crontab-> save ();

Add a simple scheduled task:

The code is as follows:

Use phpmanagercrontabCrontabManager;

$ Crontab = new Ssh2_crontab_manager ();
$ Job = $ crontab-> newJob ();
$ Job-> on ('*****');
$ Job-> onMinute ('20-30')-> doJob ("echo foo ");
$ Crontab-> add ($ job );
$ Job-> onMinute ('35-40')-> doJob ("echo bar ");
$ Crontab-> add ($ job );
$ Crontab-> save ();


Class file

The code is as follows:

Class Ssh2_crontab_manager {

Private $ connection;
Private $ path;
Private $ handle;
Private $ cron_file;

Function _ construct ($ host = NULL, $ port = NULL, $ username = NULL, $ password = NULL)
{
$ Path_length = strrpos (_ FILE __,"/");
$ This-> path = substr (_ FILE __, 0, $ path_length ).'/';
$ This-> handle = 'crontab.txt ';
$ This-> cron_file = "{$ this-> path} {$ this-> handle }";

Try
{
If (is_null ($ host) | is_null ($ port) | is_null ($ username) | is_null ($ password) throw new Exception ("The host, port, username and password arguments must be specified! ");

$ This-> connection = @ ssh2_connect ($ host, $ port );
If (! $ This-> connection) throw new Exception ("The SSH2 connection cocould not be established .");

$ Authentication = @ ssh2_auth_password ($ this-> connection, $ username, $ password );
If (! $ Authentication) throw new Exception ("cocould not authenticate '{$ username}' using pasword: '{$ password }'.");
}
Catch (Exception $ e)
{
$ This-> error_message ($ e-> getMessage ());
}
}

Public function exec ()
{
$ Argument_count = func_num_args ();

Try
{
If (! $ Argument_count) throw new Exception ("There is nothing to exececute, no arguments specified .");

$ Arguments = func_get_args ();

$ Command_string = ($ argument_count> 1 )? Implode ("&", $ arguments): $ arguments [0];

$ Stream = @ ssh2_exec ($ this-> connection, $ command_string );
If (! $ Stream) throw new Exception ("Unable to execute the specified commands:
{$ Command_string }");
}
Catch (Exception $ e)
{
$ This-> error_message ($ e-> getMessage ());
}

Return $ this;
}

Public function write_to_file ($ path = NULL, $ handle = NULL)
{
If (! $ This-> crontab_file_exists ())
{
$ This-> handle = (is_null ($ handle ))? $ This-> handle: $ handle;
$ This-> path = (is_null ($ path ))? $ This-> path: $ path;
$ This-> cron_file = "{$ this-> path} {$ this-> handle }";

$ Init_cron = "crontab-l >{$ this-> cron_file} & [-f {$ this-> cron_file}] |>{$ this-> cron_file }";

$ This-> exec ($ init_cron );
}

Return $ this;
}

Public function remove_file ()
{
If ($ this-> crontab_file_exists () $ this-> exec ("rm {$ this-> cron_file }");
Return $ this;
}

Public function append_cronjob ($ cron_jobs = NULL)
{
If (is_null ($ cron_jobs) $ this-> error_message ("Nothing to append! Please specify a cron job or an array of cron jobs .");

$ Append_cronfile = "echo '";

$ Append_cronfile. = (is_array ($ cron_jobs ))? Implode ("n", $ cron_jobs): $ cron_jobs;

$ Append_cronfile. = "'>>{$ this-> cron_file }";

$ Install_cron = "crontab {$ this-> cron_file }";

$ This-> write_to_file ()-> exec ($ append_cronfile, $ install_cron)-> remove_file ();

Return $ this;
}

Public function remove_cronjob ($ cron_jobs = NULL)
{
If (is_null ($ cron_jobs) $ this-> error_message ("Nothing to remove! Please specify a cron job or an array of cron jobs .");

$ This-> write_to_file ();

$ Cron_array = file ($ this-> cron_file, FILE_IGNORE_NEW_LINES );

If (empty ($ cron_array ))
{
$ This-> remove_file ()-> error_message ("Nothing to remove! The cronTab is already empty .");
}

$ Original_count = count ($ cron_array );

If (is_array ($ cron_jobs ))
{
Foreach ($ cron_jobs as $ cron_regex) $ cron_array = preg_grep ($ cron_regex, $ cron_array, PREG_GREP_INVERT );
}
Else
{
$ Cron_array = preg_grep ($ cron_jobs, $ cron_array, PREG_GREP_INVERT );
}

Return ($ original_count === count ($ cron_array ))? $ This-> remove_file (): $ this-> remove_crontab ()-> append_cronjob ($ cron_array );
}

Public function remove_crontab ()
{
$ This-> remove_file ()-> exec ("crontab-r ");
Return $ this;
}

Private function crontab_file_exists ()
{
Return file_exists ($ this-> cron_file );
}

Private function error_message ($ error)
{
Die ("

ERROR: {$error}
");
}

}

Project address
Https://github.com/MediovskiTechnology/php-crontab-manager

2. Ssh2_crontab_manager detailed tutorial on php management plan tasks

For details, refer:
Http://net.tutsplus.com/tutorials/php/managing-cron-jobs-with-php-2/

References:

Http://stackoverflow.com/questions/4421020/use-php-to-create-edit-and-delete-crontab-jobs

Bytes. 1. use php-crontab-manager to manage the scheduler...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.