• Post Reply Bookmark Topic Watch Topic
  • New Topic

Execute .sh file from Servlet  RSS feed

 
Juan Pablo Arriagada Cancino
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there! this is my first post.
You all help me with your questions and answers for a long time, i am a full time reader of this forum. Thanks all for your time and dedication!.

I have this problem:

I have a shell script named "proceso.sh", this shell simply creates a file (touch MYFILE) and echo a message (echo 'WORKS FINE') in two diferent routes.

/tmp/ and /home/jarrcan/pw2/procesos/bin/

The file has same chmod permission in both directories (chmod 777) and belongs to same user and group (root:tomcat).

I im trying to execute this shell script from a Servlet with this code:


When i use param_proceso_path = "/bin/csh /tmp/proceso.sh"
The file is created and i get a line in my app log that says "WORKS FINE".
And Exit Value = 1

When i use param_proceso_path = "/bin/csh /home/jarrcan/procesos/bin/proceso.sh"
I get no message, no file is created and Exit Value = 1.

As a test i write environtment variables to a file.
USER=tomcat

This app (Spring MVC) runs on a CentOS machine, Tomcat 6 with OpenJDK.

Any lights on this? i read a lot of post with same problem and try every posible solution with out success.

Thanks!



 
J Steven Perry
Greenhorn
Posts: 13
Android Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may have already tried this, but make sure that the tomcat user has access to every directory in the tree:

/home/jarrcan/pw2/procesos/bin/

That is, /home/jcarran, /home/jcarran/pw2, and so on.

Otherwise you have buried a file that the tomcat user *does* have access to inside a directory tree to which it *does not*.
 
Juan Pablo Arriagada Cancino
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there J Steven!

I dont have much experience in Linux permisions.
What i do was to add tomcat and apache users to jarrcan group. its that the same?

If not, how can i do it?

Thanks for your reply!!
 
margaret gillon
Ranch Hand
Posts: 335
6
Linux Tomcat Server Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Juan,

I don't think it is normal or a good practice for programs inside the container to execute code outside the container.

There are many issues with Linux. First the program has to confirm that the servlet is running on Linux. Then you would have to know what user is running the application and their permissions. Each Linux distribution will have different directory structures so where Tomcat is installed on an Ubunut server is very different from where Tomcat is installed on a Redhat server. This can change with the o/s's version, The Tomcat install location on Ubuntu 10.04 is different from the install location on Ubuntu 12. Last , many distributions block .sh files from being executed. On Ubuntu server .sh files are always suspect and Ubuntu stops many of them from running even if the user is an administrator.
 
Juan Pablo Arriagada Cancino
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Margaret! thanks for your reply.

margaret gillon wrote:Hi Juan,
I don't think it is normal or a good practice for programs inside the container to execute code outside the container.


I agree with you in this point, but you know... i'm not the boss so i'm not allowed to think... xD
There's a reason to do this. This shell executes a legacy C++ program that do stuff, and do it well. Implement a ESB to handle this legacy program and use it from its context may be an elegant way but not the way they're goint to take. They don't like my "ideal world".

margaret gillon wrote:
There are many issues with Linux. First to have to confirm that the software is running on Linux. Then you would have to know what user is running the application and their permissions. Each Linux distribution will have different directory structures so where Tomcat is installed on an Ubunut server is very different from where Tomcat is install on a Redhat server. This can change with the o/s's version, The Tomcat install location on Ubuntu 10.04 is different from the install location on Ubuntu 12. Last , many distributions block .sh files from being executed. On Ubuntu server .sh files are always suspect and Ubuntu stops many of them from running even is the user is an administrator.


I didn't now this. We use CentOS on our development (and some clients) servers and RHEL in our big clients servers.
I will consider! thanks for sharing!
 
Juan Pablo Arriagada Cancino
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I forgot to tell that a "Senior Linux Sys Admin" is trying to solve this.
If he can make it work and wants to share the information with this insignificant programmer, i will share it here.

I told him about your aproach J Steven. He look to some point in the space, took his beard and say... "mmm,... maybe,...". (he dont talk too much... ) :p

Thanks!
 
Ivan Jozsef Balazs
Rancher
Posts: 992
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doing first a waitFor and then consuming the output looks suspicious.

Another idea is exec'ing the command broken into parts as an array
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm surprised nobody has yet pointed out that you should read and implement what's written in this article.
 
Ivan Jozsef Balazs
Rancher
Posts: 992
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:I'm surprised nobody has yet pointed out that you should read and implement ...


We all held our breath and waited for you to take the bait.
 
Juan Pablo Arriagada Cancino
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Jozsef Balazs wrote:Doing first a waitFor and then consuming the output looks suspicious.

Another idea is exec'ing the command broken into parts as an array


Ivan! thanks for take the time to reply.
I read a lot of post in this forum and try a lot of things... I forgot to tell that i already try to break command and bring it to .exec() as an array of commands.

Thanks!!
 
Juan Pablo Arriagada Cancino
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:I'm surprised nobody has yet pointed out that you should read and implement what's written in this article.


Ulf! thanks for the great article.
I must say... i already read that but its a good point to start.

Finally i did'nt wait for SysAdmin answer/solution. I implement a ServerSocket / Socket solution with a password send by client to server to execute the Shell (the socket server is runing outside tomcat with user that have privileges to execute Shell). Password is generated by web app and store y datebase, when client ask socket server to execute shell it get password for that user instance and if equals, execute Shell.

I dont have code example here, tomorrow at job y will send it.

Thanks to all!!! this is solved for me.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!