Script runs manually but not correctly from crontab


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script runs manually but not correctly from crontab
# 1  
Old 02-29-2012
Script runs manually but not correctly from crontab

Hello all,
I'm new here and have a question if you don't mind helping me. I have a script that will work if I kick if off manually but not from Cron. My cron entry is this:
Code:
05,20,35,50 * * * * /scripts/status.sh > /dev/null 2>&1

The first script (works fine) is this:
Code:
#!/bin/sh
#
# ****************************************************************************************
# Purpose: Script to check the status of the following:
#       Disk usage - Call mon_fs.sh (this is dependent on mon_fs.dat 
#       Memory Usage - memorywatcher.sh
#       MWTM Server process - ServerUporDown.sh (this is dependent on /NOT.txt file 
# Notifies via e-mail.
# Usage: Execute from crontab every 15 minutes.
# Outputs: None - if one of the child scripts detects and error it will email the group 
# ****************************************************************************************
# The directory this script resides in
ADMINDIR=/scripts
# remove old status.log file
rm /scripts/status.log
# Fist 2 scripts to monitor disks and memory
/scripts/mon_fs.sh
# section to run the server status command
/opt/CSCOsgm/bin/mwtm status >> /scripts/status.log
# script to determine if any of the server process are down.
/scripts/ServerUporDown.sh

At the end of that script it call another one (the broken one) called ServerUporDown.sh. This is the one that is not working.
Code:
#!/bin/ksh
# ****************************************************
# Porpose: Montior Status of server components
# Notifies on trigger via eamil
# Usage: Execute from Crontab every 15 miniuites
# Outputs: email
# ***************************************************
#
# The directory this script resides in
ADMINDIR=/scripts
#
# The next variable can be set for multiple addresses
# (i.e. jsmith@yahoo.com,jsmith@hotmail.com)
MAILADD=abc@mail.com
# Define the hostname of the server
SRVNM=`uname -n`
# Remove old file 
rm /scripts/NOT.txt
# location of error messages
more /scripts/status.log | grep NOT >> /scripts/NOT.txt
# Logic to check if anything was written to the NOT file
# and email group if error was found
if [ ! -s "/scripts/NOT.txt" ]
then
echo "No error found."
else
echo "Sending eamil"
mailx -s $SRVNM" Server down - Action required" $MAILADD < NOT.txt
fi
exit 0

I'm sure it something simple... everything about it work fine but won't email that result when there is a entry into the NOT.txt file. It updates the status.log file and the NOT.txt file according the time stamps as it should.

Thoughts?

Last edited by methyl; 02-29-2012 at 08:44 AM..
# 2  
Old 02-29-2012
Code:
mailx -s $SRVNM" Server down - Action required" $MAILADD < /scripts/NOT.txt

# 3  
Old 02-29-2012
methyl - Thanks... staring at it too long and being up for 20+ hours... need sleep.. should have seen that. After making that change... all still the same.... everything works up to the point of email me
# 4  
Old 02-29-2012
The untrapped output from the script should be in the unix mail file for the owner of the cron.
Check whether the "Sending eamil" (sic) message appears in that mailbox and whether there are any other error messages.
You might want to redirect the cron output to a logfile and not to /dev/null while you are testing.


I'd certainly move this quote to encompass the whole subject line but whether it causes trouble depends on the value of $SRVNM.
Code:
mailx -s "$SRVNM Server down - Action required" $MAILADD < /scripts/NOT.txt

# 5  
Old 02-29-2012
Quote:
more /scripts/status.log | grep NOT >> /scripts/NOT.txt
This line is dodgy but might work.
Would be much better without the pointless "more" as:
Code:
grep "NOT" /scripts/status.log  >> /scripts/NOT.txt

# 6  
Old 02-29-2012
I'd also replace
Code:
more /scripts/status.log | grep NOT >> /scripts/NOT.txt

with
Code:
grep 'NOT' /scripts/status.log >> /scripts/NOT.txt

EDIT: methyl was faster...
# 7  
Old 02-29-2012
After you could try this alternative (you have now your /scripts/NOT.txt using methyl's or cero's suggestion):
Code:
\tail -1  /scripts/NOT.txt | $ux2dos | \mailx -s "$SRVNM Server down - Action required" $MAILADD

