0

I have a script which sends emails. It works perfectly when run manually but I doesn't work when run with crontab:

#!/bin/bash
cat $HOME/myfolder/mailbody | /bin/mailx -v -s "mymail" -a file.txt xxx.yyy@zzz.com > $HOME/myfolder/script.log

When run with crontab, nothing is written in "$HOME/myfolder/script.log". I can't understand why with crontab the command to send the email is not run.

  • 1
    Does this answer your question? [Why is my crontab not working, and how can I troubleshoot it?](https://serverfault.com/questions/449651/why-is-my-crontab-not-working-and-how-can-i-troubleshoot-it) – Gerald Schneider Apr 16 '20 at 09:34
  • I'm 100% sure crontab works since, if I add a log line before mailx command, it gets written. The problem is that, with crontab, the line code to send the mail seems not to be executed. I don't know how to solve this. – mark009 Apr 16 '20 at 09:39
  • Which crontab file? Could it be $HOME is not set? – davidgo Apr 16 '20 at 09:41
  • I edited crontab using "crontab -e" and I'm sure $HOME is set because I see it in the log if I add a log line before mailx command. – mark009 Apr 16 '20 at 09:43

1 Answers1

0

I solved it by specifying the absolute path to mailx attachment argument:

#!/bin/bash
cat $HOME/myfolder/mailbody | /bin/mailx -v -s "mymail" -a /path/to/file.txt xxx.yyy@zzz.com > $HOME/myfolder/script.log