How can I use cron to copy files

software development

#1

I want to copy (and overwrite) everything from one catalog to another. This should be done one minute past every hour every day of the month - and someone told me to use cron.

What should this cron look like? Something like this?

01 * * * * cp /home/username/my_domain/folder /home/username/my_domain/cgi-bin/folder

Do I save this as a file?
What should I save the file as?
How can I execute the file?


#2

that cron line looks correct to me. Every hour, it will copy the files out. If it’s going to be overwritting files, you’ll want to change the command to “cp -rf”

To get it into your crontab, log into ssh and type:
“crontab -e”

That will load up the default editor (nano) where you can copy and paste that line in. Then click ctrl-o to save the file, and ctrl-x to exit. You should get some kind of a message saying new cron file installed successfully.

(if you’re having problems logging into ssh, make sure that your user is allows sshe access - Panel > Users > Manage Users > Edit > check ssh access.)

-Matttail


#3

Thank you Matttail!

I tried what you said and got this:

crontab: installing new crontab
"/tmp/crontab.rebDUh/crontab":1: bad minute
errors in crontab file, can’t install.

Any suggestion?


#4

make sur eyou typed it wright. it needs to be:
01 * * * * cp /home/username/folder /home/username/new_folder

thats 01 space followed by 4 * with spaces between and after. if - represents the space bar it woud look like this:
01-----cp-/home/username/folder-home/username/new_folder

hope this helps.

Edit:This K-base article may be of some help as well.

-Matttail


#5

I couldn’t get it to save when I wrote this:

01 * * * * cp -rf /home/username/domain/folder /home/username/domain/cgi-bin/folder

But it saved when I wrote:

01 * * * * cp -rf /home/username/domain/folder …/cgi-bin/folder

But, can I write it like that?


#6

[quote]…/cgi-bin/folder
But, can I write it like that?

[/quote]

Well, yes and no. the cron tab will accecpt that command, but I don’t think it points to your directory structure. You can allways find out - see if cron gets an error, or if it’s copied to the correct place. But I think it will give you an error becuase that would be writing to (root)/cgi-bin/folder

Is the command wrapping around to two lines? if your directory tree is long, and it doesn’t appear on one line, that could be your problem. This command (and any others later down the road) must appear on one line, if it wraps your server will think it’s two seperate commands. If this is the case, use one of the following:

  1. open up notepad and type the command there. Here it should stay on one line enless you hit enter. save your notepad file, for this example we’ll call it command.txt. Now upload your file to the server. Just place it in your home directory - that’s the one you’re in when you first login. You should see some other folders like logs, maildir, domain.com. Just put the file right there.

Now, log into telnet/ssh again and type:
"crontab command.txt"
which will import the file into your crontab, overwritting anything already there. You will hopefully get an success installing message back.

  1. This method will set the default text editor (nano) to not wrap lines. This way when you edit your crontab you should be able to type out the full line with out it messing this up. You may want to do this one anyways, so you can easly edit your crontab later.

Log into ssh and type:
"nano .bash_profile"
And just add this to the end of that file:
export VISUAL="nano -w"
press ctrl-o to save that file, and ctrl-x to exit nano. Now you should be able to edit the crontab without things wrapping.

Oh, one other thing. I think I didn’t explain what the -rf ment that I sigguested you add into your command does. r stands for recursive and f for force. recursive basically means that it will go through and take all files and sub-folders without question, and force gives it a little extra incentive to go ahead and overwrite anything allready there.

-Matttail


#7

I think you might be right about wrapping so I used your suggestion with command.txt. I think that worked. I didn’t get any error messages.

Now I just have to wait and see if it copies ok.

I also tried typing both nano.bash_profile and “nano.bash_profile”, but nothing happened (no file opened)

…And thank you for detailed explanation - I need that :slight_smile:


#8

No this command didn’t copy like I wanted to (it didn’t wrap):

01 * * * * cp -rf /home/jpwiese/webapp.jpwiese.no/db /home/jpwiese/webapp.jpwiese.no/cgi-bin

The db folder and everything in it should copy to cgi-bin and overwrite the db file thats allready there.


#9

There should be a space between nano and .bash_profile and no quotes.

-Matttail


#10

try running your command straight from the command line, and see if you get an error, perhaps that will shed some light on what’s wrong.

You can also add this to the top of your cron tab to get error messages sent out:
MAILTO:"you@yourdomain.com"

-Matttail


#11

I wrote this:

MAILTO:"me@my-email.com"
10 * * * * cp -rf /home/jpwiese/webapp.jpwiese.no/db /home/jpwiese/webapp.jpwiese.no/cgi-bin

And got this:

crontab: installing new crontab
"/tmp/crontab.WvHhZa/crontab":0: bad minute
errors in crontab file, can’t install.
Do you want to retry the same edit?


#12

I’m very confused. I took the line exactly as you posted it, put it into my crontab and it installed successfully. As you as your sure it’s not wrapping… I have no idea. Sorry, I’m at a loss here. Enless someone else on these forums has an idea, perhaps you should contact support.

sorry.

-Matttail


#13

Thank you so mutch for your help. I have learned things I didn’t know :slight_smile:
I will take this to support now and see if they know what this is.


#14

May I ask one more question?

How can I see if the crontab execute - or have executed?