Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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
share|improve this question
    
1  
So, to clarify: source $HOME/.profile; printDate > $HOME/cron.log 2>&1 should do it :) –  SirCharlo Apr 3 '12 at 12:39
    
@SirCharlo will this append the output to cron.log or overwrite it? –  Don Apr 3 '12 at 16:42
1  
The > operator overwrites; the >> operator appends.. Sorry, I had missed that part of your post. So it should be: source $HOME/.profile; printDate >> $HOME/cron.log 2>&1 –  SirCharlo Apr 3 '12 at 16:44
    
@SirCharlo it still doesn't work, I've updated my question with further details –  Don Apr 11 '12 at 15:25
add comment

1 Answer

up vote 3 down vote accepted
+50

Just wrap it in another bash script. Your file names will be different, but I have: test(.profile)

#!/bin/bash
function printDate() {
  date
}

wrap(a new script you can put wherever, just change the url)

#!/bin/bash
cd ~/
source ./test
printDate > ./date.log

when run from a /bin/sh

$ ls
test  wrap
$ ./wrap
$ cat date.log
Wed Apr 11 11:49:39 EDT 2012
$ 

Now just make sure your paths/environment are right

The crontab lines I just tested were:

SHELL=/bin/sh
HOME=/root
* * * * * $HOME/wrap >/dev/null 2>&1
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.