LinuxQuestions.org
Help answer threads with 0 replies.
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 02-22-2013, 06:10 AM   #1
Mainer44
LQ Newbie
 
Registered: Feb 2013
Posts: 7

Rep: Reputation: Disabled
Trouble with a simple script


I am having trouble running a simple script. The purpose of the script is to back one file up into another on a regular basis. For the backup I am using rsync and cron for the repeatability. I'm running Fedora on a virtualbox, if that matters.

The file I'm backing up is /home/username/regfiles.
The file I'm backing up into is /home/username/backup.

The script is:
#!/bin/bash

rsync -av --delete /home/username/regfiles/ /home/username/backup/

This script is located in /home/username/bin/backup.sh

I then used chmod +x to source the script.

I was able run the script from my home directory using ./bin/backup.sh

I tried the cron by making a crontab,
10 * * * * ./bin/backup.sh

I waited until 11 minutes after the hour and used ls backup to check what was in the backup file and it was empty.

Thinking the path wasn't correct I edited the crontab to
15 * * * * /home/username/bin/backup.sh

Again I waited until 16 minutes past the hour and checked the backup file with ls and it was still empty.


I'm not quite sure where I'm going wrong, and any help would be greatly appreciated.
 
Old 02-22-2013, 06:13 AM   #2
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
what do the logs say happened?
 
Old 02-22-2013, 06:43 AM   #3
mandyapenguin
Member
 
Registered: Nov 2011
Location: India
Distribution: RedHat, Cent OS, Fedora, Debian, Ubuntu
Posts: 106

Rep: Reputation: Disabled
Restart/Reload the crond service once
Code:
service crond restart
or
service cron restart

Last edited by mandyapenguin; 02-22-2013 at 06:54 AM.
 
Old 02-22-2013, 06:54 AM   #4
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
As you can see, shebang (i.e. script interpreter) in your script is #!/bin/bash, so can you check which is your shell i.e. login shell of user username?

If shebang and login shell are different, then you will need to run script as:
Code:
~$ bash /home/username/bin/backup.sh
And crontab entry should be like:
Code:
15 * * * * "bash /home/username/bin/backup.sh"
 
Old 02-22-2013, 06:54 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Maybe changing the script to give the full path of rsync would solve it.

cron sets a limited PATH which may not include the directory containing rsync. You could find out what the PATH is when run from cron (and incidentally demonstrate that the script is actually being run by modifying the script to:
Code:
#!/bin/bash

echo $PATH > /tmp/my_script.log

rsync -av --delete /home/username/regfiles/ /home/username/backup/
 
Old 02-22-2013, 06:56 AM   #6
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
when you created the backup script did you have it create a log file? also have you viewed /var/logs/cron to see if it did in fact start?
 
Old 02-22-2013, 07:10 PM   #7
Mainer44
LQ Newbie
 
Registered: Feb 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
Thank you for all the suggestions. I tried them all and nothing worked.

I couldn't locate the var/log files or any other logs that would tell me what's going wrong.

I'm stumped!
 
Old 02-22-2013, 07:26 PM   #8
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Quote:
"...Thank you for all the suggestions. I tried them all and nothing worked."
Which distro? Crontab entries creation is different on different OS, so once mention your OS.
 
Old 02-22-2013, 08:26 PM   #9
Mainer44
LQ Newbie
 
Registered: Feb 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
My computer is a windows 7 machine running virtualbox on which I am using Fedora 17.
 
Old 02-22-2013, 09:06 PM   #10
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Once create crontab entries like:
Code:
~$ crontab -e
*/10 * * * * "/home/username/bin/backup.sh"
OR
Code:
~$ crontab -e
*/10 * * * * "bash /home/username/bin/backup.sh"

Last edited by shivaa; 02-22-2013 at 09:08 PM.
 
Old 02-23-2013, 12:23 AM   #11
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Quote:
Originally Posted by shivaa View Post
Once create crontab entries like:
Code:
~$ crontab -e
*/10 * * * * "/home/username/bin/backup.sh"
OR
Code:
~$ crontab -e
*/10 * * * * "bash /home/username/bin/backup.sh"
why the quotes around the command? ive set up hundreds of cron jobs over the years and never had to use "" around any of the commands even for custom scripts.

not nit picking, just trying to learn.
 
Old 02-23-2013, 01:05 AM   #12
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
@lleb:
It's just a matter of choice, quoting doesn't make any difference.
 
Old 02-23-2013, 05:40 PM   #13
Mainer44
LQ Newbie
 
Registered: Feb 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
Thanks for all the suggestions. I figured out what I needed to do.

I made a directory /home/username/cron and put a file named mycron in this directory.

I put my cron table in the mycron file.

then I went to my home directory and used the command crontab ./cron/mycron

When the time came for the backup I checked the backup file and it was written to.

Now I understand cron better. Thanks
 
Old 02-23-2013, 07:43 PM   #14
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Quote:
Originally Posted by Mainer44 View Post
Thanks for all the suggestions. I figured out what I needed to do.

I made a directory /home/username/cron and put a file named mycron in this directory.

I put my cron table in the mycron file.

then I went to my home directory and used the command crontab ./cron/mycron

When the time came for the backup I checked the backup file and it was written to.

Now I understand cron better. Thanks
Happy to hear this.

But what do you think the pb was? How are your crontab entries now look like?
 
Old 02-24-2013, 05:05 AM   #15
Mainer44
LQ Newbie
 
Registered: Feb 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
I'm not totally sure what happened, but what I do know is that I can put the cron table in my home directory and I can tell cron where to find it with the crontab command and relative path. I still don't understand why using the crontab -e and absolute path didn't work. Is there a standard location to put scripts? Mine was in my home directory as was my crontab as well as both files involved in the backup. How I figured this out was using the book "Unix Power Tools" by Shelley Powers. On page 494 there is a description that I followed.
 
  


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
Trouble With FOR Loop In Simple Bash Script sudleyplace Linux - Newbie 7 03-12-2008 04:55 AM
Need help getting started simple simple shell script dhonnoll78 Programming 6 12-17-2007 05:34 PM
Having trouble with a simple script Arcturuss Linux - Newbie 1 12-03-2007 01:10 AM
Iptables (with masq) troubleshooting, very simple script attached script and logs. xinu Linux - Networking 13 11-01-2007 04:19 AM
Simple C Shell script is not so simple elconde Programming 2 09-16-2001 11:53 PM

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

All times are GMT -5. The time now is 06:43 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