LinuxQuestions.org
Review your favorite Linux distribution.
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 02-23-2009, 11:44 AM   #1
elthox
Member
 
Registered: Oct 2006
Posts: 41

Rep: Reputation: 15
Sed not working from Cron


Hi,

I have a script with a sed statement included. When I execute it manually it produces the normal output, but doesnt happen when it is executed by cron. It shows the below output;

Your "cron" job on 141-1
/export/home/sigvalue/check_bind.sh > /dev/null

produced the following output:

sed: command garbled: 2519, 3708 p

and the code is like this;

sed -n "$STSIZE,$CUR_SIZE p" \
$LOGDIR/current.log \
> $TEMPDIR/bind.error.diff
 
Old 02-23-2009, 02:06 PM   #2
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
And are all those variables visible to cron/inside the script? We're
not clairvoyant, you see ... :}
 
Old 02-23-2009, 02:14 PM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Hello elthox

Chances are some of the variables you are relying on are not set in the environment that the script runs in as a cron job.

You can check by putting
Code:
set > ~/trash
in the script.

If what you need isn't there because the login process has not been run (it isn't, by cron) you should be able to fix it by putting this on the first line of your script:
Code:
#!/bin/bash -l
(that's a letter l)

Best

Charles
 
Old 02-24-2009, 03:15 AM   #4
elthox
Member
 
Registered: Oct 2006
Posts: 41

Original Poster
Rep: Reputation: 15
Thanks for your reply.

I tried adding -l option but it was not recognised as below;

Your "cron" job on 141-1
/export/home/sigvalue/check_bind.sh > /dev/null

produced the following output:

/bin/bash: -l: unrecognized option
Usage: /bin/bash [GNU long option] [option] ...
/bin/bash [GNU long option] [option] script-file ...
GNU long options:
--debug
--dump-po-strings
--dump-strings
--help
--init-file
--login
--noediting
--noprofile
--norc
--posix
--rcfile
--restricted
--verbose
--version
--wordexp
Shell options:
-irsD or -c command (invocation only)
-abefhkmnptuvxBCHP or -o option


I`m also pasting my script where vaiables take place.


#!/bin/bash -l


TEMPDIR=/export/home/sigvalue/
LOGDIR=/export/home/sigvalue/logs/SMPP_SERVER-A/
CUR_SIZE=`cat $LOGDIR/current.log|wc -l`
PREV_SIZE=`cat $TEMPDIR/current.size`

if [ $CUR_SIZE -eq $PREV_SIZE ]
then
echo "Nothing to do..." > $TEMPDIR/bind.error.diff
exit
fi

if [ $CUR_SIZE -gt $PREV_SIZE ]
then
let STSIZE=$PREV_SIZE+1

sed -n "$STSIZE,$CUR_SIZE p" \
$LOGDIR/current.log \
> $TEMPDIR/bind.error.diff
cat $TEMPDIR/bind.error.diff|egrep "UNBIND|failed|Client Is not connected" > $TEMPDIR/error
if [ -s $TEMPDIR/error ]
then
wget --post-data="serverName=SMPP-A&msgBody=`cat $TEMPDIR/error`" http://10.1.21.236:8585/pns.asmx/SendEmail
rm $TEMPDIR/error
fi
fi
echo $CUR_SIZE > $TEMPDIR/current.size


Thanks guys
 
Old 02-24-2009, 04:18 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by elthox View Post
Thanks for your reply.

I tried adding -l option but it was not recognised as below;

[snip]

I`m also pasting my script where vaiables take place.

[snip]
Hello elthox

Try changing the first line of the script from #!/bin/bash -l to #!/bin/bash --login

If that doesn't help, let's try and get some more information. Just before the problem sed command add these lines
Code:
set > ~/trash.$$
echo DEBUG: sed -n "$STSIZE,$CUR_SIZE p" >> ~/trash.$$
$$ is the process ID; it will give the two ~/trash.* files different names. After running the script manually and by cron, use diff to compare the two ~/trash.* files, including any difference in the sed command on the last line.

You might also try changing
Code:
sed -n "$STSIZE,$CUR_SIZE p" \
$LOGDIR/current.log \
> $TEMPDIR/bind.error.diff
to
Code:
sed -n "$STSIZE,$CUR_SIZE p" $LOGDIR/current.log > $TEMPDIR/bind.error.diff
It shouldn't make any difference but simplifying can help identify the unexpected.

Best

Charles
 
Old 02-24-2009, 05:17 AM   #6
elthox
Member
 
Registered: Oct 2006
Posts: 41

Original Poster
Rep: Reputation: 15
Thankyou guys,

It was much simpler, just adding . $HOME/.profile at the beginning

Now it works


Thanks
 
Old 02-24-2009, 06:44 AM   #7
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
You might want to check the line "CUR_SIZE=`cat $LOGDIR/current.log|wc -l`". It might be more proper you used:
`wc -l networking-concepts-HOWTO.letter.ps | cut -f1 -d' '` instead to cut out the filename and leave just the integer.

If the script you tested doesn't run, the $HOME variable might not be set.

Use delimiters in the sed command:
Code:
sed -n "/$STSIZE,$CUR_SIZE/p" $LOGDIR/current.log > $TEMPDIR/bind.error.diff
As you probably know, the sed command prints a range of lines between $STSIZE and $CUR_SIZE.

Make sure that the first time you run the script, that the contents of '$TMPDIR/current.size' makes sense.

How do you deal with log rotation on the system? The contents of the log will change and the range values won't be valid.
 
Old 02-24-2009, 07:58 AM   #8
elthox
Member
 
Registered: Oct 2006
Posts: 41

Original Poster
Rep: Reputation: 15
Hi,


Regarding log rotation...is it logical to make an if control such as

previoussize=1000
currentsize=3 (after the rotation new current.log is being written)

if [ $previoussize -gt $currentsize ]
then
...do nothing
fi

Is it logical or it may be another way?

Thanks
 
Old 02-24-2009, 12:06 PM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by elthox View Post
Thankyou guys,

It was much simpler, just adding . $HOME/.profile at the beginning

Now it works


Thanks
Glad you got it working

If you are going to develop and test scripts while logged on (the alternative is difficult!) it's safer to use #!/bin/bash --login because that sets up, as far as practicable, an identical environment. Several files are used to set up your environment when you log on. In this case ~/.profile was the significant one; it may not always be.
 
  


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
adding a perl script to cron.daily / cron.d to setup a cron job CrontabNewBIE Linux - Software 6 01-14-2008 09:16 AM
SED - minor changes work - Larger doesn't (working and non working code included) Nimoy Programming 17 09-22-2007 05:34 PM
cron.daily, cron.weekly not working mwhite74 Linux - Server 2 06-07-2007 10:51 AM
sed not working... culin Programming 26 02-07-2007 05:58 PM
cron not working from crontab nor form /etc/cron/cron.d. What did SuSE change? JZL240I-U SUSE / openSUSE 11 01-04-2007 02:57 AM


All times are GMT -5. The time now is 10:41 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration