LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-04-2006, 01:23 AM   #1
tommytomato
Member
 
Registered: Nov 2003
Location: Narrogin Western Australia
Distribution: GUI Ubuntu 14.0.4 - Server Ubuntu 14.04.5 LTS
Posts: 966

Rep: Reputation: 32
Using cron


I'm following a back up idea on this link http://esm2.imt-mrs.fr/~staffelb/gui...art1/cron.html

Quote:
Putting Theory Into Practice

Here's a handy little automation exercise for you to try out. It will backup the contents of your home directory to a .tar.gz file each morning at 2.30a.m.

If you haven't already done so, enter:

su -c 'pico /etc/cron.allow'
...and add your username to this file to allow you to submit crontab files.

- 1 -

If you're not already in your home directory, enter:

cd
- 2 -

Enter:

pico backup
...and enter the following:

--------------------------------------------------------------------------------
rm backup.tar.gz
tar cfz backup.tar.gz .
--------------------------------------------------------------------------------
...then press Ctrl+o to save the file, and Ctrl+x to exit.

- 3 -

Now make the script executable by entering:

chmod +x backup
- 4 -

Enter:

crontab -e
- 5 -

Press i to enter Insert mode and enter the following:

--------------------------------------------------------------------------------
30 2 * * * ./backup
--------------------------------------------------------------------------------
...then press Esc to return to Command mode, and enter:

:wq
The only way I can get it to back a DIR of ours is to use the command line only ./backup

cron doesn't seem to be working

[root@localhost subs]# crontab -l
40 13 * * * ./usr/local/awstats/wwwroot/cgi-bin/./awstats.pl -update -config=www.rockinghamgateway.com
20 14 * * * ./srv/www/subs/backup
[root@localhost subs]#

I tried a few different ways by testing it by editing the crontab a few minutes forward to see if they are updateding or backing up.

hope that makes sence.

??? ??? ???

TT
 
Old 01-04-2006, 02:35 AM   #2
WindowBreaker
Member
 
Registered: Oct 2005
Distribution: Slackware
Posts: 228

Rep: Reputation: 40
tommytomato:
I believe the error is with your "tar" command.
Change from:
Code:
tar cfz backup.tar.gz .
To:
Code:
tar czf backup.tar.gz .
The backup file's name must directly follow the "f" option.

Give that a whirl and let me know if it worked for you.
 
Old 01-04-2006, 03:03 AM   #3
tommytomato
Member
 
Registered: Nov 2003
Location: Narrogin Western Australia
Distribution: GUI Ubuntu 14.0.4 - Server Ubuntu 14.04.5 LTS
Posts: 966

Original Poster
Rep: Reputation: 32
Nup nothing happing

10 * * * 0,1,2,3,4,5,6,7 /usr/local/awstats/tools/awstats_updateall.pl now >/dev/null 2>&1p
02 16 * * * ./srv/www/subs/backup

top one works and the 2nd one dont.

Code:
crontab -l
10 * * * 0,1,2,3,4,5,6,7 /usr/local/awstats/tools/awstats_updateall.pl now >/dev/null 2>&1p
02 16 * * * ./srv/www/subs/backup
file back up has perm 0755 and has this in side the file

Code:
rm backup.tar.gz
tar czf backup.tar.gz .
file was saved as backup, if I use the command line #./backup it works.

TT
 
Old 01-04-2006, 03:27 AM   #4
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
Look at cron's logs to see what's happening.

Furthermore, cron uses it's own environment variables. $PWD, the present working directory, is one of them.
So I'm not sure "./", which is synonymous to $PWD, is set up correctly. In other words, I doubt if cron
can actually find your command, since it may not be running out of the directory that you expect it to.

It would be a good idea to change "./..." into an absolute path, or use ~root/ for the home directory of the
user root.
A alternative is to add the command
cd (or cd to your root's home dir)
to the top of your backup script, just to make sure it's running in the right directory.
A 3rd option is to specify absolute paths in the "rm" and "tar" commands.

If "./backup" works, assuming you're inside your home directory, then why do you ask cron to run it as
"./srv/www/subs/backup" (assuming for a moment that cron runs the commands out of your home directory)?
Shouldn't it simply be "./backup" instead? In other words, pay attention to the paths!
If you're worried that your path is wrong, try adding "ls" to the cron command:
02 16 * * * ls -l ./srv/www/subs/backup

This should send you an e-mail containing the output of the ls -l command. If it doesn't, add "> a_new_file" at the
end to make "ls" write it's output to a regular file (so you can read it afterwards).
 
Old 01-04-2006, 03:30 AM   #5
WindowBreaker
Member
 
Registered: Oct 2005
Distribution: Slackware
Posts: 228

Rep: Reputation: 40
How about specifying the full paths:
Code:
rm /full/path/to/backup.tar.gz
tar czf /full/path/to/backup.tar.gz .
 
Old 01-04-2006, 04:31 AM   #6
tommytomato
Member
 
Registered: Nov 2003
Location: Narrogin Western Australia
Distribution: GUI Ubuntu 14.0.4 - Server Ubuntu 14.04.5 LTS
Posts: 966

Original Poster
Rep: Reputation: 32
I've done what you guys said, and still no go, must be missing someting

my cron is

10 * * * 0,1,2,3,4,5,6,7 /usr/local/awstats/tools/awstats_updateall.pl now >/dev/null 2>&1p
27 17 * * * ./srv/www/subs/backup

I added that path into the backup file under subs
rm /srv/www/subs/backup.tar.gz
tar czf /srv/www/subs/backup.tar.gz .

must be the way I do my crons even the top cron is not work, I thought it was, OS is tinysofa odin

TT

Last edited by tommytomato; 01-04-2006 at 04:33 AM.
 
Old 01-04-2006, 05:20 AM   #7
WindowBreaker
Member
 
Registered: Oct 2005
Distribution: Slackware
Posts: 228

Rep: Reputation: 40
Is crond running???
Try "pidof crond", or "ps aux | grep crond" to see.
If it's not, then fire it up and verify that at least one of your crons is running.
 
Old 01-04-2006, 05:21 AM   #8
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
First of all, be carefull about the paths.
/srv/www/subs/backup.tar.gz
is NOT the same as
./srv/www/subs/backup.tar.gz

Secondly, to check if cron works, follow the following steps:
-Check if the cron daemon (crond) is running.
This can be done via the "ps" command (eg ps -efl|grep -i crond) or via
/sbin/chkconfig --list | grep -i cron
Via /sbin/service cron status (or something like that) may also work.

Seems WindowBreaker beat me to this one... - lol

-Secondly, your user must be allowed to run cron jobs. In other words, your username must appear in
the file /etc/cron.allow. If cron.allow doesn't exist, then your username may not appear in /etc/cron.deny.
If cron.deny and cron.allow do not exist, then you must create the cron.allow file and put "root" as well as
your username inside it. Be sure to create it as root.

For more info, see:
man cron
man crontab
man 8 crontab
 
Old 01-04-2006, 10:24 AM   #9
tommytomato
Member
 
Registered: Nov 2003
Location: Narrogin Western Australia
Distribution: GUI Ubuntu 14.0.4 - Server Ubuntu 14.04.5 LTS
Posts: 966

Original Poster
Rep: Reputation: 32
ok guys thanks

I had to have a break so I went fishing, I'll double check every thing in the morning because I'm nacked.

I checked to see if cron is running here is what I get

Quote:
ps -efl|grep -i crond
1 S root 1965 1 0 76 0 - 1079 - 12:22 ? 00:00:00 crond
0 R root 3794 3759 0 79 0 - 1072 - 23:21 pts/0 00:00:00 grep -i crond
Also I dont have /etc/cron.allow in my system

Quote:
First of all, be carefull about the paths.
/srv/www/subs/backup.tar.gz
is NOT the same as
./srv/www/subs/backup.tar.gz
So which is the correct one, I have the top one /srv/www/subs/backup.tar.gz

TT
 
Old 01-04-2006, 11:23 AM   #10
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
By the looks of it, your cron daemon is running alright.

Do you have /etc/cron.deny? If so, make sure that your user isn't listed in that file.
If you don't, then "su" to root, create the file /etc/cron.allow and put your username in there.
Something like:
su #switches to root user, you'll need the password...
echo "your_username_goes_here" > /etc/cron.allow
exit #exits su
(the text after the '#' sign are just comments, they are not executed by your system)

As to which is the right path, that depends on the directory you're working in.
Paths starting in "/" are called absolute paths. They are subdirectories located under
the "/" (top-level) directory of your disk.
The path that starts with "./" is different. "./" refers to the current directory (use the "pwd" command to see what it is). So, ./srv/www/subs/backup.tar.gz is located somewhere under the current directory, which may or may not be the "/" directory.

You can actually use any path you want. You just need to decide:
-where (in which directory) you want to store the backup.tar.gz file
-where the files you need to backup are stored.
 
Old 01-04-2006, 11:34 AM   #11
trickykid
LQ Guru
 
Registered: Jan 2001
Posts: 24,149

Rep: Reputation: 270Reputation: 270Reputation: 270
Remove the . in the ./srv... should result in the script executing from cron. You only use a ./ in front of a command when your in a directory that has an executable that isn't in your present PATH. If your specifying the full path, you never need to put a . in the path name.
 
Old 01-04-2006, 12:28 PM   #12
morrolan
Member
 
Registered: Sep 2003
Location: Manchester UK
Posts: 264

Rep: Reputation: 30
If you ask me, the entire tutorial is written badly.

Surely "tar -zcf backup.tar.gz ." should be in a bash script? So therefore:
Code:
#!/bin/bash

# this file is an automated backup script, backup.sh.

cd ~
rm backup.tar.gz
tar -zcf backup.tar.gz .
Would make more sense?

I know that the #!/bin/bash isn't completely necessary, but there just doesn't seem to be any thought put into it.
 
Old 01-05-2006, 08:06 PM   #13
tommytomato
Member
 
Registered: Nov 2003
Location: Narrogin Western Australia
Distribution: GUI Ubuntu 14.0.4 - Server Ubuntu 14.04.5 LTS
Posts: 966

Original Poster
Rep: Reputation: 32
Quote:
If you ask me, the entire tutorial is written badly.
Thats what I was thinking hey, while having a break and fishing I was thinking the same thing, I haven't had alot to do with scripts, but the ones I have use all start with #!/bin/bash.

So thanks again I'm going to give it another shot today

timmeke

I'm using root all the time, so do I still need to create that /etc/cron.allow folder ?

TT

Last edited by tommytomato; 01-05-2006 at 08:11 PM.
 
Old 01-05-2006, 08:40 PM   #14
tommytomato
Member
 
Registered: Nov 2003
Location: Narrogin Western Australia
Distribution: GUI Ubuntu 14.0.4 - Server Ubuntu 14.04.5 LTS
Posts: 966

Original Poster
Rep: Reputation: 32
ok here is what I did,

I renamed the backup file to backup.sh for starters then I edited the file like so.

Code:
!/bin/bash

# this file is an automated backup script, backup.sh.

cd ~
rm backup.sh.tar.gz
tar -zcf backup.sh.tar.gz .


#rm /srv/www/subs/backup.tar.gz
#tar czf /srv/www/subs/backup.tar.gz .
then I edited my crontab like so, working only with backup.sh for now

Quote:
[root@localhost]# crontab -l
10 * * * 0,1,2,3,4,5,6,7 /usr/local/awstats/tools/awstats_updateall.pl now >/dev/null 2>&1p
30 9 * * * /srv/www/subs/backup.sh
Then I put the time foward a bit and waited for the backup to work, which it didn't to a point, but some how that crontab created a backup.sh.tar.gz file under the path /root/

I'm just wondering if the pathis wrong, the script jumps back to root dir cd ~is that correct if so should it be cd /srv/www/subs/

TT
 
Old 01-05-2006, 08:47 PM   #15
tommytomato
Member
 
Registered: Nov 2003
Location: Narrogin Western Australia
Distribution: GUI Ubuntu 14.0.4 - Server Ubuntu 14.04.5 LTS
Posts: 966

Original Poster
Rep: Reputation: 32
Well guys it was the path

it now works, thanks for the help and tips, need to find out why the awstats one wont work.

TT
 
  


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 On
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 09:09 AM
cron.allow and cron.deny in slackware? tl64 Slackware 5 10-13-2005 10:44 PM
No More Cron Mail, Cron Error? Xhost Linux - General 3 07-26-2004 05:28 PM
[cron][mdk9.1]cron deamon seems to ignore some task... yannrichet Linux - Newbie 5 06-26-2003 10:57 AM
dual entries in cron log for cron.daily cpharvey Linux - General 3 02-27-2003 03:30 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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