Question


Last activity: 18 Jul 2017 10:20 EDT
How can I execute a shell script from PRPC?
Hi,
If I have shell script on the filesystem of the machine hosting my JVM, how can I execute it from PRPC? I suspect this will require some java in an activity. Any pointers are appreciated
Thanks.
***Updated by moderator: Lochan to close post***
This post has been archived for educational purposes. Contents and links will no longer be updated. If you have the same/similar question, please write a new post.
-
Like (0)
-
Share this page Facebook Twitter LinkedIn Email Copying... Copied!
Accepted Solution
Updated: 17 Jun 2015 12:54 EDT


Here you go, use the Java ProcessBuilder class to executive OS native scripts, below is an example:
ProcessBuilder pb = new ProcessBuilder("myshellScript.sh", "myArg1", "myArg2"); Map<String, String> env = pb.environment(); env.put("VAR1", "myValue"); env.remove("OTHERVAR"); env.put("VAR2", env.get("VAR1") + "suffix"); pb.directory(new File("myDir")); Process p = pb.start();


Pegasystems Inc.
GB
Not done this myself - but I would say a Java Step will probably do it : using standard Java APIs - see here: http://stackoverflow.com/questions/525212/how-to-run-unix-shell-script-from-java-code
Alternatively (if you have some control over the shell-script) - you could create a file from PRPC that the Shell Script could poll for.


Pegasystems Inc.
US
I would also consider the business need. Why do you need the shell script? What is the desired functionality? What requires PRPC to be the initiator ?


Hi Peter, from a business perspective, the requirement would be to eliminate the need to connect to the system using SSH to execute some common administrative tasks.


I have seen people executing shell scripts from within PRPC by writing a java step in an activity.
Accepted Solution
Updated: 17 Jun 2015 12:54 EDT


Here you go, use the Java ProcessBuilder class to executive OS native scripts, below is an example:
ProcessBuilder pb = new ProcessBuilder("myshellScript.sh", "myArg1", "myArg2"); Map<String, String> env = pb.environment(); env.put("VAR1", "myValue"); env.remove("OTHERVAR"); env.put("VAR2", env.get("VAR1") + "suffix"); pb.directory(new File("myDir")); Process p = pb.start();


MBAFC
CN
Now I have a requirement to run the shell script from the java method in the activity.
I had try as follow but it look's like nothing happend.
=====================================================
try{
//Process pb = new ProcessBuilder("/opt/apl/batch/sample_script.sh").start();
//Process proc = Runtime.getRuntime().exec("/opt/apl/batch/sample_script.sh");
new ProcessBuilder("/opt/apl/batch/sample_script.sh").start();
} catch(java.io.IOException ioEx) {
}
=====================================================
Is there somebody had been successfuly running the unix/linux shell on the prpc.
If you had? could you post your coding?


Pegasystems Inc.
GB
You are swallowing any errors with your empty 'catch' block:
Try re-throwing any exceptions like this:
catch(java.io.IOException ioEx) { throw new PRRuntimeException(ioEX); }
And see what error you get.
You may also have fully qualify your Java Class reference:
java.lang.ProcessBuilder
Thanks
John
Updated: 22 Jan 2016 4:41 EST


Pegasystems Inc.
GB
This works on my PRPC719 system running on Linux.
I have a script '/tmp/sample.sh' like this:
#!/bin/sh echo "I was run" >> /tmp/sample.log
I made sure it can run from the Linux command line first:
$ chmod +x /tmp/sample.sh $ /tmp/sample.sh $ tail -f /tmp/sample.log
This shows the script was run:
I was run
I left the 'tail -f' running (to watch for new entries in 'sample.log').
And then created the following Activity,based on your code (I did NOT have to fully qualify the class in this instance - probably because it is in java.lang.*):
Java Code:
This works on my PRPC719 system running on Linux.
I have a script '/tmp/sample.sh' like this:
#!/bin/sh echo "I was run" >> /tmp/sample.log
I made sure it can run from the Linux command line first:
$ chmod +x /tmp/sample.sh $ /tmp/sample.sh $ tail -f /tmp/sample.log
This shows the script was run:
I was run
I left the 'tail -f' running (to watch for new entries in 'sample.log').
And then created the following Activity,based on your code (I did NOT have to fully qualify the class in this instance - probably because it is in java.lang.*):
Java Code:
try{ new ProcessBuilder("/tmp/sample.sh").start(); } catch(java.io.IOException ioEx) { throw new PRRuntimeException(ioEx); }
Saved it, and ran it:
Observed that the 'tail -f' shows the new entry in 'sample.log'
I was run
I was run
So this works: but it is fragile : we aren't capturing any output from the script, or checking the result...which I would recommend you would need to do in any sort of Production environment.
(Consider whether calling a shell script from java is a good solution at all: couldn't you construct an OOTB PRPC Service Rule to output to a queue/file; which a shell script could pick up ? That might be more maintainable in my opinion - and decouples PRPC from having to create and manage lots of workings of the script).


MBAFC
CN
Hi, John
Thanks for your help.
I will try it again and think about if we would use the java to calling a shell script from java.


MBAFC
CN
Hi, John
one more question about that.
Could you post which user run the shell when we call the shell by PRPC


Pegasystems Inc.
GB
It's the user which runs the JVM.
So in my case - my user 'pritj' is running Tomcat:
$ ps -ef|grep java|grep -v grep
pritj 6902 1 6 09:23 ? [...]org.apache.catalina.startup.Bootstrap start
The file my script is writing to is also owned by the same same user:
$ ls -lrt sample.log
-rw-r--r-- 1 pritj pritj 20 2016-01-22 09:36 sample.log
This is another good reason to reconsider whether you really want to make a direct call from Java to create a process: much cleaner to decouple the PRPC Activity from the working of the underlying actual script.
Just have PRPC write a file and have a (quite straightforward) shell script to poll for it maybe ?


MBAFC
CN
Hi, John
Thanks for your reply.
Just now I had successfully called the shell script by the java method in the activity.