Install cron job from php
I'm trying to install an assignment cron
from php
, but every time I try to remove the code it doesn't work. tell me what is the problem with this code.
$cron = $this->generator();
file_put_contents("cron.txt", $cron);
shell_exec('crontab cron.txt');
the generator function makes a cron job line like below
12 1 * * * /usr/bin/sample >/dev/null 2>&1
The cron.txt file is fine. it contains a string, but shell_exec of some kind doesn't work
source to share
Running your script with $ corn hardcoded locally works fine for me, I've included a couple of lines to clear the crontab at the beginning and a couple of lines to confirm the entry. see below.
$cron = '12 1 * * * /usr/bin/sample >/dev/null 2>&1';
file_put_contents('cron.txt', $cron);
shell_exec('crontab -r');
shell_exec('crontab cron.txt');
// To check the result
$output = [];
exec('crontab -l',$output);
print_r($output);
Output:
Array
(
[0] => 12 1 * * * /usr/bin/sample >/dev/null 2>&1
)
And here's a way to check the execution result crontab cron.txt
:
$output = $return = [];
exec('crontab cron.txt',$output, $return);
var_dump($output, $return);
If successful, there will be no exit or return:
array(0) {
}
int(0)
source to share
The problem is that the Apache server will run as a separate limited resource account, which should rightfully not create cron jobs. This is a pretty easy way to hack the host server. Especially if it's done in a public IP address space.
However, if the environment is secure and authorized by your server administrator, you can start the apache process as a higher priority account (for example, for example) or use the suexec function to run as a designated account with limited but appropriate permissions.
source to share