LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 11-22-2006, 03:25 AM   #1
lgmqy2000
LQ Newbie
 
Registered: Nov 2006
Posts: 3

Rep: Reputation: 0
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

Code:
#!/bin/bash
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,

Code:
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
 
Old 11-22-2006, 03:35 AM   #2
billymayday
LQ Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
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

Last edited by billymayday; 11-22-2006 at 03:36 AM.
 
Old 11-22-2006, 03:49 AM   #3
lgmqy2000
LQ Newbie
 
Registered: Nov 2006
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
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.
 
Old 11-22-2006, 04:06 AM   #4
billymayday
LQ Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
Sorry - picked the wrong name - should have said

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

Had a brain fade


Rgds
 
Old 11-22-2006, 04:29 AM   #5
lgmqy2000
LQ Newbie
 
Registered: Nov 2006
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks again, billy. I'll try it and hopefully it will work out.
 
  


Reply



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 08:09 AM
cron job to execute automatically lifegamer Linux - Newbie 2 09-16-2006 08:35 AM
Cannot execute rsync as a cron job jdaniels73 Linux - Software 2 09-03-2006 05:03 AM
Bash script and cron job rust8y Linux - General 2 07-08-2006 07:45 AM
How do I set my profile in CRON job? lolson33 Linux - General 2 12-06-2005 04:18 PM

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

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