getting php file to work with crontab - php

I have a php file that I can run from the browser and it works perfect. I tried to set up a cron job to run the php file, but am obviously missing something.
Since this php file was originally ran from the browser, it is uploaded to the /var/www folder. I made a copy of it in a new folder called /var/cron. I made this folder just to test. I will probably put it in another folder, but for now it is in this folder.
Here is what I did. After copying the php file to the /var/cron folder, I ran the crontab -e command to edit the crontab file. My cron job looks like this:
00,30,59 * * * * /var/cron/download.php
I have tried changing permissions by using chmod 755 download.php
that didn't do anything.
I have tried /usr/bin/wget -q /var/cron/download.php
this didn't do anything either.
What should I do?

If you add
#!/usr/bin/php
as the first line of the php file, you can run it from the command line (as long as it has appropriate permissions. You can test run by going to the directory and typing
./download.php

I'm a little surprised amccausl's approach didn't work for you.
Have you tried changing your crontab so it looks like this:
00,30,59 * * * * /usr/bin/php /var/cron/download.php
This assumes /usr/bin/php is where php lives on your server.
(You may also need to install a "cli" package for PHP, eg. Ubuntu/Debian's php5-cli.)

Try with full URL like :
wget -O - -q "http://www.domain.com/cron.php"

Related

rename() not working in cron job

rename() runs fine from the command line, but when run from cron job, the rename() does not. Since the connect.php file works I assume the cron job is in the right directory, but can't figure out why rename() doesn't work. I tried absolute paths and they didn't work:
<?php
include 'connect.php';
$oldlocation='xxx/xxx/'.$oldfilename;
$newlocation='yyyy/xxx/'.$newfilename;
$move=rename("$oldlocation","$newlocation");
The cron job: * * * * * /usr/bin/php /usr/xxx/xxx/xxx/xxx.php -q -f
I have no root access to the server. Should this be run through a SHELL script?
The current path while a cron execution is the home directory of the user which is running the cron process. See also this post.
Just change the relative path to an absolute and the issue is fixed.
The solution, not directly a directory issue (well sort of):
$oldlocation='xxx/xxx/'.$oldfilename;
has to be changed to:
$oldlocation='/xxx/xxx/'.$oldfilename;
I guess I was missing the first /

Linux Crontab executes PHP script but this Script won't delete file

I'm running a PHP script via Linux Crontab. It runs correctly (verified using ps -ef). This script checks all the files in a specified directory and if the files don't meet certain requirements they will be deleted.
This sript works perfectly executed through Linux console (as root) but when It's executed by Crontab it won't work...
Suggests? Thanks!
PD:
- Permissions ->
- PHP Script (755)
- Target folder (777)
- Files to be removed (644)
Crontab Line:
*/1 * * * * php /var/www/server/close_con_watch.php >> /var/www/server/phpcronlog.txt
make sure you add the user/group to the Cron command, like
10 * * * * root /path/file.php
And make sure your file starts with
#!/usr/bin/php
It finally worked. The conflict was in the PHP Script.
My script checks files in a certain directory, the path to that directory was declared in a relative way. I declared the path in an aboslute way and It worked but I still don't get it... The PHP Script is in a fixed path, so all the paths declared in the lines of code should work as relative regardless where It's executed from... Am I wrong? Thanks everyone.
Why I'm confused:
The relative path declared before didn't throw a path warning/exception.
It worked perfectly when I executed the Script from console.

Permission denied with bash.sh to run cron

How to run a cron with bash script here.What i did as follows and here with errors.I want to know how to do that in ubuntu.I was struck with it now
bash.sh file
#!/bin/bash
cd /var/www/Controller
/usr/bin/php post.php
In crontab -e
* * * * * /home/samitha/bash.sh >> /home/samitha/log/cron.log 2>&1
But now i getting following error
/bin/sh: 1: /home/samitha/bash.sh: Permission denied
How will i fix it ? what i did wrong ?
you can try the following solution as well:
chmod +x post.php
chmod +x bash.sh
echo "* * * * * /home/samitha/bash.sh >> /home/samitha/log/cron.log 2>&1" >> cronjob
chmod +x cronjob
/etc/init.d/crond start #redhat based servers like centos
/etc/init.d/cron start #debian based servers like ubuntu
crontab cronjob
The problem could be that your user does not have the rights to execute the file.
First you set the execution flag for your script
chmod +x /home/samitha/bash.sh
Then you should check the permissions for the php file with
ls -lah /var/www/Controller
If neither your usergroup nor your username shows up, you have to run the script with superuser rights or change its permissions.
First way would be put your entry in
sudo crontab -e
or the second one would be (which I would not recommend, as everyone would be allowed to execute the script by calling your site)
chmod a+x /var/www/Controller/post.php
The user executing that cron (the one who executes cron -e) doesn't have proper rights for executing that script. That is: either the script lacks the execution flag, or it's not possible to reach it because some of its ancestor directories lack the execution flag.
TL;DR: Insert "bash" before the script in crontab and in any scripts called by the script.
I have a fix for this. None of the previous answers worked for me. I have two Asus laptops running Kubuntu (updated to kernel v5.8) with virtually identical configurations. I don't know why one has the problem and the other doesn't. However, after 2 days of experimentation I found a fix. Hopefully, someone more knowledgeable than I can find the cause.
Cron is using sh instead of bash. I tried adding SHELL=/bin/bash and defining PATH above the commands in crontab and it had no effect. All of my scripts have the #!/bin/bash shebang at the beginning, also with no effect. My scripts' (and their directories') permissions are 777. The scripts don't run for cron or users, no matter what combination of user:group I tried on the files. Using full pathnames is cron and inside the scripts had no effect that was different from using environment variables.
My fix was to insert "bash" before the script filename in crontab. E.g.:
00 01 * * * bash $BASH_SCRIPTS/backup_os.sh
(Yes, cron has no problem with using my environment variables defined in /etc/environment.) Also, in cron, when a script runs another script, the second script will get "permission denied" unless that script is modified to have "bash" before the 2nd script's filename, or use "source" if that'll work with your script.
File must be executable (#see chmod)
All parent directories must have the execution flag (#see chmod)
If crontab is running by different user i.e. not owner, maybe this user don't have rights to execute. (#see chown)
I had exactly the same error as the OP, but on RHEL. Of course it was not a cron issue, but permissions. I should have checked this by trying to run the script manually, but I did not, because it worked well on other servers and file permissions were OK. Eventually, after a couple of hours scratching my head, I figured that the cause was that on this particular server the FS with home directory was mounted with noexec option (use for example mount | grep home to check mount options for /home). Another detail to keep in mind.
EDIT:
I read some more and it turns out that using noexec on the FS with home directories is now advisable for "security" reasons (although the point of doing this is debatable). I also realized that probably yendao42 had the same problem, because indeed adding bash in front of bash scripts is a very simple workaround for this exact issue.

running php file with crontab

I have the following problem:
I'm using the latest crontab version with win 9x/nt
I put the crontab file, the exe file and the log file into the root of my website (aruba linux)
I put the following code inside the crontab file
0 24 * * * php example.php
In the example.php I make an insert
I've tried to do the operation going to the page and it's working fine.
However the crontab doesn't start, even modifying the minutes and hours like * *. In the log file there's written nothing.
I've read that the "php" voice represents the folder in which php is located.
How can know where is it on my website?
Do have to ask to the service of the server or what?
00 * * * * /usr/local/bin/php /home/username/myscript.php
The first path is the location of your php binary
The second path is the location of your php script.
Just specify the absolute path for example.php.
Otherwise crontab will never find it to execute.
To find it, you can use echo __FILE__; so, you'll get the full path for script.
Try the absolute path of the php file, and the absolute path of the php command :
0 24 * * * /usr/bin/php /full/path/to/example.php
if /usr/bin/php is not working, try which php and replace /usr/bin/php with the result of this command .
the default folder for crontab is set in the /etc/crontab file in :
HOME=/path/
and you can change it to your default web root, this way you can use just example.php, but if only you have root access .

Running Cronjob from a specific directory

I created a php script for generating RSS feeds which is to eventually be run via a Cronjob.
All the php files and the resulting RSS xml will be within a sub folder in a website. The php script runs fine on my local dev if I use terminal or the browser while within the same directory on my local development machine.
e.g. php /Library/WebServer/Documents/clientname/siteroot/rss/dorss.php
works fine as does navigating to the dorss.php file in Chrome.
The CronJob has executed though with errors related to the fact that it cannot find the files specified with require_once() which are located in the same folder as rss or in a subfolder of that.
Long story short I need to have the Cronjob run from within the same directory as the dorss.php file so it can reference the include files correctly.
My knowledge on setting up a cronjob is VERY limited so I wanted to ask if this is at all possible (Change directory before running command) to do this on the same command line of the crontab or if not how can it be achieved please?
The current cronjob command is
0 0,6,12,18 * * * /usr/bin/php /var/www/vhosts/clientname/stagingsite/rss/dorss.php
TIA
John
You can change directory to the one you want and then run the php from that directory by doing this:
cd /var/www/vhosts/clientname/stagingsite/rss/
&&
/usr/bin/php dorss.php
It's easier than creating bash scripts, here is the result:
0 0,6,12,18 * * * cd /var/www/vhosts/clientname/stagingsite/rss/ && /usr/bin/php dorss.php
I don't know if there's a better solution, but I would suggest to write a bash script that changes to the correct directory before executing the PHP file, and use cron to execute this script. E.g:
#!/bin/bash
cd /Library/WebServer/Documents/clientname/siteroot/rss/
php dorss.php
Or even just:
#!/bin/bash
php /Library/WebServer/Documents/clientname/siteroot/rss/dorss.php
Save it somewhere and use cron to run that.

Categories