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.

4
  • 1
    Does this answer your question? Why is my crontab not working, and how can I troubleshoot it? Apr 16 '20 at 9: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 9:39
  • Which crontab file? Could it be $HOME is not set?
    – davidgo
    Apr 16 '20 at 9: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 9:43
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.