Last active
May 2, 2017 10:23
-
-
Save MMF/9e7afa7372deb481b764 to your computer and use it in GitHub Desktop.
create windows tasks from PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* This class will be responsible for create/delete windows tasks | |
* Sample code: | |
* | |
$task = new WindowsTask('my monthly task', 'USERNAME', 'PASSWORD"); | |
$task->schedule_type = WindowsTask::ScheduleType_MONTHLY; | |
$task->start_time = '12:41'; | |
$task->command = 'notepad'; | |
$task->day = '30'; | |
if ($task->create()) { | |
echo 'registered, check the win scheduler'; | |
} else { | |
echo 'not created'; | |
} | |
* | |
*/ | |
class WindowsTask { | |
// schedule Type | |
//const ScheduleType_DAILY = 'MINUTE'; | |
//const ScheduleType_DAILY = 'HOURLY'; | |
const ScheduleType_DAILY = 'DAILY'; | |
const ScheduleType_WEEKLY = 'WEEKLY'; | |
const ScheduleType_MONTHLY = 'MONTHLY'; | |
// task name | |
private $_name; | |
// schedule type | |
public $schedule_type; | |
// task command | |
public $command; | |
// user | |
public $user; | |
// pass | |
public $pass; | |
// start_time HH:MM 24-hour format | |
public $start_time; | |
// start date | |
public $start_date; | |
// day of the task | |
public $day; | |
// name prefix | |
private $_prefix = "Vinus_"; | |
// constructor | |
public function __construct($name=false, $windows_user, $windows_pass) { | |
// set user & pass | |
$this->user = $windows_user; | |
$this->pass = windows_pass; | |
// set name | |
if ($name) { | |
$this->setName($name); | |
} | |
} | |
/* | |
* set command name | |
*/ | |
public function setName($name) { | |
$this->_name = $this->_prefix . $name; | |
} | |
/* | |
* execute command, check and return | |
*/ | |
private function _execute_command($cmd) { | |
$output = ''; | |
$return = ''; | |
exec($cmd, $output, $return); | |
// check if succeeded | |
if (isset($output[0]) && strpos($output[0], 'SUCCESS') === 0) { | |
return true; | |
} | |
return false; | |
} | |
/* | |
* create windows task | |
* must set name, schedule_type, ..etc before calling this method | |
*/ | |
public function create() { | |
// day option (only with weekly and monthly schedule type) | |
$day_opt = ''; | |
if ($this->schedule_type == self::ScheduleType_WEEKLY || | |
$this->schedule_type == self::ScheduleType_MONTHLY) { | |
$day_opt = '/D @day'; | |
} | |
// the complete command for creating windows task | |
$cmd = 'schtasks /create /TN "@name" /TR "@command" /ST @start_time /sd @start_date /ru @user /SC @sch_type ' . $day_opt; | |
$replace = [ | |
'@name' => $this->_name, | |
'@command' => $this->command, | |
'@user' => $this->user, | |
'@start_time' => $this->start_time, | |
'@sch_type' => $this->schedule_type, | |
'@day' => $this->day, | |
'@start_date' => $this->start_date | |
]; | |
// replace the values | |
$cmd = str_replace(array_keys($replace), array_values($replace), $cmd); | |
//dd($cmd); | |
// execute command | |
return $this->_execute_command($cmd); | |
} | |
/* | |
* delete | |
* must set name before calling this method | |
*/ | |
public function delete() { | |
$cmd = 'schtasks /DELETE /F /TN "@name"'; | |
$cmd = str_replace('@name', $this->_name, $cmd); | |
// execute command | |
return $this->_execute_command($cmd); | |
} | |
/* | |
* return array of schedule types available | |
*/ | |
public static function list_schedule_types() { | |
return [ | |
self::ScheduleType_DAILY => trans('win_tasks.type_daily'), | |
self::ScheduleType_WEEKLY => trans('win_tasks.type_weekly'), | |
self::ScheduleType_MONTHLY => trans('win_tasks.type_monthly') | |
]; | |
} | |
/* | |
* list days used for weekly schedule type | |
*/ | |
public static function list_week_days() { | |
// days and their keys on locale file | |
$days = [ | |
'SAT' => trans('days.sat'), | |
'SUN' => trans('days.sun'), | |
'MON' => trans('days.mon'), | |
'TUE' => trans('days.tues'), | |
'WED' => trans('days.wednes'), | |
'THU' => trans('days.thurs'), | |
'FRI' => trans('days.fri') | |
]; | |
return $days; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment