45

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.

starball
  • 20,030
  • 7
  • 43
  • 238
Khalilos
  • 721
  • 2
  • 9
  • 17
  • Surely just having "php" would be enough? In my experience using CentOS, it can be a BIG nightmare getting PHP configured correctly with all the security and whatnot that it entails. –  Mar 12 '14 at 16:55

4 Answers4

110

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!

Community
  • 1
  • 1
Ilia Ross
  • 13,086
  • 11
  • 53
  • 88
  • Thanks for your response, I did that but i would like to execute the script in apache crontab (crontab -uapache -e) which doesn't work yet. in fact the script work fine in crontab root (when I add it in crontab -e) – Khalilos Mar 12 '14 at 17:37
  • @Khalilos It should be `crontab -u apache -e`, is that what you meant? – Ilia Ross Mar 12 '14 at 17:40
  • yes it is, but it does not work when i add the script in 'crontab -u apache -e' – Khalilos Mar 12 '14 at 22:39
  • Should it be `/usr/bin/php -f /opt/test.php`, rather than `/usr/bin/php /opt/test.php`? – user2768 Nov 27 '15 at 11:21
  • @user2768 `Both ways (whether using the -f switch or not) execute the file my_script.php. Note that there is no restriction on which files can be executed; in particular, the filename is not required have a .php extension.` http://php.net/manual/en/features.commandline.usage.php – Ilia Ross Nov 27 '15 at 12:20
  • If you want many PHP files running as a cron job, you can put all of them into a .sh file, such as this myExample.sh: #!bin/bash /usb/bin/php /var/www/html/yourdomain/file1.php /usb/bin/php /var/www/html/yourdomain/file2.php /usb/bin/php /var/www/html/yourdomain/file3.php ... – Alex Correia Sep 19 '21 at 14:21
16

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
Philip
  • 393
  • 3
  • 9
1

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?

Community
  • 1
  • 1
Russell E Glaue
  • 1,602
  • 13
  • 9
0

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.

Rafa
  • 851
  • 8
  • 9