I want to edit cron tab daily to delete / add some jobs .. so I added cron job ( helper job ) to run php script to handle these edits.
When I run this script by browser works fine .. but when it runs using the helper job not running and I receive notification mail from cpanel with this error :
Please note I am on shared hosting plan with C-Panel so I have no root access .. but the code working fine when run from browser.
Error:
You (dcfn35c8st1b) are not allowed to use this program (crontab)
See crontab(1) for more information
You (dcfn35c8st1b) are not allowed to use this program (crontab)
See crontab(1) for more information
You (dcfn35c8st1b) are not allowed to use this program (crontab)
See crontab(1) for more information
You (dcfn35c8st1b) are not allowed to use this program (crontab)
See crontab(1) for more information
You (dcfn35c8st1b) are not allowed to use this program (crontab)
See crontab(1) for more information
PHP Script:
exec('crontab -l', $crontab);
$record_found = false;
foreach($crontab as $key => $value){
if(strpos($value, 'record_extra.php') !== false){
//echo $key . ' ' . $value . '<br>';
unset($crontab[$key]);
$record_found = true;
}
if(strpos($value, 'record.php') !== false){
//echo $key . ' ' . $value . '<br>';
unset($crontab[$key]);
}
}
if($record_found){
file_put_contents('/tmp/crontab.txt', arrayToString($crontab) . $firstJob.PHP_EOL);
if(exec('crontab /tmp/crontab.txt')){
//echo 'success <br>';
} else {
//echo 'error <br>';
}
$output = shell_exec('crontab -l');
file_put_contents('/tmp/crontab.txt', $output . $secondJob.PHP_EOL);
if(exec('crontab /tmp/crontab.txt')){
//echo 'success <br>';
} else {
//echo 'error <br>';
}
} else {
$output = shell_exec('crontab -l');
file_put_contents('/tmp/crontab.txt', $output . $firstJob.PHP_EOL);
if(exec('crontab /tmp/crontab.txt')){
//echo 'success <br>';
} else {
//echo 'error <br>';
}
$output = shell_exec('crontab -l');
file_put_contents('/tmp/crontab.txt', $output . $secondJob.PHP_EOL);
if(exec('crontab /tmp/crontab.txt')){
//echo 'success <br>';
} else {
//echo 'error <br>';
}
}
Need your help.
Thanks in advance.
2
Answers
Seems like the user isn’t allowed to run crontab. Look into the files "/etc/cron.allow" and "/etc/cron.deny". If cron.allow exists, then only users in that list can use crontab. Users in cron.deny, can’t use. If neither exist then only root user can use crontab.
I’ve looked at numerous "can’t run crontab" posts and none of them addressed my problem. I want to report my unique solution.
My service provider copied my system to another place, and apparently they literally used the "cp" command to do it, which meant that no setuid bits were preserved. /usr/bin/crontab was one of several files for which I had to restore the setuid bit.
You can’t run crontab if it doesn’t have the setuid bit set.