-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCron.php
More file actions
71 lines (71 loc) · 1.51 KB
/
Cron.php
File metadata and controls
71 lines (71 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
/**
Schedule and remove CRON entries from the local System
**/
class Cron
{
/**
Set Timezone from the system!
**/
public static function SetTimeZone()
{
$timezone=file_get_contents('/etc/timezone');
$timezone=explode(PHP_EOL, $timezone)[0];
if($timezone) date_default_timezone_set($timezone);
}
/**
Set a cron job to run After 3 minutes from now and daily at that time.
**/
public static function SetNowDaily($sline,$min=NULL)
{
static::SetTimeZone();
if($min===NULL) $min=3;###
$t=time(NULL)+($min*60);# AFTER 3 minutes
$m=date('i',$t);
$m=intval($m);
$h=intval(date('G',$t));
$cline="$m $h * * * $sline";
return static::Set($cline);
}
/**
Set a cron job.
**/
public static function Set($cline)
{
$c=shell_exec('crontab -l');
$c.=$cline;
$c.=PHP_EOL;
file_put_contents('/tmp/crontab.txt', $c);
exec('crontab /tmp/crontab.txt');
}
/**
Remove a cron job.
**/
public static function Remove($search)
{
$c=(shell_exec('crontab -l'));
$ls=explode(PHP_EOL, $c);
$o='';
foreach ($ls as $l) {
if(strpos($l, $search)===FALSE) $o.=$l.PHP_EOL;
}
file_put_contents('/tmp/crontab.txt', $o);
exec('crontab /tmp/crontab.txt');
}
/**
Replace a cron job.
**/
public static function Replace($search,$cline)
{
$c=(shell_exec('crontab -l'));
$ls=explode(PHP_EOL, $c);
$o='';
foreach ($ls as $l) {
if(strpos($l, $search)===FALSE) $o.=PHP_EOL.$l;
}
$o.=$cline;
$o.=PHP_EOL;
file_put_contents('/tmp/crontab.txt', $o);
exec('crontab /tmp/crontab.txt');
}
}