-   Linux - General (
-   -   How to set up cron job to execute bash script (

lgmqy2000 11-22-2006 04:25 AM

How to set up cron job to execute bash script
Hi folks,

I am very new to Linux. I want to set up a cron job to execute a bash script, below is what I think I shoul do after searching stuff on the Internet. I'd be much appreciated if anyone could point me out what is incorrect and anything else I need to do.

The scenario is SSH (I have access details) to the server, set up a cron job to execute a bash script at 3am every 48 hours. The bash script meant to change system current working directory to the one I specify and invoke a php script.

1. create a bash script search.txt (text file) which contains the following command. I want this bash script switch current working dicrectory to the one i specified below and invoke file sphider.php


cd /home/gwcwater/public_html/sphider/admin
php spider.php full

2. I think I need to upload this bash script to somewhere on the server. is this correct? Where is the appropriate folder to place this file?

3. create a text file, contab.txt, which contains,


0 3 */2 * * [i know i need to specify the bash script file path here, but not sure how]
4. upload crontab.txt using ASCII mode (where is the appropriate palce to place this file on the server?)

5. SSH to the server

6. cd to the directory where contab.txt is stored

7. execute command: crontab contab.txt

billymayday 11-22-2006 04:35 AM

Where you put the script doesn't matter too much - I'd probably put it in the home directory of the relevant user.

Why not edit crontab directly - use crontab -e

Make sure the script is executable (ie chmod u+x filename)

in crontab, type

0 3 */2 * * /path/to/script/sphider.php

Note that crontab uses vim commands, so type i to insert, move to the relevant line, type the above line, then escape wq (to write and quit)

You should be able to do this all through ssh

lgmqy2000 11-22-2006 04:49 AM


Originally Posted by billymayday

in crontab, type

0 3 */2 * * /path/to/script/sphider.php

Hi billymayday,

thank you for your help. If I edit crontab directly, how about the line that is meant to change the current working directory to "/home/gwcwater/public_html/sphider/admin"? where do I type that?

the reason I wanted to put them in a bash script is that I can easily edit the text file and upload it. I am afraid I won't be able to easily get the command/syntax correct if I type everything in a command window and will have to repeat it many times if i always make mistakes.

billymayday 11-22-2006 05:06 AM

Sorry - picked the wrong name - should have said

0 3 */2 * * /path/to/script/search.txt

Had a brain fade


lgmqy2000 11-22-2006 05:29 AM

Thanks again, billy. I'll try it and hopefully it will work out.

All times are GMT -5. The time now is 04:25 PM.