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.
Related
I have a set a crontab in my ubuntu 16.04 to fetch data from /var/www/html/import/IMPORT-DATA.CSV through a PHP script.
*/5 * * * * php /var/www/html/cron/import-csv.php
Its working fine, and after fetching the data my PHP script will delete the file (/var/www/html/import/IMPORT-DATA.CSV).
I want to set up a script in Linux (either a crontab or something else) which can run my PHP script once if the IMPORT-DATA.CSV file uploaded in the directory /var/www/html/import/
There's a couple of ways I can think of off the top of my head:
Find out if your FTP server can be configured to trigger a script for you. (For example, pureftpd's upload-script function https://linux.die.net/man/8/pure-uploadscript , not all FTP server software can do this.)
Setup an inotify watcher perhaps with inotify-tools. (You could create your own with PHP as well (inotify extension), but that would probably negate any performance gain since you'd have an instance of PHP constantly running.)
And a sort-of third option: If you're merely wanting to avoid invoking PHP just to see that the file isn't there - you can chain to a bash file test before invoking your PHP script. The cron call still runs every 5 minutes, but only calls PHP if the file is there. (Note that I haven't exactly tested this, but pretty confident it would work.)
SHELL=/bin/bash
*/5 * * * * test -e /var/www/html/import/IMPORT-DATA.CSV && php /var/www/html/cron/import-csv.php
One way is you can distribute your tasks is 3 different scripts and chain them in cron.
*/5 * * * * php /var/www/html/cron/import-csv.php && /var/www/html/cron/YOUR_PERFORM_SOME_OTHER_TASK_PHP_SCRIPT && /var/www/html/cron/YOUR_DELETE-CSV-SCRIPT.php
&& will make sure that, the next script runs only if the previous
ran sucessfully.
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
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.
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.
I need to run a php script to generate snapshots using CutyCapt of some websites using crone job, i get websites' addressess from database and then delete this record after generating screenshots.
i used */5 * * * * /usr/bin/php -f /path/generate.php
it didn't worked for crone job but if i access the same file using browser it works fine, and if run this script using command php from command line it also works fine.
then i just created another file and accessed the url using file_get_contents; added this file to crone job it worked fine for some days but now this approach is also not working. i don't know what happened. i didn't change any of these files.
I also tried wget command to access that url but failed to get required out put.
my crontab is now looks like this
*/5 * * * * wget "http://www.mysite.com/generate.php" -O /dev/null
Strange thing is that crone job executes fine it fetches data from database and deletes record as well but does not update images.
Is there any problem with rights or something similar that prevents it to generate images using crone job but not when accessed using browser.
Please help i am stuck.
I don't know what your script is doing internally, but keep in mind that a user's cron jobs do not inherit the users environment. So, you may be running into a PATH issue if your php script is running any shell commands.
If you are running shell commands from the script, try setting the PATH environment variable from within your php script and see if that helps.
is there any user credintials on this page , such as Basic authentication ?
if so , you have to define the user name and password in wget request like
wget --http-user=user --http-password=password "http://url" ?
and try another solution by running yor script from php command line
so your crontab could look like
*/5 * * * * /usr/bin/php -f /path/to/generate.php
try this solution it will work and it is better than hitting the server to execute background operations on your data
and I hope this helps