Skip to content

Instantly share code, notes, and snippets.

@Anan5a
Last active November 7, 2017 03:16
Show Gist options
  • Save Anan5a/35c2977aca028fd9c0ebb6081125008f to your computer and use it in GitHub Desktop.
Save Anan5a/35c2977aca028fd9c0ebb6081125008f to your computer and use it in GitHub Desktop.
Example of how to manage cron using PHP Rev #2
<?php
/*
Copyright 2017 Ananta
This is an example script of how to manage crontab using PHP
Yeah it's a messy script but it works :p
* Create the crontab.tmp.txt file first to use it. This script is unable to populate the file when running using a webserver like apache or nginx
*/
$user = exec('whoami');
$lastCrontab =shell_exec('crontab -u '.$user.' -l');
if(isset($_POST['submit'])){
$cronjobs=$_POST['cronjobs'];
//create tmp file
file_put_contents('crontab.tmp.txt',$cronjobs."\n");
//execute command and try to set new cronjobs
echo shell_exec('crontab -u '.$user.' crontab.tmp.txt');
echo "<a href=''>Go back</a>";
//override tmp file
file_put_contents('crontab.tmp.txt',null);
}else{
if(preg_match('/no crontab/i',$lastCrontab)){
echo "No cronjob found for $user";
}else{
echo "<h2>Current cronjobs of $user</h2>";
echo '<pre>';
echo $lastCrontab;
echo '</pre>';
}
echo"<h1>Create new cronjobs!</h1>";
echo "
<form action='' method='POST'>
<textarea cols=120 rows=20 name='cronjobs' placeholder='No cronjob were found! Create some'>$lastCrontab</textarea>
<br>
<button type='submit' name='submit'>Setup!</button>
</form>
";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment