LinuxQuestions.org
Register a domain and help support LQ
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 10-01-2004, 10:16 PM   #1
the_rhino
Member
 
Registered: Sep 2004
Distribution: PCLinuxOS
Posts: 59

Rep: Reputation: 15
cron job


I hope this is the right forum for this question.

I am trying to figure out how to write a cron job to put the current date into the name of a new directory that the cron job is going to create on my local box.

Here is what I am trying to do:

* 4 * * * mkdir /home/jack/serverbackup/serverbackup100104
1 4 * * * mv *.tar.gz --target-directory=serverbackup100104

The 100104 would be the current date each day. I have not been able to figure out what command to use to get the current date from my box, so I can't figure out how to do that part of this cron job. I just figured out how to write a cron job today without asking for any help up to this point.

Will some one tell me what command will get the date and point me to some good information that will show me how to use it in my cron job or tell me a better way to do what I am trying to do?

Thanks,
The Rhino
 
Old 10-01-2004, 11:04 PM   #2
secesh
Senior Member
 
Registered: Sep 2004
Location: Savannah, GA
Distribution: Ubuntu, Gentoo, Mythbuntu, ClarkConnect
Posts: 1,154

Rep: Reputation: 47
have you any background in programming?

you'll want to write a script to accomplish this -- what shell do you use?
I use bash... http://www.tldp.org/LDP/abs/html/

here's some code i drew up real quick, you'll want to put this in a file called backup, and modify your crontab to run only backup.

Code:
#!/bin/bash
###########
## Moray ##
###########
today=$(date +%m_%d_%y)
mkdir /home/jack/serverbackup/serverbackup$today
mv *.tar.gz --target-directory=serverbackup$today
of course this isn't a final version of your backup script, but it should get you started, i can see you know how to reference help given your progress to this point.
 
Old 10-01-2004, 11:06 PM   #3
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,065

Rep: Reputation: 295Reputation: 295Reputation: 295
mkdir /home/jack/serverbackup/serverbackup`date +'%m%d%y'`

should do it. For simplicity, you might want to put both commands into a shell script, and then have cron invoke that. And for your first cron command, unless you want it run every minute during the 4 AM hour, replace the first asterisk (before the 4) with a 0 (or whatever minute you want it to run at).
 
Old 10-02-2004, 11:24 AM   #4
the_rhino
Member
 
Registered: Sep 2004
Distribution: PCLinuxOS
Posts: 59

Original Poster
Rep: Reputation: 15
This sort of worked.

#!/bin/bash
###########
## Moray ##
###########
today=$(date +%m_%d_%y)
mkdir /home/jack/serverbackup/serverbackup$today
mv *.tar.gz --target-directory=serverbackup$today

The directory was created like this - "serverbackup$(date" on the first pass
Then the second pass created another directory like this "+%m_%d_%y" inside "serverbackup$(date"
The files that were in serverbackup were moved to "serverbackup$(date"

I tried it by taking out the space between "date" and "+" so it looked like this $(date+%m_%d_%y) that created this "serverbackup$(date+%m_%d_%y)" then the files were moved into that directory and the second directory was not created. Doing this $(date+%m%d%y) did the same thing as the other.

This did not work at all.

mkdir /home/jack/serverbackup/serverbackup`date +'%m%d%y'`

Thanks for pointing out my mistake with the minute value.

I don't understand how to know or control what shell is being used.

I assume I am using the BASH shell. I start konsole in KDE and whatever shell is automatically started with that is what I am using. The man page for BASH is major information overload and very confusing. Google brings up sites that basically have the same information. I have not been able to find a tutorial that explains what the information in the BASH manual is saying. I will figure this out one way or another, sooner or later. This whole learning process with Linux has been very difficult but I won't stop because I like what I see and have learned so far. It sure beats using mswindows.
 
Old 10-02-2004, 11:45 AM   #5
mirradric
Member
 
