0

When I run this mail command from the terminal, there's no problems:

mail -s "Test Mail" [email protected] < home/user/example.txt

When I add it to a bash script that mainly does this, there's no problems:

tmpString=$(ssh user@server `perl -s` < /home/user/perlscript.pl 2> errorfile.txt)
mail -s "Test Mail" [email protected] < home/user/example.txt

But, when I create a crontab to run the bash script, the mail never gets sent:

10 14 * * * /home/user/mybashscript 

However, I do recieve an email from cronjob about the output. The email with the subject "Test Mail" never arrives.

Anyone had this issue before?

The trace:

Ok, I'll just change some of personal related naming. The mail should be sent before the last echo, but it's not showing up in the trace?

+ . /home/user/.bashrc
++ PS1=`\u@\h \w$`
++ TERM=vt100
++PATH=/usr/sbin:/usr/bin:/bin
++ ssh user@example `perl -s`
+ scriptString=`$VAR` = ``\``Build was OK`\``;`
+echo `$VAR1`= ``\``Build was OK`\``;`
$VAR1 = `Build was OK`;
+ [[ $VAR1 = `Build was OK`; == *\B\u\i\l\d\ \f\a\i\l\e\d* ]]
+ echo

Thanks!

2
  • What are the permissions of a file? Were you able to add another cron job with another bash file that i.e simply echoes in a file to make sure that your cron job can be executed?
    – pankar
    Oct 19, 2012 at 11:59
  • When the cron jub runs, I recieve an email from it with what i echoed, so I think it runs OK.
    – user1758367
    Oct 19, 2012 at 12:35

1 Answer 1

0

crontab don't have the same environment as yours, so in the top of your script, after the shebang, put :

. ~/.bashrc || . /etc/profile

this will set PATH for child process.

Moreover, modify the shebang to #!/bin/bash -x to turn debug ON, and in your crontab do this :

* * * * * /path/to/script >/tmp/trace 2>&1

and come again with the debug trace.

7
  • Thanks for your reply. I tried adding the line, but the mail still isn't being sent.
    – user1758367
    Oct 19, 2012 at 12:12
  • See my edited post after Moreover Oct 19, 2012 at 12:15
  • Ok, I'll just change some of personal related naming. The mail should be sent before the last echo, but it's not showing up in the trace? + . /home/user/.bashrc ++ PS1=\u@\h \w$ ++ TERM=vt100 ++PATH=/usr/sbin:/usr/bin:/bin ++ssh user@example perl -s + scriptString=$VAR = \Build was OK`;` +echo $VAR1= \Build was OK`;` $VAR1 = Build was OK; + [[ $VAR1 = Build was OK; == \B\u\i\l\d\ \f\a\i\l\e\d ]] + echo
    – user1758367
    Oct 19, 2012 at 12:28
  • Please, paste it in your post or on pastebin.com Oct 19, 2012 at 12:30
  • Ok, I edited my first post
    – user1758367
    Oct 19, 2012 at 12:33

You must log in to answer this question.