• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 359
  • Last Modified:

script runs from prompt but not in crontab

The following script runs from a command prompt. However, it doesn't run from my cronjob on my Debian box, other cronjob run. At first I specified the path, then I put the script in /usr/bin. It still doesn't the script has the same permission and owner as "mrtg". mrtg runs correctly from the cronjob.


I'm stumped.


#! /bin/bash

raid=`cat /proc/rd/status`
OK="OK"
if [ "$raid" == "$OK" ]; then
        echo "BACH raid working" | mail noc@cshore.com
        else
        echo "BACH raid NOT working" | mail noc@cshore.com
        fi
~
~
~
0
duficy
Asked:
duficy
  • 8
  • 6
  • 3
1 Solution
 
jlevieCommented:
I think the problem might be the first line. From what's above there's a space between #! and /bin/bash. Try taking the space out.

The only other thing that occurs to me is that you don't specify a full path to the external commands in the script (cat & mail). You could try fully qualifying those.

Have you checked root's mail to see if cron is reporting any errors (assuming this is in root's crontab)? If it's not in root's crontab check local mail for the user that owns the crontab. Also as a debugging tool you can add an echo or two, say like:

#!/bin/bash

echo "Debug-beginning
raid=`/bin/cat /proc/rd/status`
echo "Debug-<$raid>"
OK="OK"
if [ "$raid" == "$OK" ]; then
       echo "Debug-RAID OK"
       echo "BACH raid working" | /bin/mail noc@cshore.com
       else
       echo "Debug-RAID Not OK"
       echo "BACH raid NOT working" | /bin/mail noc@cshore.com
       fi
echo "Debug-done"

The echo's should generate an email to the user that owns the crontab.
0
 
ahoffmannCommented:
> .. a space between #! and /bin/bash.
This is not necessarry, except there is one more stupid bash implementation.

I vote for full paths too.

crons produces mail to root for each script which failes (means exits with status != 0), check root's mail.
0
 
duficyAuthor Commented:
I added the paths and debug info. The new scripts runs correctly but only from the command prompt. I can't find any crontab errors (crontab is owned by root) There's no mail for root and I can't find any crontab info in the log files.


#  /etc/syslog.conf     Configuration file for syslogd.
#
#                       For more information see syslog.conf(5)
#                       manpage.

#
# First some standard logfiles.  Log by facility.
#

auth,authpriv.*                 -/var/log/auth.log
*.*;\
        local0,local1,local2,local3,local4,local5,local6,local7.none;\
        radius.none;\
        auth,authpriv.none              -/var/log/syslog
cron.*                          -/var/log/cron.log
daemon.*                        -/var/log/daemon.log
kern.*                          -/var/log/kern.log
lpr.*                           -/var/log/lpr.log
mail.*                          -/var/log/mail.log
user.*                          -/var/log/user.log
uucp.*                          -/var/log/uucp.log

#
# Logging for the mail system. Split it up so that
# it is easy to write scripts to parse these files.
#
mail.info                       -/var/log/mail.info
mail.warn                       -/var/log/mail.warn
mail.err                        -/var/log/mail.err

# Logging for INN news system
#
news.crit                       -/var/log/news/news.crit
news.err                        -/var/log/news/news.err
news.notice                     -/var/log/news/news.notice

#
# Some `catch-all' logfiles.
#
*.=debug;\
        auth,authpriv.none;\
        local0,local1,local2,local3,local4,local5,local6,local7.none;\
        radius.none;\
        news.none;mail.none     -/var/log/debug
*.=info;*.=notice;*.=warn;\
        auth,authpriv.none;\
        local0,local1,local2,local3,local4,local5,local6,local7.none;\
        radius.none;\
        cron,daemon.none;\
        mail,news.none          -/var/log/messages

#
# Emergencies are sent to everybody logged in.
#
*.emerg                         *

#
# I like to have messages displayed on the console, but only on a virtual
# console I usually leave idle.
#
#daemon,mail.*;\
#       news.=crit;news.=err;news.=notice;\
#       *.=debug;*.=info;\
#       *.=notice;*.=warn       /dev/tty8

