This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I'm beginning to learn to write simple shell scripts. It's not as bad a learning experience as I thought.
I am a bit stuck with one of my scripts right now, and I am hoping that someone will suggest the right thing. This is a fairly newbie question.
I have a java program that generates a file. This file has to be uploaded to a server via ftp.
I have written two shell scripts, one to run the java program and the other to upload the file. I am calling them in crontab so that they are executed everyday at a specific time with a difference of half an hour. Although this is working, this is not very good as the file upload script is being called after half an hour. If for some reason my java program takes more than half an hour for execution the file upload scipt will upload incomplete file, or if my program finishes in 5 minutes file upload will wait unnecessarily for 25 minutes.
What I would like to do is call this file upload script as soon as the java program finishes its execution. So what should I be doing, catch the exit code of the java program and call the file upload script?
For simplicity I made the ftp inside a "black box" script here.
Actually, I usually use scp these days, as it's both more secure and simpler to run in batch, but an FTP script works, too.
Almost forgot: In the case of a cronjob, stdout and stderr usually get emailed to you. Otherwise the "echo" would get lost. [ September 28, 2006: Message edited by: Tim Holloway ]
An IDE is no substitute for an Intelligent Developer.
Joined: Jan 23, 2004
Thanks very much for the answer, It did work when I tested this.
However, I hit upon another problem. I've tried several different ways and tried to read about shell script parameters a couple times. But this seems like a tough nut to crack.
Today I generated a couple reports and I had to upload them via ftp to a server. instead of sending each file manually, I thought I would make use of another script to list all the files and send one at a time looping through the list by calling the other script that I have. I found a sample which was easy to understand and pretty neat.
my other script looks like the following
The target directory I am passing in contains whitespaces. I am including that argument in double quotes(I've also tried single quotes). When I echo the variable right before I establish the ftp session, it seems fine. But when it comes to cd, the path is broken up and the program is complaining no such directory found.
I would really appreciate if someone points me in the right direction here.
Thanks for your time.
Regards, Cnu [EDIT] problem with angled brackets with arguments fixed [ September 28, 2006: Message edited by: cnu sri ]
Hmm. I don't think FTP works properly with spaces in names at all. If the destination is a Windows machine can you get away by using the DOS form of the name such as LONGNA~1 ?
As an aside, have you found out about the FTP "mput" command yet? Setting up and tearing down an FTP connection is not a quick process, so if all you are doing is transferring a binch of files from one dir to another over FTP, I'd do something more like:
and call it using
send [hostname] [dest dir]
But I'd second the recommendation for using scp instead. It's simpler, more flexible, more secure and faster too!
scp *.xls username@hostname:destdir [ September 29, 2006: Message edited by: Frank Carver ]
You know, I hate to rain on anyone's parade when writing shell scripts, but this exactly the kind of thing that ANT is designed for.
For instance, you could use something like.
I have no java certifications. This makes me a bad programmer. Ignore my post.
Joined: Jan 23, 2004
Thanks very much for the answer. Specially the mput thing. Unfortunately scp doesn't seem to be an option. when I did man scp, it said no entries.
We are on SunOS 5.8.
When I establish a session either from dos prompt or from the unix box, and then ic I cd to the directory, it's working fine. when I call the same thing from the script, it doesn't. Right now I'm just uploading to a directory under their home directory, so instead of using the full path, I am specifying the directory name. There are no white spaces in that name. So for now, it seems fine.
Thanks for your help.
Lewin, Thanks for your ant task suggestion, When I have taken this up actually the first thought was to either write a java class or update the existing class to upload the files, or to use and ant task, unfortunately I could not convince my team members about that. I also thought this may be an opportunity to learn a little bit of unix as well.
One more thing to keep in mind when using mput, from my understanding you are using this process in batch mode(crontab), I would specifiy when you access the ftp command, otherwise the mput will prompt you for a response before each file is transfered.