I am a bit baffled here, and not really sure how to debug this. I have a self-written bash script, that checks if a samba share is active, and if not, sends me an email.

Script is in /root/SKRIPTS/, permission looks like this:

-rwxr-xr-x   1 root  wheel   281 Nov  8 08:54 test_samba_shares.sh
-rwx------   1 root  wheel    39 Nov  7 13:56 smbclient.cred

Content of the .sh script is this:

smbclient -L -A /root/SKRIPTS/smbclient.cred | grep -q Backup

if [ $? -eq 0 ]
    echo "Backup_* mounted, nothing to do"
    echo "Subject: Samba has failed" | /usr/sbin/sendmail -v mail@localmail.server

return 1;

/etc/crontab looks like this (entry only)

10 1 * * * root /root/SKRIPTS/test_samba_shares.sh

The thing is, if I, as root, run this, it works flawlessly. But every night I get an email that samba has failed, indicating that the cron script somehow trips into the else path. How can this be? Am I missing something obvious here? What would be the best way to debug this?

Thanks for your help

  • 2
    First thing, if you what to make sure your bash script is being interpreted by bash, make #!/bin/bash the first line in the script. Otherwise cron may be executing the script under /bin/sh which may not link to bash and will certainly not give you all the features as /bin/bash. – doneal24 Nov 12 '18 at 15:28
  • For further troubleshooting, start with the cron tag's info page – glenn jackman Nov 12 '18 at 18:16

Sometimes, cron jobs don't run with the $PATH that you might be expecting. First and foremost, I would try fully-qualifying the path to your smbclient executable, just to eliminate that as a possibility. Or, explicitly export your PATH in the wrapper script that's called by cron.

|improve this answer|||||
  • You can also set the PATH environment variable in the crontab file itself (but be sure to put that before the item you want it to apply to). – Gordon Davisson Nov 13 '18 at 6:02
  • 1
    That was it, thanks a lot! I will keep the PATH issue in mind for the next scripts, greatly appreciated. – Sebastian Nov 13 '18 at 8:00

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.