LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 11-20-2007, 12:18 PM   #1
senthilvael
Member
 
Registered: Nov 2007
Posts: 73

Rep: Reputation: 17
Question Problem in running a script in CRON


I wrote a script for server monitoring. Its working fine. Now i have to make to run for each 30 min. I tried cron by following steps.

1.crontab -e

2.*/30 * * * * /shellscript/finalnew.sh

3.save and exit by Esc+:wq+enter

4.i saw this message "crontab: installing new crontab"

5.I checked by crontab -l


It too showed all. But instead of running that script, The cron sends a mail as follows

From root@qmail.com Tue Nov 20 22:28:06 2007
Date: Tue, 20 Nov 2007 22:28:01 +0530
From: root@qmail.com (Cron Daemon)
To: root@qmail.com
Subject: Cron <root@qmail> /shellscript/finalnew.sh
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
X-Cron-Env: <USER=root>

/bin/sh: /shellscript/finalnew.sh: No such file or directory


I know this is very very worst fault by me. But please bear it n help me.....
 
Old 11-20-2007, 12:28 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390
Hi,

Cron tells you that it cannot find /shellscript/finalnew.sh

Are you sure this script is called finalnew.sh (uppercase/lowercase )and can be found in /shellscript ?
 
Old 11-20-2007, 12:30 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
First of all the crontab entry is incorrect, the format for the
time is invalid. Look at the man-page.


And does the script ind that path exist, is it readable by the
user that owns the crontab?


Cheers,
Tink
 
Old 11-20-2007, 12:50 PM   #4
senthilvael
Member
 
Registered: Nov 2007
Posts: 73

Original Poster
Rep: Reputation: 17
Blender mistake was my mine.. I told the path wrongly to cron. But time entry is correct. I saw that from http://www.reallylinux.com/docs/basiccron.shtml

But only one more error is coming. The script is not running. Its sending mail only. its telling that
"TERM environment variable not set."
 
Old 11-20-2007, 01:03 PM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390
Hi,

If you run a script from cron the environment for the user running that script is not set .

You can let the script check if it is run from a terminal or not. If not, the environment is parsed. Something like this:

# Needed due to crontab behavior: parse user profile if needed
[[ "`/usr/bin/tty`" == "not a tty" ]] && . ~/.profile


Check if ~/.profile is the file to parse. It could be ~/.bash_profile (or alike).

Hope this helps.
 
Old 11-20-2007, 01:13 PM   #6
senthilvael
Member
 
Registered: Nov 2007
Posts: 73

Original Poster
Rep: Reputation: 17
Sorry i cant get u. Can u please tell what exactly i have to do now....
 
Old 11-20-2007, 01:40 PM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390
Hi,

