0

I am trying to use crontab running as a different user(due to required permissions) to create log files but my script doesnt seem to be executing

I am currently logged in as admin and I want to run the script as www-data so to edit the crontab I am using

sudo crontab -u www-data -e 

Then entering

* * * * * /path/to/sh/mkLogFile.sh 

mkLogFile.sh is a file that is owned by www-data

-rwxrw-r-- 1 www-data www-data 58 May 22 10:08 mkLogFile.sh

and its contents are

tday=`date +"%y%m%d%h%m"

`touch /path/to/log/$tday.log`

In the crontab I have also tried the following without making the file first

* * * * * /path/to/file/populateLogs >/path/to/log`date +%y%d%m`.log

none of these work to either create the file or to create and populate the file.

If I run any of these using

sudo -u www-data ./mkLogFile.sh

or any other command from the crontab they work, but not when run from the crontab. Other scripts have run in this way just none that make a file.

the log dir is also owned by www-data and has full permissions.

drwxrwxrwx 2 www-data www-data 4096 May 22 10:40 log

Is there something glaringly obvious that I'm doing wrong ? or is there a better way of doing this ?

Cob50nm
  • 911
  • 2
  • 9
  • 27
  • This may be just a typo, but you're missing a backtick on the first line of mkLogFile.sh, and you have two unnecessary backticks on the second line. Someone will probably be along to tell you that it's considered preferable to use the `$()` syntax instead, i.e. `tday=$(date +"%y%m%d%h%m")` – rojomoke May 22 '15 at 11:05
  • Thank you, it is now making files. and idea why `>/path/to/log\`date +%y%d%m\`.log` wasn't working ? – Cob50nm May 22 '15 at 11:15
  • 1
    It is a matter of escaping the date strings. Check the duplicate. – fedorqui May 22 '15 at 12:05
  • @fedorqui escaping the date string has not solved the issue – Cob50nm May 22 '15 at 13:36
  • @Cob50nm please share your last attempt, so we can see. – fedorqui May 22 '15 at 14:42

0 Answers0