dimanche 8 mars 2015

Shell script printing blank $USER variable when executed by Smartd


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.



Aucun commentaire:

Enregistrer un commentaire