run crond a user

XK8ER
XK8ER used Ask the Experts™
on
hello there,
I would like to know how can I run an script every 5 minutes as user apache..
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2013
Top Expert 2013

Commented:
Log in as user apache and issue

crontab -e

The user's crontab will be opened by (probably) vi.

Add this line:

*/5 * * * * /path/to/script >/path/to/logfile 2>/path/to/errorlog

Save the file and you're done.

wmp

Author

Commented:
any other way to do this rather than to login as apache?
Most Valuable Expert 2013
Top Expert 2013

Commented:
Another possibility is using root's crontab.

Log in as root, issue crontab -e and add

*/5 * * * * su - apache -c "/path/to/script >/path/to/logfile 2>/path/to/errorlog"

On some systems you can also place a file into the /etc/cron.d directory, containing a line in the format

*/5 * * * * apache /path/to/script >/path/to/logfile 2>/path/to/errorlog

if you are using  redhat/centos or similar linux distro,
you can add system jobs in file /etc/crontab.

In this file you can set the user for the job.

sample:

*/5 * * * *    apache  /path/to/script >/path/to/logfile 2>/path/to/errorlog

bye
Most Valuable Expert 2013
Top Expert 2013

Commented:
It's good practice to never touch /etc/crontab.

Better use the /etc/cron.d directory!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial