Jump to content
Aerosol

Create CronJobs using PHP

Recommended Posts

Posted

Permissions for www-data user

www-data ALL=(ALL) NOPASSWD: /usr/bin/crontab, /bin/mv, /bin/sh, /bin/cp,  /bin/rm, /usr/bin/php


function createCronJob($min,$hour,$dayofMonth,$month,$dayOfweek,$command,$commented)
{
$realpath_var = realpath(__FILE__);
$realpath_arr = explode('/',$realpath_var);
$FullPathRoot = "/".$realpath_arr[1]."/".$realpath_arr[2]."/".$realpath_arr[3];

if($commented!="")
{
$commented="#";
}

if($min=="")
{
$min="*";
}
if($hour=="")
{
$hour="*";
}
if($dayofMonth=="")
{
$dayofMonth="*";
}
if($month=="")
{
$month="*";
}
if($dayOfweek=="")
{
$dayOfweek="*";
}

$CronJobs = shell_exec("sudo crontab -l");



$CronJobs_arr= explode("#***\n",$CronJobs) or die("ERROR");
$CronJobs_arr = array_filter($CronJobs_arr);


$NewCron = $CronJobs_arr[0]."#***\n";


if($command!="")
{
$command_file = "#!/bin/bash\n";
$command_file.= $command."\n\n";
$command_file.= 'echo '.'"Scheduled Backup: Backup performed at '.'`date '."'+%d-%m-%Y %H:%M:%S'`".'\n<br/>---------------------------------------------------------------------------------\n" >> '.$FullPathRoot."/logSchedule/".date("Y-m-d").".log" ;

file_put_contents($FullPathRoot."/bash/ExecCron.sh",$command_file);
$NewCron.= $commented.$min." ".$hour." ".$dayofMonth." ".$month." ".$dayOfweek."\t".$FullPathRoot."/bash/ExecCron.sh"."\r\n#***\n";

}




$file = $FullPathRoot."/InstallNewCron.txt";
file_put_contents($file,$NewCron);

$ClearCronJob = shell_exec("sudo crontab -r");
$reinstall_cronJob = shell_exec("sudo crontab ".$file);
unlink($file);

$Body="The Original Cronjob was changed \n\n\nOriginal:\n\n****************\n\n".$CronJobs."\n****************\n\n\nModfied To:\n\n".$NewCron."\n\n";
return $Body

}

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...