Run php script at scedule using Linux server shell command - php

My Linux server runs PHP and Apache. I want to run a .php file every day at a certain time. (Which will be run automatically in the server)
I have two files: mail.php which sends mail to myEmail#gmail.com. And bash.php which contains some codes which calls mail.php.
bash.php contains :
<?php
#!/usr/bin/php
$command="52 14 * * * ./mail.php";
$result=shell_exec($command);
echo "<pre>$result</pre>"
?>
Then I run bash.php in the browser.
I get no error message. But don't receive any email. Where is wrong?

You are trying to write a cron job in php.
Throw your bash.php away and convert it to a cron task (crontab -e).
php_mailer.cron:
52 14 * * * /usr/bin/php /path/to/mail.php

You shouldn't do it this way, All you have to do is to open the Cronjob config file using the command crontab -e, and then add the command line in it:
52 14 * * * <path to>php <Full absolute Path>/mail.php
To know your php path use the command:
Which php
For more details you may refer to the link: Crontab Command

Related

Cron job keeps failing in cPanel

My cron job keeps failing in cPanel:
/bin/sh: /opt/cpanel/ea-php73/root/etc: is a directory
I have changed the code, previously it was /usr/local/bin/php
This is the full code:
0 0,12 * * * /opt/cpanel/ea-php73/root/etc /home4/***/****.org/api/fetch.php
Does anyone know the reason? The file fetch.php works fine otherwise. How do I know where the PHP command is?
If you want to run a PHP file in a cron job, you should run with the PHP interpreter. But your error shows that the cron job is running by the Bash interpreter.
This means you have issue with this part /opt/cpanel/ea-php73
You should either revert your interpreter back to /usr/local/bin/php
Or you can use the curl command to run the webpage from an external source.
Eg: 0 0,12 * * * curl ****.org/api/fetch.php
In the PHP script, you're directly executing an incorrect shell command. Probably a missing / prefix in a path.

how to run my PHP script every 20 minutes

I have one PHP script that I want to run every 20 minutes. I search in google and I see I must use CRON for that, I have to use FileZilla and putty to access my server. I found this:
https://crontab.guru/every-20-minutes
SO the code should looks like this:
*/20 * * * * /usr/bin/php /path.php
I need to write in the putty?
Use Putty to connect to your server via SSH. It will give you a command line interface (CLI).
Once in the CLI, you need to type the following command:
crontab -e
This will open the CRON config file. Then you need to add a line at the bottom of the file:
*/20 * * * * /usr/bin/php /path.php
Finally, you save and quit using CTRL+X
And that's it. The PHP script will be executed every 20 minutes. Of course, I'm assuming you're SSH access have the right permissions.
Now I don't know if your script path.php is already on your server, but this is where you need FileZilla, to upload the file on the server, or to update it every time you change it's content.

Run a php file in a specified timeframe

I have a php script, let's say fetch.php which will pull data from other database and insert those data to another database, How do I run the script automatically maybe via command line, let's say I want it to run Everyday at 3pm? Something like a cron.
Here is the cron rule you should add:
00 15 * * * php fetch.php
Read more here: Running a script every day using a cron job
If you are on Windows, you can make use of Windows Task Scheduler.
Here is the link: http://windows.microsoft.com/en-US/windows/schedule-task#1TC=windows-7
Using php from command line is not that hard. Start the fetch.php file like this:
#!/usr/bin/env php
<?php
// You have the command line arguments available as
$scriptName = $argv[0];
$firstArg = $argv[1];
// Add PHP code here :)
// If an error occurs, exit the script with an error code:
if($someError) exit(1);
After saving, set execution rights:
$ chmod 755 fetch.php
Then add the script to the crontab. There are many ways, control panels like Plesk and cPanel have a web interface for example, but here's the command line version:
$ crontab -e
Add to the file:
0 15 * * * /path/to/fetch.php
Save, and you're done.

Run a php script with cron in mac

I am trying to run in mac a php script using cron. I want this php script to run every one minute. I read several sources online and from my understanding is better if I use launchd. In any case, I try to make it work with cron and then if it works fine I might try to use launchd.
So here is what I do:
I wrote this command in a txt file:
* * * * * /usr/bin/php /Applications/MAMP/htdocs/php_test/main_script.php
There I have the path to php (I found it with the "which php" command) and the path to my php script.
I named the txt file: crontasks.txt and I run it through terminal with this command:
crontab /Users/dkar/Desktop/crontasks.txt
When I list the crons (crontab -l) I see that this job is listed. But nothing comes as output every one minute. The output is supposed to be an image stored in a specified folder. What am I missing here?
Thanks in advance
D.
You might have a slight miscomprehension how crond processes the "crontab" files. It would suffice to run the command crontab -e, which would let you edit your personal crontab or you edit the system crontab.
If you use crontab -e it will open the default editor (vi/vim) and you can enter the line:
* * * * * /usr/bin/php /Applications/MAMP/htdocs/php_test/main_script.php
Then you simply save your file. If you want to use the system crontab you will have to edit it directly and enter the line. Additionally the system crontab has one more field for the username. Like this:
* * * * * /usr/bin/php root /Applications/MAMP/htdocs/php_test/main_script.php
On the next full minute your script will be executed by crond.

PHP Cronjob does not execute

I am running Ubunutu Linux server, PHP5, Apache2 and am having trouble getting any sort of cronjob to run through the crontab.
I edit the crontab using
crontab -e
I save the file I want to run:
*/5 * * * * php /home/user/public_html/crx/cronx.php
it saves fine. I can run the file from the console and goes through fine. I can't even find any existing logs for the file. I checked cron was running, stopped and started... no change.
The current php file is just a simple test script that inserts a single line into a database.
I checked the permissions for the file and has read and write. Am absolutely stumped. I can't seem to get ANYTHING to run through cron. Is there something I can run to test permissions?
EDIT
I have also tried the following command
/usr/bin/php /home/user/public_html/crx/cronx.php
I used whereis php and which php to locate and confirm it is all running in the right area
You have too many * values for your times.
Also, cron may not have a PATH set up correctly to use PHP.
Instead try:
*/5 * * * * /usr/bin/php /home/user/public_html/crx/cronx.php
Where /usr/bin/php is the actual path to PHP. From the console you can run which php to see the path to the PHP binary you should use.
EDIT: Here are a couple of more things to try in order to troubleshoot:
# see if cron is running just by having it create a file
*/5 * * * * touch /tmp/crontab-$(date +%s)
Another option:
Set the permissions of your PHP script to 755, and change the beginning to:
#!/usr/bin/php -q
<?php
// rest of script
Then change your cron tab to:
*/5 * * * * /home/user/public_html/crx/cronx.php
I'm still not sure if cron is the issue or the running of the PHP script.

Categories