Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

script runs from prompt but not in crontab

Posted on 2002-04-03
17
Medium Priority
?
356 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
ID: 6916726
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
ID: 6917506
> .. 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
ID: 6918284
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
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
LVL 40

Expert Comment

by:jlevie
ID: 6918319
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
ID: 6920269
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
ID: 6920693
>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
ID: 6920964
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
ID: 6921323
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
 

Author Comment

by:duficy
ID: 6921343
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
ID: 6921705
> .. 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
ID: 6921745
>>
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
ID: 6921783
# 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
ID: 6921827
>>
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 400 total points
ID: 6921852
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
ID: 6921861
Thank you for the solution and explanation.
0
 

Author Comment

by:duficy
ID: 6921864
Thank you for the solution and explanation.
0
 

Author Comment

by:duficy
ID: 6921865
Thank you for the solution and explanation.
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.:
Suggested Courses
Course of the Month9 days, 10 hours left to enroll

877 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