LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices

Reply
 
Search this Thread
Old 08-11-2008, 12:21 AM   #1
mwi.user
LQ Newbie
 
Registered: Aug 2008
Posts: 6

Rep: Reputation: 0
Help requires for my first cron/shell script


Hi All,

I am very new to server task and that is why I dont know some basic concepts. This time I am facing problem with one of my cron script. Our puspose for creating this script is to "rsync" our domain's filesystem and keep it somewhere on server. After that creating a success log and then email it to us.
But things are not going the way we thought so here I need your help.
This is my shell script
************************************************************************************
#!/bin/bash

errfile=/BACKUPS/yyy/logs/yyy_`date +%d"-"%b"-"%Y`.log
rsync -azv /x/xx/xxx/xxxx.xxx/httpdocs/ /BACKUPS/xxx/xxxx.xxx > $errfile

subject="`date +%d"-"%b"-"%Y`-xFER_Log_abc"
cat "$errfile"|mail -s $subject abcd@gmail.com
************************************************************************************

I saved it as test1.sh
My command to run this script is "bash test1.sh" and after pressing enter it gives me following errors
***********************************************************************************
: command not found
: command not found
cat: /BACKUPS/nnn/logs/yyy_11-Aug-2008.log: No such file or directory
Null message body; hope that's ok
***********************************************************************************

Another problem is with the log's name. The log name I can see in my log's directory is "yyy_11-Aug-2008.log??"

I think I am doing or following some wrong format of "date" but I am not sure.

Kindly help me out to resolve this probelm.

Thanks in advance.
Amit

Last edited by mwi.user; 08-11-2008 at 04:27 AM.
 
Old 08-11-2008, 04:07 AM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,289

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
In cron, there is a very minimal env provided, so always specify the full path to all cmds eg
/usr/bin/rsync, /bin/date etc
Also, no need to break this line
rsync -azv /x/xx/xxx/xxxx.xxx/httpdocs/ /BACKUPS/xxx/xxxx.xxx > $errfil\
e
just use
rsync -azv /x/xx/xxx/xxxx.xxx/httpdocs/ /BACKUPS/xxx/xxxx.xxx > $errfile

Your errfile name is inconsistent; orig is BACKUPS/yyy... then you cat BACKUPS/nnn.
In any case, just use $errfile, since you've already assigned it.

don't keep generating the date, do it once at the start and save to a var eg $today, and use that instead.

If you really need to debug, use
set -xv
as line 2 of your script.
 
Old 08-11-2008, 04:32 AM   #3
mwi.user
LQ Newbie
 
Registered: Aug 2008
Posts: 6

Original Poster
Rep: Reputation: 0
Sorry Chrism01, I have updated my post now and I have corrected it. Kindly let me know how can I debug my script. What should be the steps to move ahead from my current problem.

Thanks a lot
Amit
 
Old 08-11-2008, 04:39 AM   #4
billymayday
Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
I think you need to space

cat "$errfile"|mail -s $subject abcd@gmail.com

like

cat "$errfile" | mail -s $subject abcd@gmail.com

To identify where problems are occurring, add

echo "1"
echo "2"

and so on before each line you suspect may cause an issue, since the output isn't that clear on where the errors are occurring


Then post back, but don't update the original post, add a new one - it makes it easier to follow the thread.
 
Old 08-11-2008, 09:47 AM   #5
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
mwi.user: Your script has the wrong end of line characters. You've saved it using an editor that saves the file with DOS line endings. Use dos2unix to change the line endings to UNIX style line endings, or use vim with the command:

:set ff=unix
:wq


billymayday: The pipe symbol is a word separator character in the shell. Spaces are nice, but not required here:

Code:
$ foo="Hello World"
$ echo "$foo"|cat
Hello World
 
Old 08-12-2008, 01:08 AM   #6
mwi.user
LQ Newbie
 
Registered: Aug 2008
Posts: 6

Original Poster
Rep: Reputation: 0
Hello Mr. C., Thanks for replying but I am not able to understand you. I am using "emacs" editor and do not have any idea how I can resolve this problem.

Thanks in advance
Amit
 
Old 08-12-2008, 01:45 AM   #7
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
Use dos2unix then:

Code:
dos2unix test1.sh
and then retry:

Code:
/path/to/test1.sh
It doesn't matter that you use emacs for editing. You can still use vim for the simple translation if you don't have dos2unix. I don't recall the equivalent command in emacs - I haven't used it in many years.
 
Old 08-12-2008, 01:49 AM   #8
billymayday
Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
I had a play with this file, and although I can't fully replicate your error, I can get the latter part if the path to the logfile doesn't exist, so in this case, the path /BACKUPS/yyy/logs/

Are you sure this exists?

Edit - sorry Mr C - stuffed up my conversion DOS, you are spot on

There must be an emacs setting to switch between DOS and Unix type eol

Last edited by billymayday; 08-12-2008 at 01:52 AM.
 
Old 08-12-2008, 02:05 AM   #9
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
Perhaps this gives you clues:

http://www.fnal.gov/docs/products/em...38.html#SEC468
 
Old 08-12-2008, 06:55 AM   #10
mwi.user
LQ Newbie
 
Registered: Aug 2008
Posts: 6

Original Poster
Rep: Reputation: 0
Hello Mr. C.,
Your way of avoiding that problem worked for me but it is working partly. Now there is no "??" in log file's name. After "dos2unix test1.sh" and then "bash test1.sh" gave me an error "rsync error: received SIGUSR1 or SIGINT (code 20) at rsync.c(231)". Here is what I am not able to understand, If I manually rsync my code it is working and not showing any error but the same line if I run through script It would give me an error which I mentioned above.
 
Old 08-12-2008, 11:14 AM   #11
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
Show the output of the run with set -x enabled.

When code works via command line but not in a script, the things to consider are:
  • PATH differences
  • environment variable differences
  • controlling TTY differences
  • shell variable and other expansion of your command line

Since we can't see *exactly* what command is running, it is difficult to diagnose other than to search google for others experiencing the same error message.
 
  


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
shell script using /etc/cron.hourly to execute cron.php file? rioguia Programming 3 06-11-2008 08:09 AM
Shell script in cron.daily ;Where does the log go ? jeepescu Linux - General 2 12-03-2007 06:07 PM
Cron wont run my shell script shelb Linux - Desktop 4 05-27-2007 09:55 PM
run shell script on cron varunbihani Linux - Newbie 5 07-08-2005 01:50 AM
shell script fo run auto job in cron JolynnMarie LinuxQuestions.org Member Intro 0 04-28-2004 11:21 AM


All times are GMT -5. The time now is 10:25 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration