Site5 - Built For Designers & Developers MENU

How do I setup a cron job?

[3 Grey Star Level]

I’d like to automate, on schedule, a specific task. Suppose every Wednesday at 2am I want to copy a given file from one location to another on my server. How would I go about doing that using a cron job?

Answer #1

Hey Mike,

Cron jobs are laid out as follows:

* * * * * command to be executed
– – – – –
| | | | |
| | | | +—– day of week (0 – 6) (Sunday=0)
| | | +——- month (1 – 12)
| | +——— day of month (1 – 31)
| +———– hour (0 – 23)
+————- min (0 – 59)

You can do this either from SSH in your account or from your SiteAdmin/CPanel under the Cron Jobs section. If from SSH, just do the following:

crontab -e

This will open a command prompt. Then, insert the following line:

0 2 0 0 3 nice -n15 cp /home/USER/public_html/somefile.php /home/USER/public_html/somefolder/

Just modify the string as necessary. If you’re copying a folder, use “cp -fr” instead of just “cp”. Or, if you’ll be replacing the same file each day – then you’d need at least cp -f.

From SiteAdmin/CPanel you’d just enter in the appropriate time to execute the command and save. The new cron job will be automatically executed from then on.

Hope that answered your question!

Answers Answered By: Ronald [6 Grey Star Level]

Answer #2

Sorry, one correction!

0 2 0 0 3 would actually be * 2 * * 3

Answers Answered By: Ronald [6 Grey Star Level]

Answer #3

Firstly you need to make a script that does what you want.

Make a file somewhere and put the following in it:

#!/bin/bash
cp /path/to/file /path/to/copy/to

Then in the cron job editor set the command as bash -e /path/to/that/script/you/made

Answers Answered By: david [10 Grey Star Level]

Answer this Question

You must be Logged In to post an Answer.

Not a member yet? Sign Up Now »

Money Back Guarantees
Click to verify BBB accreditation and to see a BBB report.