Viewed   103 times

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

Thanks in advance, Please Help me

 Answers

2

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!

Friday, December 9, 2022
2

Could be that although you have 755/777 permissions, SELinux is blocking httpd from writing/creating dirs.

Try:

chcon -R -t httpd_sys_content_t /path/to/www
chcon -R -t httpd_sys_content_rw_t /path/to/www/dir/for/rw

Further info: http://wiki.centos.org/TipsAndTricks/SelinuxBooleans

Thursday, August 18, 2022
 
2

test.php:

<?php
print_r($argv);
?>

Shell:

$ php -q test.php foo bar
Array
(
    [0] => test.php
    [1] => foo
    [2] => bar
)
Thursday, October 13, 2022
 
chris_e
 
4

As said before, CLI scripts by default have no time limit.

But I would also like to mention an alternative to your cron job approach:
You can fork a CLI PHP script from a PHP script under webserver control. I have done this many times. It is especially useful if you have a script with long execution time which must be triggered by some website user action (e.g. building a very large archive file and send a download link by email when the file is complete). I usually fork a CLI script from a webserver PHP script using the popen() function. This allows to nicely transfer parameters to the new script instance like this:

$bgproc = popen('php "/my/path/my-bckgrnd-proc.php"', 'w');
if($bgproc===false){
  die('Could not open bgrnd process');
}else{
  // send params through stdin pipe to bgrnd process:
  $p1 = serialize($param1);
  $p2 = serialize($param2);
  $p3 = serialize($param3);
  fwrite($bgproc, $p1 . "n" . $p2 . "n" . $p3 . "n");
  pclose($bgproc);
}

In the CLI script you would receive these params like this...

$fp = fopen('php://stdin', 'r');
$param1 = unserialize(fgets($fp));
$param2 = unserialize(fgets($fp));
$param3 = unserialize(fgets($fp));
fclose($fp);

...and do anything with them that would take to long under webserver control.

This technique works equally well in *nix and Windows environments.

Sunday, August 14, 2022
2

To elaborate on Sougata Bose's answer, I think the OP wants a command to be run every 10 seconds from a start time; not 10 seconds after the first minute and every subsequent minute.

cron only has a resolution of 1 minute (there are other tools I think that may have finer resolutions but they are not standard on unix).

Therefore, to resolve your issue you need 60 seconds / 10 seconds = 6 cron jobs, each with a sleep.

e.g. run crontab -e and add the following lines to your chosen editor:

* * * * * ( /usr/bin/wget http://api.us/application/ )  
* * * * * ( sleep 10 ; /usr/bin/wget http://api.us/application/ )  
* * * * * ( sleep 20 ; /usr/bin/wget http://api.us/application/ )  
* * * * * ( sleep 30 ; /usr/bin/wget http://api.us/application/ )  
* * * * * ( sleep 40 ; /usr/bin/wget http://api.us/application/ )  
* * * * * ( sleep 50 ; /usr/bin/wget http://api.us/application/ )  
Thursday, November 17, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :