Solved

script runs from prompt but not in crontab

Posted on 2002-04-03
17
341 Views
Last Modified: 2013-12-16
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
Comment
Question by:duficy
  • 8
  • 6
  • 3
17 Comments
 
LVL 40

Expert Comment

by:jlevie
Comment Utility
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
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
> .. 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
 

Author Comment

by:duficy
Comment Utility
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
 
LVL 40

Expert Comment

by:jlevie
Comment Utility
Okay... How are you setting up the job, via "crontab -e"? And what does the crontab look like?
0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
IMHO bash is not the choice for root, probably not the problem but worth a try: replace /bin/bash by /bin/sh
0
 

Author Comment

by:duficy
Comment Utility
>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
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
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
 
LVL 40

Expert Comment

by:jlevie
Comment Utility
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
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 

Author Comment

by:duficy
Comment Utility
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
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
> .. 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
 

Author Comment

by:duficy
Comment Utility
>>
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
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
# 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
 

Author Comment

by:duficy
Comment Utility
>>
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
 
LVL 51

Accepted Solution

by:
ahoffmann earned 100 total points
Comment Utility
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
 

Author Comment

by:duficy
Comment Utility
Thank you for the solution and explanation.
0
 

Author Comment

by:duficy
Comment Utility
Thank you for the solution and explanation.
0
 

Author Comment

by:duficy
Comment Utility
Thank you for the solution and explanation.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now