I wrote a PHP script that push a CSV file into a database. I want to do this automatically every minute. I know there is a way via cron on Linux but I don't know anything about bash and think cron can't give my PHP file a callback, so I can show a progress bar for the user to see the timer interval. What do I do?
You can enter your jobs using crontab -e. If your default editor is vi, I recommend to change it nano using export EDITOR=nano because it is easy to use for starters.
Every line of the crontab file represents a job. The first 5 tokens are : minute, hour, day of month, month, day of week respectively, the last one is command so in your case first 5 tokens will be * * * * * that means run this job every minutes when the second is '00'.
You can call your php files directly using this command : php /var/www/cron.php & or using a browser wget -O /dev/null http://example.com/cron.php If you use first one you cannot use some $_SERVER variables but if you use second one, it is like a real browser.
In your case you can use like this :
* * * * * wget -O /dev/null http://example.com/cron.php
to add a cron and make it run every minute, type crontab -e and add the following line
* * * * * command you need executing
example:
* * * * * ls -l /home/ > /usr/local/users.txt
* * * * * df -h > /tmp/filesystem_usage.txt
* * * * * service httpd restart
Look at this for a starter: http://kvz.io/blog/2007/07/29/schedule-tasks-on-linux-using-crontab/
Also remember that cronjobs don't support all $_SERVER vars like 'DOCUMENT_ROOT' and 'HTTP_HOST', so try to avoid them, or use a workaround.
Some 'callback' possibilities:
- Let your script trigger another script
- Redirect the output of your cron to an another bash script
- ...
Related
I'm using whm/cpanel and the first time i'm doing a cron job.
I have a php file located in /home/my_website/public_html/wp-content/youtube_channels/cron.php
I want this to run every minute.
I have this as a cron command but i want to make sure it is correct before going forward.
***** /home/my_website/public_html/wp-content/youtube_channels/cron.php
Is this correct? I want this code to run every minute.
A crontab line
* * * * * php /path/to/phpfile.php >> /path/to/logfile.log 2>&1
* * * * * time settings
php to execute an php file, You can not just call the phpfile like an executeable file (only if you setup and add #!/usr/bin/php correctly)
>> pipes the output from the phpfile into logfile (or else)
/path/to/logfile.log the log file
2>&1 ensures that all what is comeing from STDOUT and STDERR is piped into the logfile.
More info:
https://corenominal.org/2016/05/12/howto-setup-a-crontab-file/
Run a PHP file in a cron job using CPanel (not only for CPanel user)
http://php.net/manual/en/wrappers.php.php (STDOUT/STDERR)
Have a nice cron
For a crontab entry, you need spaces between the stars -
* * * * * /home/my_website/public_html/wp-content/youtube_channels/cron.php
I'm Working on a php app where I need to have a number count in a sql data base reset at the first of the month and at 12am everyday via PHP and MYSQL.
I have googled but found nothing. As far as I know PHP needs to be accessed by a client to be manipulated.
What is the best method of having the server do this on its own?
You can use crontab
* 0 * * * /path/to/my/script
You can use cronjobs. On windows 'scheduled tasks', on linux crontab. For linux:
crontab -e
A line have to look like this to be called every day 0 am:
* 0 * * * /bin/php -q /script/path
Or, to call the file via web but do not get the content:
* 0 * * * wget --spider "http://url.com/cron.php"
If you do not have a dedicated server, check your webspace control panel for this or ask your webhoster.
I currently have a system where on the client side this Update function is called every X amount of seconds:
var Update = function(){
$.getJSON("PHP/Update.php",{
userid: userID },
function(json){
doSomething();
});
}
In the PHP file some values are updated and these new values are sent back to the client.
<?php
session_start();
include 'dbconnect.php';
$userid = $_GET['userid'];
//Change some values in the database
//Add these new values to $json array
echo json_encode($json);
mysqli_close($link);
?>'
This works great when there is exactly one client active at all time. Not so much when there are multiple or no clients active. So what I want is that this PHP files gets called once every X amount of time, regardless of how many clients there are. I realize I probably have to create a loop in PHP but I just can't get it working. Can anyone tell me how I'm supposed to do this? Thanks in advance.
The tool you use to schedule things in unix-style operating systems is cron. If your server runs MS Windows, you'll need to use some other scheduler.
There are a couple of ways that you can use cron to run your script.
First off, if you have a CLI version of PHP available (as opposed to an Apache module or a fcgi version), you can run it directly. On my system, a cron job to run the script once per minute might look like this:
* * * * * /usr/local/bin/php /path/to/your/script.php
Note that on your system, the php binary might be located in /usr/bin/ or /opt/local/bin/ or somewhere else. You'll need to check with your operating system's package repository to find out where, or type which php from a terminal.
If you use a "shebang" at the beginning of your script, you can get away with a simpler cron job:
* * * * * /path/to/your/script.php
More information about running PHP shell scripts can be found here. A word of warning though... If you launch scripts directly like this, they will run as the user who owns the cron job, which may have security (or at least permission) implications. If you don't understand what this means, please ask more questions before doing it!
If you don't have a command line PHP available, you can still have the PHP script run via cron, by fetching it from a command-line HTTP client like curl or wget or fetch (depending on your operating system). For example:
* * * * * curl -o /dev/null http://example.com/path/to/script.php
or
* * * * * wget -O /dev/null http://example.com/path/to/script.php
On a unix system you can man crontab for instructions on how to install cron jobs, and probably man 5 crontab (depending on your operating system) for instructions on the format for setting specific times. Note that cron uses 1-minute granularity. If you want greater frequency than that, you can fake it. For example, for three times per minute:
* * * * * /path/to/your/script.php
* * * * * sleep 20; /path/to/your/script.php
* * * * * sleep 40; /path/to/your/script.php
And if you want lesser frequency, you use crontab's documented format. For example, every five minutes on weekdays but every 10 minutes on weekends:
*/5 * * * 1-5 /path/to/your/script.php
*/10 * * * 6-7 /path/to/your/script.php
I've created a cron job in AWS EC2 but it is not working.
I followed below steps to create cron tab:
Step 1: I logged in to AWS EC2 Instace
step 2: crontab -e
Step 3: Insert mode
Step 4: I entered * * * * * php/var/www/html/welcome.php (To run every min.)
Step 5: :wq
Cron tab is created but not running.
Please can you any one help me if is there any PHP script means please provide me. Do I need to give spaces between every star?
First of all, you need to put an space between php and /var:
From
* * * * * php/var/www/html/welcome.php
to
* * * * * php /var/www/html/welcome.php
^
Then, you'd better use /bin/php instead of just php. To determine where the php executable is located, type which php in your console, it will give you the file path. So it will become something like this:
* * * * * /bin/php /var/www/html/welcome.php
^^^^^^^^
More things:
check if crontab is saved properly? Type crontab -l. Your new crontab line should be there.
is the script exactly in this dir? Try ls -l /var/www/html/welcome.php.
is the script executing if you do from console? Try /bin/php var/www/html/welcome.php to see if it is a script or crontab problem.
does the script have executing mode? Try chmod 755 /var/www/html/welcome.php
Keep us updated so we can find what can be causing the error.
May be a too late but anyways if you intend to run the script every minute the command should probably be
* * * * * php /var/www/html/welcome.php
Running cron on EC2 is not any different from running on any *nix server - as far as I know.
I would check of the system messages for any errors. You can also redirect stderr/stdout to a file as in
* * * * * <your script> >> /var/tmp/out.log 2>&1
and check for any issues for starters.
I have a database with a bunch of links that I want to keep updated. Basically if a link returns a 404 error code I want to remove it from the database. I have a script that I am using however I need to run it manually. How can I make this work using CRON?
in your shell as cron user (or root):
crontab -e
This will bring up your crontab file in your editor. Add a new line something like this:
* */12 * * * /path/to/script
Save/exit the file.
Now for a quick lesson on cronjobs:
-The first 5 arguments in the line tell how often, or when the cron daemon will execute the 6th argument.
-From left-right, arguments represent: minutes, hours, days, weeks, months
-An asterix (*) tells the cron to run on all values of it's associated time measurement (example * * * * * means to run every minute, of every hour, of every week, and of every month!)
In my example, * */12 * * * means to run every 12 hours.
Check out: http://kevin.vanzonneveld.net/techblog/article/schedule_tasks_on_linux_using_crontab/
To run a PHP script with cron you can use the PHP executable and the path to the script.
On most linux systems you want to edit your cron file (the crontab) with the command crontab -e. This will open up a command line based editor and you can just append your new job to the bottom of the file using this format.
<minute> <hour> <day_of_month> <month> <day_of_week> php /path/to/script
If the commands dont work for you let me know what distribution you are using and I can modify the instructions.
/usr/bin/php -q /home/user/public_html/script.php