4

I am trying to run a php file every night at a certain time using crontab, however the php needs to be running as a www-data because of the directory permissions. To run it as www-data I am using the root crontab and changing the user in there, like so:

* 20 * * * sudo -u www-data /usr/bin/env TERM=xterm /path/to/dailyProc.sh

dailyProc is as follows

today=`date +"%d%m%y"`

year=`date +"%y"`

dm=`date +"%m%d"`

`tar -zxf /path/to/input/$today.tgz -C /path/to/output`

echo "starting data proc"

`/usr/bin/php5 -f /path/to/dataproc.php date=$dm year=$year`

echo "data proc done"

All other commands in dailyProc.sh work but the php doesnt run. The php is using an output buffer and writing it to a file, which works fine calling it from the command line but doesnt work when calling by cron.

I can also definitely run dailyProc.sh from the command line as www-data using

sudo -u www-data dailyProc.sh

and everything works as expected.

Is there any reason I would not be able to run this php file in dailyProc.sh using crontab when everything else in it works?

7
  • Do you use #!/bin/bash on your script ? Commented May 25, 2015 at 12:49
  • There are two crontab files that run with root privileges. One is /etc/crontab, the other is /var/spool/cron/crontabs/root. Are you using one of these? Commented May 25, 2015 at 12:52
  • Check crontab -l to make sure your crontab is registered. Also, check the logs, probably at /var/log/syslog Commented May 25, 2015 at 13:17
  • @alex no but everything else in the script runs fine, do I need it or is it just better practice? To edit the crontab I used sudo contab -e I read that editing etc/crontab is not a good idea beacuse the system can overwrite it? Commented May 25, 2015 at 13:40
  • @JohnCartwright both of those indicate that dailyProc.sh is being run, which it is, however the php inside it is not. Commented May 25, 2015 at 13:42

4 Answers 4

2

Cron can be run per user too.

crontab -u www-data -e
Sign up to request clarification or add additional context in comments.

Comments

1

This works for me:

* 20 * * * su - www-data -C "/path/to/dailyProc.sh"

1 Comment

This returned the following error /usr/bin/env: Syntax error: word unexpected (expecting ")")
1

You do not need to use su or sudo in a crontab entry, because the 6th column is for the user name anyway. And you don't need to start a terminal, because you won't see it anyway. Hence, the following should do:

* 20 * * * www-data /path/to/dailyProc.sh

The Syntax error: word unexpected… you mentioned in a comment appears to be inside your code. Try running the script from the command line and start debugging from there.

2 Comments

Is the user only a parameter if you edit /etc/crontab directly ? when trying this using sudo crontab -e I get the following error /bin/sh: 1: www-data: not found
When you use crontab -e, you're creating/editing a personal crontab. Then the user column is not being used and the 6th column is already the command. Edit /etc/crontab directly if you want the script to be executed by a different user.
0

To do this I used curl inside dailyProc.sh

today=`date +"%d%m%y"`

year=`date +"%y"`

dm=`date +"%m%d"`

`tar -zxf /path/to/input/$today.tgz -C /path/to/output`

echo "starting data proc"

`/usr/bin/curl "myserver.com/dataproc.php?date=$dm?year=$year"`

echo "data proc done"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.