How to run php jobs regularly in my Amazon EC2 server? - php

I have a Amazon EC2 server.
And I would like to run php jobs every 1 hours to get the latest information from websites and insert them into database.
But I am not good at server.
Any suggestions?

From this URL, you call crontab from the shell and to execute every hour, add the "00" at the beginning, point to the location of PHP and the script you want to run.
# crontab -e
00 * * * * /usr/local/bin/php /home/dir/myscript.php

As for any Linux server, use crontab.
On the terminal (SSH) :
crontab -e
See the man page for cron

Related

Execute PHP script in cron job

In our centos6 server. I would like to execute a php script in cron job as apache user but unfortunately it does not work.
Here is the edition of crontab (crontab -uapache -e)
24 17 * * * php /opt/test.php
and here is the source code of "test.php" file which works fine with "apache" user as owner.
<?php exec( 'touch /opt/test/test.txt');?>
I try to replace php with full path of php (/usr/local/php/bin/php) but also it doesn't work.
Automated Tasks: Cron
Cron is a time-based scheduling service in Linux / Unix-like computer operating systems. Cron job are used to schedule commands to be executed periodically.
You can setup commands or scripts, which will repeatedly run at a set time. Cron is one of the most useful tool in Linux or UNIX like operating systems. The cron service (daemon) runs in the background and constantly checks the /etc/crontab file, /etc/cron./* directories. It also checks the /var/spool/cron/ directory.
Configuring Cron Tasks
In the following example, the crontab command shown below will activate the cron tasks automatically every ten minutes:
*/10 * * * * /usr/bin/php /opt/test.php
In the above sample, the */10 * * * * represents when the task should happen. The first figure represents minutes – in this case, on every "ten" minute. The other figures represent, respectively, hour, day, month and day of the week.
* is a wildcard, meaning "every time".
Start with finding out your PHP binary by typing in command line:
whereis php
The output should be something like:
php: /usr/bin/php /etc/php.ini /etc/php.d /usr/lib64/php /usr/include/php /usr/share/php /usr/share/man/man1/php.1.gz
Specify correctly the full path in your command.
Type the following command to enter cronjob:
crontab -e
To see what you got in crontab.
EDIT 1:
To exit from vim editor without saving just click:
Shift+:
And then type q!
I had the same problem... I had to run it as a user.
00 * * * * root /usr/bin/php /var/virtual/hostname.nz/public_html/cronjob.php
You may need to run the cron job as a user with permissions to execute the PHP script. Try executing the cron job as root, using the command runuser (man runuser). Or create a system crontable and run the PHP script as an authorized user, as #Philip described.
I provide a detailed answer how to use cron in this stackoverflow post.
How to write a cron that will run a script every day at midnight?
I tried all combinations with PATHs, but don't work. Probably they are needed.
In my case, with Centos 7, a reboot or server worked.

Cron job using codeigniter on an ubuntu server

I'm having trouble setting up a cron job.
I have a cron job created at a url like so:
myurl.com/cron
When that link is accessed, it is run.
How do I set it so that it runs automatically, every 10 minutes?
I can't figure out how to create a cron job. I know that that's what I need to do, but I can't figure out how to do it for a ubuntu server.
Login via SSH and in the shell, type the following:
crontab -e
Then, place the following line:
*/10 * * * * /path/to/command
Save the file and that command will be called every 10 minutes.

Want to set cron job for wordpress (Backwpup plugin)

This is my first contact with cron jobs, so I'm sorry if my question sounds dumb.
BackWPup is for making a back up automatically after some period of time, but its own cron job does not work correctly. It starts only when I sign into wp-admin. So I decided to use the server's cron jobs, but I don't know how to. It says:
If you would use the cron job of your hoster you must point it to the
url: http://example.com/wp-cron.php
Also, I want to know how to remove a job.
Note: I have only ssh access, there is no hosting control panel. OS: CentOS.
I guess you should add the command
wget http://example.com/wp-cron.php >> /path/to/my/wp-cron.log 2>&1
to the crontab. Of course you can use any other CLI http-tool instead of wget, but it's the most simple I know and I think is sufficient here.
Call
crontab -e
then add a line like
0 * * * * wget http://example.com/wp-cron.php >> /path/to/my/wp-cron.log 2>&1
This will call this command every hour. For further information see man crontab.

How can i make a code run automatically every 30 mins in shell script or PHP?

I have a code which I want to run every 30mins. Since it is not possible to do it manually over 30 days I wanted to make it an automated process. Please suggest me how to do it. I want to execute this command in Putty.
--user root --host 10.0.2.140 --port 3306 --socket /var/lib/mysql/mysql.sock
Use crontab, your entry would look like this:
*/30 * * * * /path/to/your/command
Unix and derivatives use Cron Jobs
http://en.wikipedia.org/wiki/Cron
Windows uses scheduled tasks.
https://superuser.com/questions/85297/how-to-schedule-a-task-in-windows-7
Note that there are also some Cron clone implementations for windows available if you prefer.

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