LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Trouble with a simple script (https://www.linuxquestions.org/questions/linux-newbie-8/trouble-with-a-simple-script-4175451267/)

Mainer44 02-22-2013 06:10 AM

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.

Habitual 02-22-2013 06:13 AM

what do the logs say happened?

mandyapenguin 02-22-2013 06:43 AM

Restart/Reload the crond service once
Code:

service crond restart
or
service cron restart


shivaa 02-22-2013 06:54 AM

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"

catkin 02-22-2013 06:54 AM

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/


lleb 02-22-2013 06:56 AM

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?

Mainer44 02-22-2013 07:10 PM

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!

shivaa 02-22-2013 07:26 PM

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.

Mainer44 02-22-2013 08:26 PM

My computer is a windows 7 machine running virtualbox on which I am using Fedora 17.

shivaa 02-22-2013 09:06 PM

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"


lleb 02-23-2013 12:23 AM

Quote:

Originally Posted by shivaa (Post 4897903)
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.

shivaa 02-23-2013 01:05 AM

@lleb:
It's just a matter of choice, quoting doesn't make any difference.

Mainer44 02-23-2013 05:40 PM

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

shivaa 02-23-2013 07:43 PM

Quote:

Originally Posted by Mainer44 (Post 4898372)
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?

Mainer44 02-24-2013 05:05 AM

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.


All times are GMT -5. The time now is 09:32 PM.