LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 04-03-2007, 05:44 AM   #1
anjanesh
Member
 
Registered: Sep 2004
Location: Navi Mumbai
Distribution: Ubuntu 12.04 64-bit
Posts: 230

Rep: Reputation: 30
Run bash commands script as cron


Hi

I want this batch-script run as cron.
Code:
wget ftp://username:password@domain.com/file.zip
unzip -o file.zip
rm -f data.zip
These are shell commands. What exactly am I supposed to save this file as and how do I get this to run ?

Thanks
 
Old 04-03-2007, 06:06 AM   #2
johnpaulodonnell
Member
 
Registered: Jun 2006
Location: Galway
Distribution: Suse 10.1
Posts: 114

Rep: Reputation: 15
first, make the bash script executable:

Code:
chmod u+x name_of_script
Code:
crontab -e
to add a new cronjob to the crontable; then

Code:
1 2 3 4 5 /path/to/bash-script
1: Minute (0-59)
2: Hours (0-23)
3: Day (0-31)
4: Month (0-12 [12 == December])
5: Day of the week(0-7 [7 or 0 == sunday])

which lets you set a time for which you want to run it...

google will give you more info on the time-setting syntax for things like running a job every hour etc...

I think this should do it...
 
Old 04-03-2007, 06:12 AM   #3
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Rep: Reputation: 30
Pretty easy. Create a file and add this:
Code:
#!/bin/sh
wget ftp://username:password@domain.com/file.zip
unzip -o file.zip
rm -f data.zip
Let's say you saved it as my_script in /home/anjanesh
Next make it executable:
Code:
chmod +x /home/anjanesh/my_script
Now add it to crontab. Type
Code:
crontab -e
and the crontab of whatever user you are logged in as will be opened with the default editor. Add this line to execute the file once per day at 2:00 AM:
Code:
0 2 * * * /home/anjanesh/my_script
That's really it. Here is a quick tutorial on cron.

Hope that helps.
 
Old 04-03-2007, 06:14 AM   #4
anjanesh
Member
 
Registered: Sep 2004
Location: Navi Mumbai
Distribution: Ubuntu 12.04 64-bit
Posts: 230

Original Poster
Rep: Reputation: 30
Thanks. Yes. Im pretty much getting the cron setup using my control-panel CPanel.
I thought to run a bash script I'll need to do something like

./path-to-folder/script.sh ?

zai - is the first line #!/bin/sh required ?
Im having jailshell

Last edited by anjanesh; 04-03-2007 at 06:16 AM.
 
Old 04-03-2007, 06:25 AM   #5
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Rep: Reputation: 30
The first line is the path to the interpreter. You don't have to have it, but if you don't, then the command to execute the file will be a bit different:
Code:
/bin/sh /path-to-folder/script.sh
File extensions on GNU/Linux systems are more for people than the machines, really, so the .sh is not required. And yes, you do have to specify the full path to the script. Whether you do that with an absolute path like /home/anjanesh/my_script or a relative path like ./my_script (because /home/anjanesh/ is already your working directory) doesn't matter. Remember, though, that the preceding dot "." as in the example you gave simply means "this directory that I'm in right now".
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script and cron job rust8y Linux - General 2 07-08-2006 07:45 AM
bash script causes trouble with cron Suinatsa Programming 10 06-14-2006 03:20 AM
cron won't run commands? mma8x Linux - Software 4 04-05-2006 12:25 PM
cron bash script troubles zzyzx Slackware 3 04-12-2005 12:27 PM
bash script from cron kubla Programming 3 01-22-2004 04:30 AM


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