# Script shell in java code

**URL:** <https://community.unix.com/t/script-shell-in-java-code/309746>\
**Category:** Programming\
**Created:** [May 7, 2012, 8:42am UTC](https://community.unix.com/t/script-shell-in-java-code/309746 "2012-05-07T08:42:39Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![chercheur857](https://community.unix.com/letter_avatar/chercheur857/32/6_22734026db12803ebb3e059622ac3534.png) [@chercheur857](https://community.unix.com/u/chercheur857)\
**Post date:** [May 7, 2012, 8:42am UTC](https://community.unix.com/t/script-shell-in-java-code/309746/1 "2012-05-07T08:42:39Z")

</div>

Hello,

Please i want to insert this code in a java program because i need to call a java function inside the while:

> [@](#):
>
> cmd1 my\_program & my\_pid=$!  
> while cmd2 $my\_pid  
> do  
> sleep 60  
> //call a java function  
> done

Please how can i do?  
thank you so much

---

<div class="post-metadata">

**Author:** ![jlliagre](https://community.unix.com/letter_avatar/jlliagre/32/6_22734026db12803ebb3e059622ac3534.png) [@jlliagre](https://community.unix.com/u/jlliagre)\
**Post date:** [May 8, 2012, 6:30am UTC](https://community.unix.com/t/script-shell-in-java-code/309746/2 "2012-05-08T06:30:42Z")

</div>

What do you mean with "java function" ? A method or an application ?  
In any case, if you code in java, better doing everything in java instead of trying mixing it with shell scripting.

---

<div class="post-metadata">

**Author:** ![chercheur857](https://community.unix.com/letter_avatar/chercheur857/32/6_22734026db12803ebb3e059622ac3534.png) [@chercheur857](https://community.unix.com/u/chercheur857)\
**Post date:** [May 10, 2012, 3:17pm UTC](https://community.unix.com/t/script-shell-in-java-code/309746/3 "2012-05-10T15:17:10Z")

</div>

yes, it's a java method

I would like to insert linux commands in the java code because I have a library that I can use it only through the linux command (cmd1,cmd2)

cmd1,cmd2 generate files ,I have to store them using java methods

> [@](#):
>
> cmd1 my\_program & my\_pid=$! //cmd 1 of my library  
> while cmd2 $my\_pid //cmd2 of my library  
> do  
> sleep 60  
> //call a java method : store files  
> done

Thank you for help

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_22734026db12803ebb3e059622ac3534.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [May 10, 2012, 3:28pm UTC](https://community.unix.com/t/script-shell-in-java-code/309746/4 "2012-05-10T15:28:02Z")

</div>

I think it would make more sense to create a list of files for java to use once, and have a loop inside java itself process them, for two reasons:

1) You can't really embed java inside bash like that  
2) Running, loading, and quitting java 9,000 times to process 9,000 files will be immensely wasteful.

---

<div class="post-metadata">

**Author:** ![chercheur857](https://community.unix.com/letter_avatar/chercheur857/32/6_22734026db12803ebb3e059622ac3534.png) [@chercheur857](https://community.unix.com/u/chercheur857)\
**Post date:** [May 10, 2012, 3:58pm UTC](https://community.unix.com/t/script-shell-in-java-code/309746/5 "2012-05-10T15:58:43Z")

</div>

is this possible in my case? I explain my problem again  
i use the bib of checkpoint BLCR

cmd1 my\_program & my\_pid=$! //cmd 1 of the library --\> let me to run my\_program: process that i want to checkpoint (cmd1=cr-run)  
while cr-checkpoint $my\_pid //cmd2 of the library: generate file of checkpoint  
do  
sleep 60  
//call a java method : store file  
done  
//the while let me to checkpoint my\_program every 6 seconds

---

<div class="post-metadata">

**Author:** ![jlliagre](https://community.unix.com/letter_avatar/jlliagre/32/6_22734026db12803ebb3e059622ac3534.png) [@jlliagre](https://community.unix.com/u/jlliagre)\
**Post date:** [May 11, 2012, 8:31am UTC](https://community.unix.com/t/script-shell-in-java-code/309746/6 "2012-05-11T08:31:26Z")

</div>

You cannot call a java method from a shell script, only a java application.

Use java instead of a shell script to do your loop.

---

<div class="post-metadata">

**Author:** ![chercheur857](https://community.unix.com/letter_avatar/chercheur857/32/6_22734026db12803ebb3e059622ac3534.png) [@chercheur857](https://community.unix.com/u/chercheur857)\
**Post date:** [May 11, 2012, 9:22am UTC](https://community.unix.com/t/script-shell-in-java-code/309746/7 "2012-05-11T09:22:24Z")

</div>

yes i'd like to use java instead of a shell script to do the loop.  
So I have to call Runtime.exec for the first command

> [@](#):
>
> cmd1 my\_program my\_pid = & $!

but I did not understand how to do for the second command since it's a condition in the loop, in addition it must take as parameter the pid of the process running..

Thank you for help

---

<div class="post-metadata">

**Author:** ![jlliagre](https://community.unix.com/letter_avatar/jlliagre/32/6_22734026db12803ebb3e059622ac3534.png) [@jlliagre](https://community.unix.com/u/jlliagre)\
**Post date:** [May 11, 2012, 7:01pm UTC](https://community.unix.com/t/script-shell-in-java-code/309746/8 "2012-05-11T19:01:43Z")

</div>

Use a wrapper that launch cmd1 in the background then prints its pid. Get the pid from the the process outputStream.

---

<div class="post-metadata">

**Author:** ![chercheur857](https://community.unix.com/letter_avatar/chercheur857/32/6_22734026db12803ebb3e059622ac3534.png) [@chercheur857](https://community.unix.com/u/chercheur857)\
**Post date:** [May 12, 2012, 8:33am UTC](https://community.unix.com/t/script-shell-in-java-code/309746/9 "2012-05-12T08:33:11Z")

</div>

Please can you explain your idea a little further  
I don't know this concept of wrapper, I wonder if you could clarify a bit your idea

thank you very much

---

<div class="post-metadata">

**Author:** ![jlliagre](https://community.unix.com/letter_avatar/jlliagre/32/6_22734026db12803ebb3e059622ac3534.png) [@jlliagre](https://community.unix.com/u/jlliagre)\
**Post date:** [May 12, 2012, 8:39am UTC](https://community.unix.com/t/script-shell-in-java-code/309746/10 "2012-05-12T08:39:08Z")

</div>

```nohighlight
#!/bin/sh
cmd1 &
echo $!

```