If you run a script from a terminal/xterm it (the script) can access certain environment variables (like TERM, the variable you mentioned in post #4). If you run a script from cron, there is no environment (TERM isn't present, to name just one).

Try these commands from an xterm:

echo $TERM

and

env

All the output is part of a users environment. Almost all of it is not present when you run a script from cron.

If you put the 2 lines I posted in the beginning of your finalnew.sh script (after the hash-bang [#!/bin/bash]), the environment will be parsed if it is run from cron.

[[ "`/usr/bin/tty`" == "not a tty" ]] This part checks if the script is run from a terminal. If it is true (&&) the environment will be parsed (. ~/.profile).

You need to check if you use ~/.profile or ~/.bash_profile. Use the one you have.

Something like this:
Code:
#!/bin/bash
#
# comment explaining what finalnew.sh does
#

# Needed due to crontab behavior: parse user profile if needed
[[ "`/usr/bin/tty`" == "not a tty" ]] && . ~/.bash_profile

<rest of your code>

exit 0
 
Old 11-20-2007, 03:39 PM   #8
senthilvael
Member
 
Registered: Nov 2007
Posts: 73

Original Poster
Rep: Reputation: 17
I started my script like this
#!/bin/bash
# Needed due to crontab behavior: parse user profile if needed
[[ "`/usr/bin/tty`" == "not a tty" ]] && . ~/.bash_profile

TERM=linux
export TERM
.
.
.
.
rest of my script
.
.
.
exit 0

The cron is executing the script in proper intervals but it mails the result of the script to root.(i am executing as root it self)
 
Old 11-20-2007, 04:09 PM   #9
senthilvael
Member
 
Registered: Nov 2007
Posts: 73

Original Poster
Rep: Reputation: 17
I thought i used several echo commands in that script to check in terminal. But while running from cron, it sends the output as mail. So i commented all echo. But cron is throwing empty mails to root.

Somebody please help me.

Last edited by senthilvael; 11-20-2007 at 04:12 PM.
 
Old 11-20-2007, 05:36 PM   #10
senthilvael
Member
 
Registered: Nov 2007
Posts: 73

Original Poster
Rep: Reputation: 17
Angry

Finally i ran env command n copied all of them in the beginning of my script. Still it throwing empty mails to root whenever cron runs the script.

#!/bin/bash

[[ "`/usr/bin/tty`" == "not a tty" ]] && . ~/.bash_profile
HOSTNAME=qmail.com
TERM=xterm
SHELL=/bin/bash
SSH_TTY=/dev/pts/1
USER=root
KDEDIR=/usr
MAIL=/var/spool/mail/root
PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/ bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin
INPUTRC=/etc/inputrc
PWD=/root
LANG=en_US.UTF-8
SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass
SHLVL=1
HOME=/root
LOGNAME=root
SSH_CONNECTION=::ffff:192.168.0.218 4676 ::ffff:192.168.0.222 22
LESSOPEN=|/usr/bin/lesspipe.sh %s
G_BROKEN_FILENAMES=1
_=/bin/env

TERM=linux
export TERM
clear


touch mailcontent.txt
flag1=0
flag=0 #initialze a flag

sleep 1

if [ "$(nmap -P0 -p25 1.2.3.4 | grep open)" ] #port check
then
flag1=0 #dummy
else
flag=1 #set flag
echo SMTP seems not open in 1.2.3.4, Please check \n >> mailcontent.txt #update error file
fi

sleep 1

if [ "$(nmap -P0 -p110 1.2.3.4 | grep open)" ]
then
flag1=0
else
echo POP seems not open in 1.2.3.4, Please check \n >> mailcontent.txt
flag=1
fi
.
.
.
.
like this many servers
.
.
.
.
.
.

if [ $flag -eq 1 ] ;
then
mail -s "First level warning" root@qmail.com < mailcontent.txt
else
flag1=0
fi

rm mailcontent.txt





This is my script. Please tell me why its throwing empty mails to root.....Please i am struggling for a long time.............

Last edited by senthilvael; 11-20-2007 at 05:38 PM.
 
Old 11-20-2007, 07:38 PM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
A cron job does NOT have an interactive terminal attached, it's a detached background process, therefore the clear cmd is pointless .... (hint)
 
Old 11-21-2007, 02:23 PM   #12
senthilvael
Member
 
Registered: Nov 2007
Posts: 73

Original Poster
Rep: Reputation: 17
Lightbulb

Thank u very much. Now my script is running. The lesson is we must not use any screen based command like echo, clear while writing a script for cron..... Thank u all again.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
cron NOT running php script ??!! hendrixx Linux - Software 2 11-15-2006 08:18 AM
script not running as a cron job sanjith11 Programming 5 11-23-2004 09:42 AM
cron error when running script? meeble Linux - Software 3 09-09-2004 06:00 PM
running a 'dcop' script from 'cron' dsunger Linux - Software 0 05-10-2004 02:17 AM
script not running correctly off cron wedgeworth Linux - Software 3 10-14-2003 09:30 AM

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

All times are GMT -5. The time now is 12:26 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration