How to set cron HOME environment variable?

0

I am using esmtp to send email as an output of cron job. The SMTP server and credentials configuration are stored under my $HOME/.esmtprc. However it looks like the $HOME env variable is set to '/', rather than to HOME from /etc/passwd as the manual suggests. I changed /usr/sbin/sendmail to output environment variables to file and I see this:

HOME='/'

However the email headers contain this:

X-Cron-Env: <HOME=/home/username>

which is correct. So why is $HOME unset for the esmtp?

mehturt

Posted 2011-07-27T07:06:08.210

Reputation: 43

Unless somebody already answered your question, it's better not to append edits all the time, but rather re-write the question so it is as clear as possible. – slhck – 2011-07-27T10:01:10.320

Do you use sudo or something else that resets your environment? Have you considered using env to set the home directory explicitly for the sendmail process? – Daniel Beck – 2011-07-27T11:11:07.930

@Daniel: no sudo. How should I use env to do that? Even if that is needed, why is it not documented? – mehturt – 2011-07-27T11:20:31.343

Thinking about this once again, it looks like the HOME is set for the script run from crontab, but unset for sendmail which is sending the output of the cron jobs. So I guess I need to set up the SMTP configuration to /etc/esmtprc and have it readable by the user(s) that is running cron jobs. – mehturt – 2011-07-27T11:25:10.827

Answers

0

I assume you are referring to a user cron, perhaps something you might get to via 'crontab -e' as a particular user.

Often cron jobs are run with $HOME set to '/'.

In some versions of cron you can change this by adding a line to the crontab.

for example, you might do a 'crontab -e' and then have lines along the following

HOME=/home/myhome
0 * * * * /bin/sh -c "echo $HOME" >> /tmp/doesitwork.log

or something similar

user3718260

Posted 2011-07-27T07:06:08.210

Reputation: 11