Asked  7 Months ago    Answers:  5   Viewed   55 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

93

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!

Wednesday, March 31, 2021
 
hnkk
answered 7 Months ago
21

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

Wednesday, March 31, 2021
 
nfechner
answered 7 Months ago
22

It's likely that the URL to the malicious script was distributed to a botnet, which is trying to use your webserver as a beach head. The requests will eventually subside as it's not profitable for the botnet to continue requesting a script that doesn't function.

The quick and easy fix: Add a <Location> block to your Apache configuration or a rewrite rule which will ignore these requests.

The brazen, haphazard method: Use iptables based rate limiting. iptables -A INPUT -p tcp --dport 80 -m limit --limit 60/min -j ACCEPT with a following rejection rule might help, though it may also block legit traffic, including search engine indexers and legitimate bots.

The better solution: Implement a web application firewall like Apache's mod_security. You can add an additional rule to match the incoming requests and blacklist them, or any other suitable side-effect.

Regardless of which option you take, I highly doubt that 404 errors are harming the resource usage on your server. You need to consider other factors:

  • Your PHP scripts could be causing some significant performance/efficiency issues.
  • Your Apache configuration may not be optimized to handle larger amounts of traffic.
  • Your server is compromised and you now have a much bigger problems to deal with.

You may also want to consider putting Apache behind Nginx as a reverse-proxy backend. Depending on the cause, using an HTTP cache in front of your Apache server (like Varnish) could reduce the load on your server to a relatively constant factor instead of a varying one.

TL;DR: It depends. Your server has probably been mis-configured or compromised.

Wednesday, March 31, 2021
 
Manmay
answered 7 Months ago
67

I was able to solve this with the help of the tech support from the website. Here's the solution just in case anyone was wondering. The following is the "Command to run" & needs to be added via the Control Panel (GUI) of the website.

/usr/local/php5/bin/php 

/home/username/mydomain.com/cron.php

The cron.php file stays the same.

I guess I am going to have to accept my own answer as this is the most relevant answer which carries the perfect solution. I still do thank you all for your help & appreciate all the responses.

Saturday, May 29, 2021
 
Dail
answered 5 Months ago
20

Just use crontab -e and follow the tutorial here.

Look at point 3 for a guide on how to specify the frequency.

Based on your requirement, it should effectively be:

*/10 * * * * /usr/bin/python script.py
Tuesday, June 1, 2021
 
jeremyharris
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :