run .profile function as cron job

Posted by Don on Ask Ubuntu See other posts from Ask Ubuntu or by Don
Published on 2012-04-03T12:06:29Z Indexed on 2012/04/11 17:42 UTC
Read the original article Hit count: 266

Filed under:
|

In the .profile file of the root user I have defined a function, e.g.

function printDate() {
  date
}

I want to run this function every minute and append the output to cron.log. I tried adding the following crontab entry:

* * * * * printDate > $HOME/cron.log 2>&1

But it doesn't work. The cron.log file gets created, but it's empty. I guess this is because the .profile isn't read by cron, so any functions/aliases defined therein are unavailable to it. So I tried changing the crontab entry to:

* * * * * source $HOME/.profile;printDate >> $HOME/cron.log 2>&1

But this doesn't work either. It seems cron still doesn't have access to the printDate function because I see the following in cron.log

/bin/sh: printDate: not found

© Ask Ubuntu or respective owner

Related posts about cron

Related posts about .profile