Executing PHP scripts using crontab - php

I am trying to execute a PHP script using crontab but it doesn't seem to work. I am running AWS EC2 Linux and here is the PHP script:
FOREACH (GLOB("*.jpg") AS $filename) {
ECHO "$filename size " . FILESIZE($filename) . "\n";
UNLINK($filename);
}
FOREACH (GLOB("*.jpeg") AS $filename) {
ECHO "$filename size " . FILESIZE($filename) . "\n";
UNLINK($filename);
}
FOREACH (GLOB("*.gif") AS $filename) {
ECHO "$filename size " . FILESIZE($filename) . "\n";
UNLINK($filename);
}
FOREACH (GLOB("*.png") AS $filename) {
ECHO "$filename size " . FILESIZE($filename) . "\n";
UNLINK($filename);
}
When I execute this script manually from a browser, it works normally. But it doesn't work using crontab
Here is my cron command:
00 * * * * php /var/www/html/*****/*****/delete.php
And here is the log:
Nov 28 02:12:01 ip-##-##-##-## CROND[#####]: (root) CMD (/usr/bin/php
/var/www/html/*****/*****/delete.php)
What am I possibly doing wrong?

I just had a similar issue...
Cron is setup correct (I think) but is not running
It is now working for me:
edit /etc/crontab directly and be sure to check your paths.
(You can even cd into the correct directory like this, perhaps that will solve your issue too..)
00 * * * * cd /var/www/html/*****/*****/ && php ./delete.php
Also check if just php will do the trick.
call:
which php
to see the full path of PHP and use that instead.

If it is working fine in browser then you can set the cron job as below,
00 * * * * wget -q -O /dev/null http://example.com/delete.php
this will work same like you calling in browser just change the "http://example.com/delete.php" with the url which you call in browser.

Related

User not allowed to use crontab

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.
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.

shell_exec does not execute from browser requests

After the user submits a data via POST, I show a temporary page and do the process on the background with shell_exec, atleast that's what I'm trying to do.
Here's my page setup:
C:\laragon\www\index.php
<?php
try {
shell_exec("php " . __DIR__ . "/test.php");
} catch(Exception $e) {
echo "Exception: " . $e;
}
?>
C:\laragon\www\test.php
<?php
$myfile = fopen(__DIR__ . "/testfile.txt", "w");
echo "test";
?>
If I go to localhost or localhost/index.php, the second script doesn't run. However, when I try to call both scripts from cmd, it works with both of them.
php C:\laragon\www\index.php
php C:\laragon\www\test.php
They both work and create a file called testfile.txt
Your webserver runs as a specific user and needs the path to php.exe as there is no path environment variable for the webserver user:
shell_exec("c:\path\to\php " . __DIR__ . "/test.php");

7zip command executes from command line but not within php script

I am attempting to write a script which automatically unzips 7zip archives. I'm able to get the command to run from the command prompt, but it doesn't work when running from within the php script.
Here's what I have in terms of code:
$filefolder = "F:/dev/";
$filename = "archive.7z";
$filepath = $filefolder . $filename;
$unzip = "cmd /c 7z x " . $filePath . " -o" . $fileFolder . " -spf";
print_r($unzip . "<br>"); //checking to make sure the command is formed correctly
exec($unzip, $outcome, $rtnStatus);
print_r($outcome);
print_r($rtnStatus);
The following is returned for $outcome and $rtnStatus:
Array ( )
1
What am I missing here?

PHP Deleting a file 24 hours after being uploaded

I've made a simple script that allows users to upload html files to a web directory on my server. However, I'd like it so each file is deleted after 24 hours of being on my server. 24 hours for each file, not 24 hours for the entire directory. Here is my code so far... Thank you for your help. :)
<?php
$target = "users/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)
&& ($_FILES["uploaded"]["type"] == "html"))
{
echo "File: " . $_FILES["uploaded"]["name"] . "<br />";
echo "Type: " . $_FILES["uploaded"]["type"] . "<br />";
echo "Size: " . ($_FILES["uploaded"]["size"] / 1024) . " Kb<br />";
echo "Location: /users/" . $_FILES["uploaded"]["name"];
}
else {
echo "Sorry, " . $_FILES["uploaded"]["name"] . " is not a valid HTML document. Please try again.";
unlink . $_FILES["uploaded"]["name"];
}
?>
Use Cron to execute this script every 10 minutes (better 30 or 60)
$Time=time();
foreach(glob('/users/*') as $file){
if(filemtime($file)+60*60*24<$Time){
unlink($file);
}
}
Not fullproof, but it gives an idea..
foreach(glob("/users/YOUR_USER/*") as $file) {
$file = "/users/YOUR_USER/".$file;
if ((time() - filectime($file)) >= 86400) {
// delete me
}
}
If you're using a Windows server then run a batch file using Windows Scheduler:
Batch file to delete files older than N days
Otherwise, just set up a CRON job.
Include following script at the end of your script. Don't forget to replace /var/www/uploads/ with correct path. Other wise all files will be removed in different place.
$files=shell_exec('find /var/www/uploads/ -mmin +1440');
$file = explode("\n",$files);
if(isset($file) && is_array($file))
{
foreach($file as $val)
{
#unlink($val);
}
}
The above code will work on Linux/UNIX based hosting.
"shell_exec" will execute linux command and return the out put
"find -mmin -1440" is looking for files older than 1 day or 1440
minutes
First line "/var/www/uploads/" path need to be replaced with full
path of the directory.
Unlink have "#" sign to avoid warning if the file not exists there.
As this is powerful linux based command verify the paths correctly.

fuse python script not running in background using php

i am trying to run a file sytem for dropbox ff4d ( from github) in background using php
the purpose if that user will get his dropbox files mount on the server and then i will give the path to a web based explorer (like eXtplorer) so user can manage his file
the script is working fine when using command shell
but when i using the exec function it working printing out the last line of the command shell
and that it . i can not get the folder mount
here the code in php :
$folder = $_POST['foldername'];
$oldumask = umask(0000);
$path = "/var/www/cloudsite/" . $folder;
if(mkdir($path, 0777, true)) {
echo $path . " success directory created ";
} else {
echo $path . "error directory not created ";
}
umask($oldumask);
#$command = '/usr/bin/python /var/www/cloudsite/ff4d/./ff4d.py '. $path .'c7ZYof8Bl0YAAAAAAAAAARThZwAUexbukmv3rMEprPJWFcoQSKGWtWHQBYM40OgC';
$result = exec($command);
if ($result){
echo " </br> execution success";
}else{
echo "</br> error not success";
}
echo $result;
and here what i get in the browser it seems like it working but just hang here nothing mount in the created directory :
var/www/cloudsite/chao success directory created
execution successStarting FUSE...
Within the latest release of my ff4d.py script I've added a "background" switch (-bg).
https://github.com/realriot/ff4d
BTW: Please remove your access key (and revoke it afterwards) because it holds your personal information and ALL your data...

Categories