$ux2dos is for path and name of your unix2dos program (I set it as variable because many scripts I wrote run on different platforms...)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Script only runs first time through crontab

Hello, I am trying to run a script through crontab and it runs the first time and then it does not run. I tried to run a simple script (as shown below) and I see the same issue. #!/bin/ksh clear echo "Good Morning, World." > /tmp/test123 Crontab Entry: 30 09 * * *... (9 Replies)
Discussion started by: hasn318
9 Replies

2. Shell Programming and Scripting

Job runs manually, doesn't work in crontab

I have a script (/home/admin/run_bkup.sh) that I can run manually to kick off an executable job. I want to run it in crontab, but it doesn't work. Here's the script: shell=/bin/bash today=$(date +"%m-%d-%y") /opt/CPsuite-R77/fw1/bin/upgrade_tools/upgrade_export mgt-svr-bkup-$today << EOF y... (18 Replies)
Discussion started by: df08388
18 Replies

3. Shell Programming and Scripting

Script runs manually, but not from cron

Hi, I "borrowed" a script I found online, to start a SAP router application on a Solaris 11 (SPARC) server. The script runs fine when calling it manually, but when I schedule it to run from cron, it doesn't. I don't see any warning or failure messages anywhere, just nothing happens. ... (11 Replies)
Discussion started by: bredman
11 Replies

4. UNIX for Dummies Questions & Answers

Script runs manually but not from crontab in UNIX

Hi Guys, I am executing the script called Delet.sh manually it is successfully completing the task but it is failing to run vi cron tab, I tried to pass PATH & .profile before execution but no luck, Any suggestions? Script below #!/usr/bin/ksh #set -x # Purpose : Delete folders file from... (9 Replies)
Discussion started by: Anilsa77
9 Replies

5. Shell Programming and Scripting

Script runs good manually but failing via crontab

Hello Gurus, I have written small script which will start the given service if its stop .Its running fine when manually executed but its unable to run from crontab. #!/bin/bash SERVICENAME=rsyslog service $SERVICENAME status > /dev/null SYSLOGSTATUS=`echo $?` COUNT=0 THRESHOLD=3 if ... (4 Replies)
Discussion started by: kapil514
4 Replies

6. Shell Programming and Scripting

Part of the Shell script is not running via crontab, runs fine manually

Hello Team, As a part of my job we have made a script to automate a service to restart frequently. Script having two functions when executing it's should find the existing service and kill it, then start the same service . Verified the script it's working fine when executing... (18 Replies)
Discussion started by: gowthamakanthan
18 Replies

7. Shell Programming and Scripting

Script runs fine manually but not in crontab

Hello Guys, I have scratched my head alot on this but couldn't find clue what's wrong. Can you please help me with this? My problem is as following. 1) When I manually execute following script it runs successfully with below output. bash-3.00# more smssend #!/bin/bash echo -e "<Request... (16 Replies)
Discussion started by: umarsatti
16 Replies

8. Shell Programming and Scripting

Shell Script runs good manually but not through Cron tab

Hello Every one, I have a shell script which is running fine manually, but its giving me hard time when running tru cron job. :wall:. Am using #!/usr/bin/ksh >echo $SHELL /usr/bin/ksh Cron Job is as below, it execues but dosent do what i want it to do. 47 15 * * *... (1 Reply)
Discussion started by: naren.chowdhary
1 Replies

9. Shell Programming and Scripting

Script runs manually but not correctly from crontab

Hi all I have this inside a shell script (bash): cd DIRECTORY find . -maxdepth 1 | sed 's#./##' | /usr/bin/xargs -I '{}' chown -Rv '{}' /DIRECTORY/'{}' All the directories in this location are named after usernames, so it simply sets the owner to that of the username of the folder. It... (5 Replies)
Discussion started by: fakesy
5 Replies

10. Shell Programming and Scripting

CRON shell script only runs correctly on command line

Hi, I'm new to these forums, and I'm hoping that someone can solve this problem... To make things short: I have DD-wrt set up on a router. I'm trying to run a script in CRON that fetches the daily password from my database using SSH. CRON is set like so(in web interface): * * * *... (4 Replies)
Discussion started by: louieaw
4 Replies
Login or Register to Ask a Question