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.
Related
I have php function that I wnat to run every 15 minutes. I found some questions like this:
How can I make a cron to execute a php script?
and in my case I should use this:
*/15 * * * * /usr/local/bin/php -q /path/to/my/file.p
But should I run this command in terminal or put it in my file? And once, it is executed, will it run all the time or will have time limit?
Thanks!
PHP doesn't run cron jobs, your server (or operating system) is doing this. There are two ways to put your cron job to work:
#1
Using the shell command crontab. The command crontab -l will list all existing cronjobs for your user (most likely there are none yet). crontab -e will open an editor window where you can put in a you cron job as a new line. Save, and your cron job is now running. crontab -l again and you will see it listet. crontab -r to remove all the cont jobs.
You can also start a cron job from a file. Simply type crontab filename (eg. crontab textfile.txt)
Alternatively you can also start it from within PHP. Just put your cron job into a file and start it via exec() like so:
file_put_contents( 'textfile.txt', '*/15 * * * * /usr/local/bin/php -q /path/to/my/file.php' );
exec( 'crontab textfile.txt' );
#2
If you have admin privileged on your system you can create a file in /etc/cron.d/ (for example, call it my_cronjob) and put your cron job there. In this case you probably want to run it as a user (not as admin, that would be rather insecure). This is quite easy to do, just add the user name like so:
*/15 * * * * user_name /usr/local/bin/php -q /path/to/my/file.p
(In this case the cron job will not be listet under crontab -l)
Answering your second question: As long as the cron job is listet in crontab -l or as long as the file is sitting in /etc/cron.d the cron job is running, in your case, every 15 minutes.
10 * * * * /usr/bin/php /www/virtual/username/cron.php > /dev/null 2>&1
There are two main parts:
The first part is "10 * * * *". This is where we schedule the timer.
The rest of the line is the command as it would run from the command line.
The command itself in this example has three parts:
"/usr/bin/php". PHP scripts usually are not executable by themselves. Therefore we need to run it through the PHP parser.
"/www/virtual/username/cron.php". This is just the path to the script.
"> /dev/null 2>&1". This part is handling the output of the script. More on this later.
Please read this tutorial http://code.tutsplus.com/tutorials/scheduling-tasks-with-cron-jobs--net-8800
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 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
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
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