# The named pipe /dev/xconsole is for the `xconsole' utility.  To use it,
# you must invoke `xconsole' with the `-file' option:
#
#    $ xconsole -file /dev/xconsole [...]
#
# NOTE: adjust the list below, or you'll go crazy if you have a reasonably
#      busy site..
#
daemon.*;mail.*;\
        news.crit;news.err;news.notice;\
        *.=debug;*.=info;\
        *.=notice;*.=warn       |/dev/xconsole

radius.*                -/var/log/radius.log
local1.*                -/var/log/radius.log
local2.*                -/var/log/ppp.log
local6.*                -/var/log/named.log
local7.*                -/var/log/ftpd/proftpd.log
                                                 
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
jlevieCommented:
Okay... How are you setting up the job, via "crontab -e"? And what does the crontab look like?
0
 
ahoffmannCommented:
IMHO bash is not the choice for root, probably not the problem but worth a try: replace /bin/bash by /bin/sh
0
 
duficyAuthor Commented:
>Okay... How are you setting up the job, via "crontab -e"?

Yes.


>And what does the crontab look like?


 Rotate and process web server logs
0 0 * * * /home/www/admin/bin/apache
0,5,10,15,20,25,30,35,40,45,50,55 * * * * mrtg /home/mrtg/webspace/mrtg.cfg
0 4 * * * rdate -s time.mit.edu
0 9 * * * /usr/bin/raid

>>replace /bin/bash by /bin/sh

Tried that. Why don't you like bash
0
 
ahoffmannCommented:
use full paths in crontab too.

>> Why don't you like bash
- 'cause there are tooo many ways reading rc files, but no way to find out what was actually read
- it's designed as interactive shell
- :() { :;};:
0
 
jlevieCommented:
Well, on Linux sh is bash, so it really doesn't make any difference whether you use /bin/bash or /bin/sh in scripts.

However, for portability considerations one should get in the habit of using #!/bin/sh in scripts and be careful not to use any 'bashisms'.
0
 
duficyAuthor Commented:
Thanks for the info on bash/sh.

>>use full paths in crontab too.

I have that

0 9 * * * /usr/bin/raid


However, my script still doesn't run.
0
 
ahoffmannCommented:
> .. on Linux sh is bash ..
I know, it's a symlink, usually.
I also know bash behaves different according to its name, but never digged deeper in this behaviour. So I leave comments about bash's sh-compatibility to more bash-experianced people.

Back to the question: the /bin/sh suggestion just enshures now that there is no sh vs. bash incompatibility.

duficy, could you please modify your cron to start following:

(/bin/sh -x /usr/bin/raid) 2>&1 | /bin/mail -sx-test root
0
 
duficyAuthor Commented:
>>
duficy, could you please modify your cron to start following:

(/bin/sh -x /usr/bin/raid) 2>&1 | /bin/mail -sx-test root
>>

I don't understand what you want me to do. Do you want me to create a new script and add that to my crontab ?
0
 
ahoffmannCommented:
# change crontab entry from
0 9 * * * /usr/bin/raid
# to
0 9 * * * (/bin/sh -x /usr/bin/raid) 2>&1 | /bin/mail -sx-test root

# adjust times as you like:)
0
 
duficyAuthor Commented:
>>
0 9 * * * (/bin/sh -x /usr/bin/raid) 2>&1 | /bin/mail -sx-test root
>>

I added that to my crontab, the parenthesis confused me. OK it's working. Please explain WHY ?!?!?! Is it because the parenthesis and X are forcing the script to run as a shell script
0
 
ahoffmannCommented:
The paranthesis open a sub-shell in which sh will be started and read its commands from /usr/bin/raid.
The parantheses are not necessary, usually, but enshure that all output, STDOUT and STDERR, are captured.
In root's mail you'll see what realy was executed by /bin/sh.
You may remove the -x option now (after checking root's mail), and also the pipe to /bin/mail.
If it then still works, it's either a problem with your bash, or your cron does not allow to have bash scripts (which is strange too).

*you* asked why I don't like bash ;-)
0
 
duficyAuthor Commented:
Thank you for the solution and explanation.
0
 
duficyAuthor Commented:
Thank you for the solution and explanation.
0
 
duficyAuthor Commented:
Thank you for the solution and explanation.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 8
  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now