crontab with php doesn't work [duplicate] - php

This question already has answers here:
Executing php with crontab
(6 answers)
Closed 9 years ago.
My situation is that I have a php file at /root/test.php.
And I set my root's crontab like this
* * * * * /usr/local/bin/php /root/test.php
The content of test.php worked for generate the aliase database for postfix just like the following:
<?php echo shell_exec("postaliases hash:/etc/aliases"); >
I know that the test.php is successfully run by crontab.
But the shell_exec inside doesn't seem to work(I can't find my target aliases.db file under /etc/).
And I have tested use commend php -f test.php directly is work.
I know the situation is a little complex. But if someone can solve my problem, I will be very appreciate your for help.

Use the full path to postaliases. Most likely it is not in the path of the user executing the script
Why do use a php script to call a shell? Just use a shellscript

Related

How can I run php commands directly in console? [duplicate]

This question already has answers here:
How can I execute PHP code from the command line?
(6 answers)
Closed 2 years ago.
When I run PHP in my console (any cli application) and I sending a command like below, it not returned anything!
echo 'Hi!';
Is there any way to run PHP commands directly in console or just we can run a .php file?
Note: My environment variables and path is OK.
You may use PHP's interactive shell
php -a
echo 'Hi!';

Executing Bash script from PHP but nothing happens [duplicate]

This question already has answers here:
How can I debug exec() problems?
(4 answers)
Screen -X isn't working ("No screen found")
(2 answers)
Closed 6 years ago.
I know that this question has been discussed before but i still can't find a solution. I'm trying to execute a Bash script from a PHP file on a (really small) website which is run using the PHP built in webserver. When running the PHP file in the terminal with
php -f test.php
it works fine and the bash script executes. However when i start the webserver and call the PHP function from the website, nothing happens. The Bash script is invoked with:
exec('/home/user/website/bash_script', $out, $return);
inside the PHP-file and i get that return = 1.
On the website the PHP script that executes the bash-script is called by another PHP script through:
<input type="button" value="..." onclick="location='test.php'"/>
I first thought that it was a problem with my permissions, but i added the script-file in visudo to be able to run it as super-user without password but still no luck. I also changed the file permissions to 755.
Here is test.php (which works fine when run through the terminal):
<?php
exec ('/home/user/website/test');
?>
Also i CAN execute exec('cp bla bla2'); from the webserver so theres no problem with exec()
EDIT------------------------------------------------------------------------
The Bash script is sending a command to a different screen and it seems that this is the root of the problem. I think that this can solve my problem:
Screen -X isn't working ("No screen found")

PHP fopen can not read file if called with cronjob [duplicate]

This question already has answers here:
Crontab - Run in directory
(2 answers)
Closed 8 years ago.
I have a php script that sends out a email report with an attached pdf document. I use fopen. If I call the php file manualy it works without any problems and the mails are beeing send as expected.
But:
If the same file should is call by cron job, it does not work at all. The error is:
Warning: fopen(filename.pdf): failed to open stream: No such file or directory in /home/public_html/util.php on line 29
Any ideas how to fix this?
Every cron runs in a minimal environment, which can be a different shell and a different profile than your user account. Also scripts are started not from the containing directory themselves.
In most cases it works, if you just change your directory to the one containing the script like you do when execute the script on the command line shell. Instead of
* * * * * /usr/bin/php /path/to/your/file.php
write this:
* * * * * cd /path/to/your/ && /usr/bin/php ./file.php
If this is not sufficient, you can load your default user shell and your PATH variable with writing
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
at the top of your crontab (with the right settings of course, which might be /bin/bash and the output of echo $PATH).
Cron jobs run without any environment, including a current directory. That means that you'll need to use the full path to any files that you want to work with.
If you add a valid email address at the top of your crontab:
MAILTO=your-email#example.com
you'll get any output the cron job does sent to your email. This includes error messages, which can be useful for debugging.

Unable to run php script using cron [duplicate]

This question already has answers here:
Cannot get PHP cron script to run
(4 answers)
Closed 8 years ago.
I am running a php script using cron. The name of the file is : PriceChecker.php .
This is my cron script
# m h dom mon dow command
0 07,11,16,20 * * * php /var/www/mainsite/PriceChecker.php
* * * * * /var/www/mainsite/pricecheck.sh
The shell script is:
#!/bin/bash
php PriceChecker.php >logger.log
The first line is the original. The second is for testing
I have tried various variations including: /usr/bin/php /var/www/mainsite/PriceChecker.php
If I log the out in the cron php /var/www/mainsite/PriceChecker.php > logfile.log
The logfile is created, but is empty
Same as here, I think Cannot get PHP cron script to run
You forgot to specify the user name you want your script to be run as.
Specify the full path of the PHP file to be execute
Okay what worked was CDing into the directory of the script
https://serverfault.com/questions/97828/php-from-command-line-path-problems/97881#97881

Keep loading my script [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
schedule an email in php
How can I keep loading a PHP file on my server after every hour?
It will mail me something. Actually it mails me something when I visit it. But I want it to mail me after every hour. So someone should either visit it each hour or I have to use cron job like something.
Any help will be appreciated.
You should use a cronjob.
Start by opening you terminal and run
crontab -e
You may need to configure your crontab settings (default editor) if this is the first time you are using crontab. Now, in your editor, you have to call your php script like this: (it is set to be called each hour)
0 * * * * /path/to/php /path/to/your/script.php
You can also use an alias of hourly.
#hourly /path/to/php /path/to/your/script.php

Categories