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@gmail.com
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@gmail.com 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.
id
command. – Mark Plotnick Mar 07 '15 at 13:49export USER=$(id -u -n)
– Mark Plotnick Mar 07 '15 at 13:59whoami
would be printing nothing. – Mark Plotnick Mar 07 '15 at 15:06export USER=$(id -u -n)
won't produce any output, but it will set theUSER
variable. Can you show us the code that is not working, for example, the code that tries to read the config file? – Mark Plotnick Mar 07 '15 at 20:27export HOME=~
. It's odd thatmailx
doesn't read/root/.mailrc
. It does on opensuse, even when HOME is not set. I'll have to install arch to see why. – Mark Plotnick Mar 09 '15 at 07:54echo $whoami
in your script will echo a blank line becausewhoami
is not normally a variable. It is a standard command, though.whoami
orecho $(whoami)
should output a username. – Mark Plotnick Mar 09 '15 at 07:57whoami
- no echo, no $ - should print the username. The absence of $HOME is probably an issue withsystemd
, and the particular version ofmailx
you have made it kind of a unique problem. – Mark Plotnick Mar 09 '15 at 10:18