I have a shell script that runs a clamav scan on a folder on my arch linux system and is supposed to email the results. Here's the code:
/usr/bin/clamscan -r -i /path/to/folder | /usr/bin/mailx -A gmail -s "Clam Scan Results $(/usr/bin/date +%F)" email@example.com
The above one-liner works fine if run on the bash command line and I confirmed that the account configured in
/etc/mailrc works and email is received. But I want it to run on a schedule and I setup a systemd service unit to call a script called ~/bin/virusscan.sh and a systemd timer unit to trigger it at 2 AM every night. The mailx piece after the pipe is always reporting
... email not sent
Is there a significant difference in how SystemD executes the script?
[Unit] Description=Daily virus scan [Service] Type=simple ExecStart=/home/username/bin/virusscan.sh [Install] WantedBy=multi-user.target
[Unit] Description=Execute virus scan daily at 2 AM [Timer] OnCalendar=*-*-* 02:00:00 Unit=virusscan.service [Install] WantedBy=multi-user.target
Then I can run the service imediatelly to test with:
sudo systemctl start virusscan
And the status of the service while running is the following:
virusscan.service - Daily virus scan Loaded: loaded (/usr/lib/systemd/system/virusscan.service; disabled; vendor preset: disabled) Active: active (running) since Tue 2016-10-04 11:54:39 PDT; 11s ago Main PID: 29915 (virusscan.sh) Tasks: 4 (limit: 4915) CGroup: /system.slice/virusscan.service ├─29915 /bin/sh /home/username/bin/virusscan.sh ├─29920 /usr/bin/clamscan -r -i /path/to/folder/ └─29921 /usr/bin/mailx -A gmail -s Clam Scan Results 2016-10-04 firstname.lastname@example.org Oct 04 11:54:39 hurricane systemd: Started Daily virus scan.
It looks like SystemD breaks the one liner in the script into separate processes and also it expands the string that is supposed to be the subject line of the message and removes the quotes ... That could be the problem, perhaps, and I just need to escape it properly ... Then when the service unit is done running, I always have the following line at the end ...
Oct 04 11:55:01 hurricane virusscan.sh: ... message not sent