How to run crontab-e? - php

I am currently reading this documentation here where I want to use CRON. Now it says in the first section that I need to enter in a command: crontab -e.
Do I only need to enter this in a simple text editor file and just upload the file into the server?
I am using helios.hud.ac.uk so would this be the correct command:
* * 25 10 * helios.hud.ac.uk/u00000000/Mobile/inactivatesession.php
This will execute this php script below (inactivatesession.php):
<?php
include('connect.php');
$createDate = mktime(0,0,0,10,25,date("Y"));
$selectedDate = date('d-m-Y', ($createDate));
$sql = "UPDATE Session SET Active = ? WHERE DATE_FORMAT(SessionDate,'%Y-%m-%d' ) <= ?";
$update = $mysqli->prepare($sql);
$update->bind_param("is", 0, $selectedDate);
$update->execute();
?>
The url for this php script is: helios.hud.ac.uk/u00000000/Mobile/inactivatesession.php
I havn't used CRON before so just need little help on it.
Thanks

If you are making a crontab that will access a remote webpage (which is what this is as it is not on your local server) you need to prepend the URL with wget
* * 25 10 * wget -O - http://helios.hud.ac.uk/u00000000/Mobile/inactivatesession.php
It will run the script on the server and output it to standard output (which in most servers will be emailed to you)
This assumes that you have a linux machine. crontab -e sets up a cron tab for your user account. So you can't really upload a crontab, but if you have cpanel or similar, most times you have access to cron from there.

You open a shell (probably through SSH) to your server
You run the command crontab -e
You edit the crontab according to your needs (if you want to run a php script over http you need to use wget)
You save and exit If you didn't make any mistakes, you will get a message that crontab was updated

Related

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.

Setting up a Cron Job on CPanel to executes a PHP script

As implied in the title, the Cron Job is supposed to execute a php file (update.php, to be specific). The php file then writes to a csv file stored in the same directory.
I have the time set to * * * * * so that it executes every minute. The command is written as follows:
php -q /home//public_html/wallboard/update.php
I don't believe this is causing any errors, though it also doesn't seem to write to the CSV file. When I visit update.php in a browser, however, it executes and writes to the CSV file immediately. I'm not experienced with Cron Jobs and I'm sure there's an issue, but I don't know what exactly that issue is. Let me know if you have suggestions/questions. Any help is appreciated!
Current Command:
* * * * * usr/bin/php -q /home/<user>/public_html/wallboard/update.php
update.php:
<?php
include('lib/HelpDeskView.php');
include('lib/WallboardDisplay.php');
include('helpdesk.csv');
$helpdesk = new HelpDeskView();
$text="\r\ntest,test,test";
file_put_contents( "helpdesk.csv" , $text, FILE_APPEND);
Since your script resides in your public_html directory you can use wget for your Cron Job
wget -O - -q https://yoursite.com/wallboard/update.php
-O - output is written to the standard output in this case it will go to the email address you specify in CPanel
-q quiet mode
IMHO the best way is to contact support and ask them about command line syntax.
This is how I'm doing it at my linux server using cPanel.
This runs script.php which is stored in public root. Course, replace <username> in command line with your username.
At another server I'm using same command line with /usr/bin/php instead of php at the beginning of line, but I'm aware that not all servers use same command line. Some require php-cli in command line instead of php, some don't "like" -f argument, etc. So try various combinations.
To find more suggestions check out this SO topic too: Run a PHP file in a cron job using CPanel
Important thing: When trying different commands wait at least a minute (this case) to see if it works because Cron doesn't fire your script immediately.
Try to execute the same command in PHP CLI and check if it gives you any error, you might be missing some libraries or references required for CLI execution.
/usr/bin/php -d register_argc_argv=On /home/USERNAME/public_html/DOMAIN/artisan AMIR:HOME

Cron job is not working-cpanel VPS Optimized 3

This is my code. File name is test_cron.php(inside the crtest folder).
Cron command is: /usr/local/bin/php -q /home/portroot/public_html/crtest/test_cron.php
It should be run on the server every minute. It should generate the text files every minute. But nothing is happen. I gave an email address also. But I didnt get any email. Please help me to correct this.
<?php
//Cron command: /usr/local/bin/php -q /home/portroot/public_html/crtest/test_cron.php
$filename = "./public_html/crtest".time().".txt";
$handle = fopen($filename,'w') or die("Cannot open file");
for($i=0;$i<10;$i++)
{
$con = "Hello world \n";
fwrite($handle,$con);
}
fclose($handle);
?>
u have to do 2 different test :
check ur script without using Cron by running it in browser and check if u get the result u want.
check ur Cron with a very sample script like an insert query to enter some data to ur database.
Then u can find the problem.
Please follow below step to check your code and setup cron on server:
1. First check your code is successfully running on your local system or not.
2. If you need to call a php script using URL; you can simply use lynx, curl or wget. Make sure you've placed your php script within the www or public_html directory and call the path properly on the cronjob.
*/2 * * * * wget -q http://localhost/test_cron.php
3. I've used this command to activate cron job for this.
/usr/bin/php -q /home/username/public_html/yourfilename.php
on mostly server and it works fine.
/usr/bin/php is php binary path (different in some systems ex: freebsd /usr/local/bin/php, linux: /usr/bin/php)

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.

Cron job to execute a PHP program

I have a Linux server and in this I want to execute a cron job for sending birthday mail to all my friend with a PHP program. I want to create a php program that read data from database and send the mail.
I want to know the command of cron job to execute the program on every day automatically. I have no knowledge of Linux commands.
You will want to read up a little bit on the 'crontab' command but basically you will do this.
From a linux command prompt run the crontab command.
Then add this entry:
* * * * * php yourscript/path
You can set what time by modifying the * values. See this URL for information on that:
http://adminschoice.com/crontab-quick-reference
This is the command to add to your crontab file:
0 0 * * * /usr/bin/php /path/to/your/script.php
Adjust the paths to the PHP interpreter and your script as necessary. It will run your script every day at midnight.
This is done using a cron table in unix systems, including linux. Check out some example documentation:
http://en.wikipedia.org/wiki/Cron
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/crontab.html
You'll find many more, if you google for crontab, or if you check out the man crontab pages on your linux box

Categories