2

I have created a script that smartd daemon (smartmontools) executes as per command below.

DEVICESCAN -a -m email -M test -M exec /usr/share/smartmontools/my-script -n stand...

Smartd appears to be running as root looking at the top command output but it fails to find a config file in /root. $USER, $HOME, whoami, export USER=$(id -u -n) commands do not echo anything, not even an empty line. When executed manually in terminal it prints the current username as expected. Script was chmod'ed to 700.

Why is this happening?

Edit: Here is the actual script that I want to run.

#!/bin/sh

export USER=$(id -u -n)
echo $whoami
echo "USER: "
echo "$USER"
echo $HOME

EMAIL_EXEC="mailx"
EMAIL_ACCOUNT="gmail"
EMAIL_SUBJECT="simplified"
EMAIL_BODY="simplified"

echo "$EMAIL_BODY " | $EMAIL_EXEC -A $EMAIL_ACCOUNT -s "$EMAIL_SUBJECT" [email protected]

After restarting smartd errors are found in the log and no email is sent unlike if executed manually as root.

sudo systemctl restart smartd
journalctl -u smartd

Here is the output of journalctl log:

Test of /etc/smartmontools/run.d/email to [email protected] produced unexpected output (69 bytes) to STDOUT/STDERR:
USER:
root
Account `gmail' does not exist.
No mail for root

export USER=$(id -u -n) appears to have set the $USER variable as you mentioned. "gmail" is a profile in /root/.mailrc file. Same error is produced when running as non-root user because the .mailrc file is missing in users home.

How can I set the $HOME variable like the $USER variable? That might give an idea why it's not finding the file.

15
  • Related: Who sets $USER and $USERNAME environment variables Commented Mar 7, 2015 at 9:07
  • Looked at the link, but unfortunately no alternatives to $USER variable produced different output.
    – DominicM
    Commented Mar 7, 2015 at 13:28
  • 1
    The USER variable is set by the login program. Daemons don't have it in their environment. If your script needs to know the userid it's running as, one thing it can do is run the id command. Commented Mar 7, 2015 at 13:49
  • 1
    export USER=$(id -u -n) won't produce any output, but it will set the USER variable. Can you show us the code that is not working, for example, the code that tries to read the config file? Commented Mar 7, 2015 at 20:27
  • 1
    Try adding export HOME=~ . It's odd that mailx doesn't read /root/.mailrc. It does on opensuse, even when HOME is not set. I'll have to install arch to see why. Commented Mar 9, 2015 at 7:54

1 Answer 1

2

Your ultimate problem was that mailx, when called from a shell script run by smartd, started by systemd, on Arch Linux, was not reading the root user's $HOME/.mailrc file.

This was caused by a few factors:

  • The mailx on Arch Linux, s-nail, relies on the environment variable HOME when looking for the .mailrc file. If HOME isn't present, it uses the current working directory.
  if ((cp = getenv("HOME")) == NULL)
      cp = "."; /* XXX User and Login objects; Login: pw->pw_dir */
  homedir = savestr(cp); 

$USER, $LOGNAME, $HOME, $SHELL

User name (twice), home directory, and the login shell. The variables are set for the units that have User= set, which includes user systemd instances.

Since there was no HOME variable in the environment provided to smartd, and it was likely started in the / directory, mailx didn't read the /root/.mailrc file.

To fix: add the line

export HOME=~

or

export MAILRC=~/.mailrc

to the shell script before it invokes mailx

or (not tested by me) add

User=root

to the [Service] stanza of your smartd.service unit configuration file.

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.