Registered: May 2004
Location: Singapore
Distribution: Debian woody and debian sarge
Posts: 188

Rep: Reputation: 31
The first line of the shell script

Code:
#!/bin/bash
tells which ever shell that is handling this script that it need to be run with the said interperter (/bin/bash in this case). So it will invoke bash and pass it the script.
 
Old 10-02-2004, 11:58 AM   #6
secesh
Senior Member
 
Registered: Sep 2004
Location: Savannah, GA
Distribution: Ubuntu, Gentoo, Mythbuntu, ClarkConnect
Posts: 1,154

Rep: Reputation: 47
let's start with the basics - run the following:
/bin/bash --version

FYI-
i have 2.05b, and verified the mkdir command in the code i suggested.

to do so, i created the file 'bak' in my home directory which now contains the codeblock below. It (today) makes the directory backup10_02_04 when i run './bak' from my home directory.
Code:
#!/bin/bash
###########
## Moray ##
###########
today=$(date +%m_%d_%y)
#directory=/home/jack/serverbackup/serverbackup
directory=./backup
mkdir $directory$today
#mv *.tar.gz --target-directory=serverbackup$today
 
Old 10-02-2004, 12:07 PM   #7
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,241

Rep: Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024
You can check what is going on by doing
today=$(date +%m_%d_%y)
echo $date
at the cmd line.
Here's some tutorials:
http://dsl.org/cookbook/cookbook_toc.html
http://freeos.com/guides/lsst/
http://www.icon.co.za/~psheer/book/index.html.gz
 
Old 10-02-2004, 12:09 PM   #8
secesh
Senior Member
 
Registered: Sep 2004
Location: Savannah, GA
Distribution: Ubuntu, Gentoo, Mythbuntu, ClarkConnect
Posts: 1,154

Rep: Reputation: 47
Quote:
You can check what is going on by doing
today=$(date +%m_%d_%y)
echo $date
well, except that $date is never set -- that'd be $today

Last edited by secesh; 10-02-2004 at 12:28 PM.
 
Old 10-02-2004, 02:47 PM   #9
the_rhino
Member
 
Registered: Sep 2004
Distribution: PCLinuxOS
Posts: 59

Original Poster
Rep: Reputation: 15
Thanks to all for trying to help. I am no closer to understanding this or getting it to work. The more I read to get my questions answered the more questions I have. I don't know how to make this work in the crontab. I can make it all work perfectly if I type it in at the prompt. I also have no idea how to make the crontab execute the commands from another file either. There is clearly something missing from my knowledge and understanding but I don't know what. At this point I am totally confused.
 
Old 10-02-2004, 03:06 PM   #10
mirradric
Member
 
Registered: May 2004
Location: Singapore
Distribution: Debian woody and debian sarge
Posts: 188

Rep: Reputation: 31
What that was suggested here is that you put the commands in a file, we call that a script, make it executable and make cron execute that instead of having to have all the commands in the crontab.

The script is just a plain text file, but you'll have to make it executable by doing a chmod.

In the crontab, you replace your original commands with the script you have just wrote.

for example if the script you have created is myscript, you have this in your cron tab.

Code:
1 4 * * * myscript
 
Old 10-02-2004, 03:34 PM   #11
the_rhino
Member
 
Registered: Sep 2004
Distribution: PCLinuxOS
Posts: 59

Original Poster
Rep: Reputation: 15
YES!!!!!

I for got about the chmod to set execute. It works! Thank You!
 
  


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
Cron Job petenyce Linux - Newbie 5 10-11-2005 04:03 PM
cron job working2hard Programming 6 07-28-2004 09:12 PM
cron job ? johnyy Linux - Software 3 12-10-2003 06:00 PM
Cron Job imanahmadi Linux - Newbie 1 07-03-2003 11:39 PM
cron job? luap Linux - Software 2 03-16-2003 08:48 AM


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