LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-17-2011, 07:02 AM   #1
Ankush Raj
LQ Newbie
 
Registered: Oct 2011
Location: Mumbai
Distribution: RedHat 5
Posts: 2

Rep: Reputation: Disabled
Shell Script Not Working with crontab


Hi,

I am new with shell script and I am facing problem with setting crontab for my script. Though my script works fine when I run it from command line.

I have these packages installed on my system :
anacron-2.3-45.el5
crontabs-1.10-8
vixie-cron-4.1-77.el5_4.1

Its RHEL-5 64 bit.

I followed these steps for setting crontab :
1) crontab -e
2) */2 * * * * /home/user/filename.sh
3) save and quit

This doesn't gives me desired output.

Script which I am using is as:

APPCHK=$( ps -ef | grep httpd | grep -v "grep httpd" | wc -l )
echo $APPCHK
pro_time=`date`
if [ $APPCHK -ne '0' ];
then
echo "httpd running" > /Crnfile/Crn_run.txt
else
echo "httpd not running" >> /Crnfile/Crn_stop.txt
fi

This script produces output as desired when run from command line. But using crontab it only gives output of first conditon mentioned in " if - else " clause.

Please suggest, what measures should I take to rectify it.

Regards,
Ankush
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 10-17-2011, 07:09 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
I would change the redirection even in the first clause to append the output (not overwrite)
Code:
echo "httpd running" >> /Crnfile/Crn_run.txt
 
Old 10-17-2011, 11:08 AM   #3
SecretCode
Member
 
Registered: Apr 2011
Location: UK
Distribution: Kubuntu 11.10
Posts: 562

Rep: Reputation: 102Reputation: 102
A suggestion: if the first line is testing the number of processes matching 'httpd' you could simplify it to
Code:
APPCHK=$( pgrep httpd | wc -l )
As to why you are getting different behaviour when run in cron, two possibilities:
- You might be running under a different shell. You could fix it by adding
Code:
#!/bin/bash
as the first line.

- There might be different environment variables set.

I would add some tracing (to a file) to make sure that the intermediate commands in the first line are returning what you expect.
 
2 members found this post helpful.
Old 10-17-2011, 11:10 AM   #4
SecretCode
Member
 
Registered: Apr 2011
Location: UK
Distribution: Kubuntu 11.10
Posts: 562

Rep: Reputation: 102Reputation: 102
Just to be sure ... what is the output you expect? Is it outputting "httd running" even when it is not?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script not Working through Crontab in Ubuntu 10.04 vijith.pa@gmail.com Linux - Newbie 16 11-03-2010 03:31 PM
Shell Script not Running from Crontab venkatramanj SUSE / openSUSE 3 01-13-2010 05:15 AM
Runs Multiple shell script inside a main script using crontab srimal Linux - Newbie 4 10-22-2009 06:19 PM
Shell script not working through crontab paragkalra *BSD 1 04-15-2009 12:36 PM
how to run crontab in shell script panselva Linux - General 3 05-08-2008 08:33 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration