LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Run bash commands script as cron (https://www.linuxquestions.org/questions/linux-general-1/run-bash-commands-script-as-cron-542913/)

anjanesh 04-03-2007 05:44 AM

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

johnpaulodonnell 04-03-2007 06:06 AM

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...

zaichik 04-03-2007 06:12 AM

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.

anjanesh 04-03-2007 06:14 AM

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

zaichik 04-03-2007 06:25 AM

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".


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