Java program stdout and detaching from foreground - unix

I have a java program, let's say Test.class.
When I execute java Test the program ask for a Password and then continute.
The problem is that the stdout is redirected to a log and the program is launched with the & ( we are on UNIX).
How can i interact with this program launched java Test & with the stdin and stdout?
One possible solution is to start the program in foreground and then after a condition run it in background from java.
Thanks!

If the program can read the password from stdin, you can have a Unix script prompt for the password, then start the Java application and pass the password to it, e.g.:
echo $PASSWORD | java Test >log.out &
Or you can consider to split your Java application in two parts; there could be one interactive "front-end" part that validates the password, and then once the password is validated this could launch a "back-end" part as a background process and exit.

One option is to pipe the input to your program using echos as:
(echo input1
echo input2
....
) | java Test >& logfile &
Alternatively if the number of inputs are large you can also put your inputs in a file and redirect the file contents as:
< input_file java Test >& logfile &

I don't see anything Java specific in this question, if you want to drive the stdin based on the application output, you can use the Expect utility: http://en.wikipedia.org/wiki/Expect
Beware though, Expect is notoriously fragile, you'd do wise to refrain from using it in production scenarios.

Actually if you only want to be able to enter the password, perhaps you can try launching your app in foreground (without the trailing &).
Then, after you have entered the password, press Ctrl+Z or in another shell do kill -SIGSTP <pid> in order to suspend your program. Finally, type bg to put it in background.
Read the docs about your shell's job-control functionality for details.

Related

sqoop2 client batch mode script example

Can anyone show me an example of script that can be run from sqoop2 client in batch mode?
I refered http://sqoop.apache.org/docs/1.99.2/Sqoop5MinutesDemo.html
and it says we can run sqoop2 client in batch mode using the following command
sqoop.sh client /path/to/your/script.sqoop
but that script.sqoop isn't like sqoop1 script, so how should it be?
Batch file is nothing but a list of the same commands you would otherwise type in interactive mode (plus comment lines starting with pound sign).
However! Some commands require manual input, thus cannot be easily fully automated (e.g., 'create link' command). See this thread for details.

Change authorization level when initializing sas batch job

I run SAS batch jobs on a UNIX server and usually encounter the problem that I cannot overwrite sas datasets in batch that have been created by my user locally without changing the authorization level of each file in Windows. Is it possible to signon using my user id and password when initializing the batch job to enable me to get full authorization (to my own files) in batch?
Another issue is that I don't have authorization to run UNIX commands using PIPE on a local remote session on the server and can hence not terminate my own sessions. It is on the other hand possible to run PIPE in batch, but this only allows me to terminate batch jobs so I also wonder if it is possible to run a pipe command in batch using my id and password as the batch user does not have authorizatio to cancel "local remote sessions" on my user?
Example code for terminating process:
%let processid = 6938710;
%let unixcmd = "kill &processid";
%PUT executing &unixcmd;
filename unixcmd pipe &unixcmd.;
there's a good and complete answer to your first point in the following SAS support page.
You can use the umask Unix command to specify the default file permission policy used for the permanent datasets created during a SAS session (be it batch or not).
If you are lauching a Unix script which invokes a SAS batch session you can put a umask command just before the sas execution.
Otherwise you can adopt a more permanent solution including the umask command in one of the places specified in the above SAS support article.
You are probably interested in something like:
umask 002
This will assign a rw-rw-r-- file permission to all new datasets.

Search for a folder using applescript

I am trying to make a folder to go on a server, that people can add photos to and then the script sends them to the correct place, however I'm having trouble with the search part.
As you can see below in my code, the part where it finds where to send the folder to is commented out, because I have no idea what the syntax is for it.
Any help would be greatly appreciated.
global theWatchedFolder
set theWatchedFolder to choose folder
on idle
tell application "Finder"
set theDetectedItems to every item of theWatchedFolder
repeat with aDetectedItem in theDetectedItems
set jobNumber to display dialog "Please enter the job number for this photo." buttons {"Submit", "Cancel"}
display dialog "File detected: " & jobNumber
--tell finder
-- search for jobNumber in (path to desktop)
--set jobFolder to top search result
--end tell
--set colourFolder to jobfolder & /colour
move aDetectedItem to the desktop --move to colourFolder
end repeat
end tell
if theDetectedItems is not {} then
activate
display dialog "test move complete"
end if
return 1
end idle
Also, I am concerned that if this script is on the server, watching a folder on the server, then it won't create a pop-up for anyone who adds a file to the folder on the server. Hopefully I am wrong but if someone could confirm this one way or the other then that would be awesome. Thanks :)
I can confirm your biggest fear. The display dialog is shown in the Finder who is targeted. You'll always address the Finder on the same machine that is running your script, unless your using remote events. If the script is running on the server, the dialog will appear in the Finder running on the server.
I have also a side note that you continuously keep running AppleScript using an idle handler to check for any updates in a particular folder. Did you know that AppleScript has memory leaks as an stay open application? It's software you don't want to run constantly on a server. It's better to start a new AppleScirpt in a new process once in a while (I prefer something like every hour) and quit the current running one. You can still use the idle handler but if the idle handler runs every 10 seconds I would quit this script and start a new one after 600 loops.
Then back to your search. Finder doesn't have a search command. Since Mac OS X Tiger Apple introduced spotlight, a meta database for finding different kind of data (files, bundles, mails, etc). However spotlight has never been scriptable but for AppleScript only accessible on the command line using mdls, mdfind and mdutil. To execute commands on the command line we use the do shell script command in AppleScript or do script command for scripting the Terminal.app. Here an example of how to use with a do shell script command
set theFolder to choose folder
set searchKey to "the*" --use * as wild card
findMetaDataInFolderByName(theFolder, searchKey)
on findMetaDataInFolderByName(HFSPath, searchKey)
set options to " -onlyin " & quoted form of POSIX path of HFSPath
set options to options & " \"kMDItemFSName == " & quoted form of searchKey & "\""
return paragraphs of (do shell script "mdfind " & options)
end findMetaDataInFolderByName
note: because we're working in the shell the paths returned are posix path which can be used anywhere by prefixing the path with posix file
But, you have mentioned that the search has to be invoked on a server. Normally when your have a server properly installed the shares are outside the application and user home folder. These folders are by default only indexed by spotlight so spotlight needs to index on the fly. In other words it's very slow compared to normal spotlight searches with are done in less than a second. So I would suggest another version of the same script as above but using find. Find will simply walk recursively to the given directory and print every match.
set theFolder to choose folder
set searchKey to "the*" --use * as wild card
findFilesInFolderByName(theFolder, searchKey)
on findFilesInFolderByName(HFSPath, searchKey)
--the HFSPath can't have a trailing "/"
set UFSPath to POSIX path of HFSPath
if UFSPath ends with "/" and UFSPath is not "/" then set UFSPath to text 1 thru -2 of UFSPath
set options to space & quoted form of UFSPath
set options to options & " -iname " & quoted form of (searchKey) --iname is case insensitive file name match
paragraphs of (do shell script "find " & options & " -print 2>/dev/null ; exit 0") --pipe error to /dev/null to exclude permission denied messages
end findFilesInFolderByName
note: A side effect is because find will try every file while meta data search works different you'll probably find more files now because folders are included in the search as well. Just like findMetaDataInFolderByName(), findFilesInFolderByName() will return posix paths.

How can I pass command line input to external process in Unix

My program runs(exec..) an external program.
While running, the external program asks user [Yes/No] to proceed next step.
Instead of typing [yes] in command line, how can I pass [Yes] to the external program from my program.
Unless the external program supports a respective flag (see #Jonathan Leffler's answer), your you have control over that program's source and can add it, you have to simulate the "yes" input.
Options:
Try launching the external program by piping the output of the yes helper application to it's stdin: yes | external_program. yes is a simple tool, should you not have it, that just writes "y" to it's stdout continually.
Manually write "yes" to to stdin of the external program.
Both options require your to use pipes in one way or the other. See this for more information on how to do that.
The classic way to provide a 'yes' response on the command line is a -y option (usually with a parallel -n option to indicate a 'no' — see fsck(1)).
There's also room to argue that running the program should be a 'yes, I mean to do it' operation. However, there are times when it makes sense to specify 'yes, I really mean to do it' (such as one-time initialization of an instance of a DBMS).

Automating Telnet without spawn/expect/send commands

I did ask this question before but that thread was closed as the question was vague. So here is try #2:
I wish to have a simple script that connects to a remote machine via telnet, then executes the command ZAHO, and then stores the output in a file cat.txt on my local machine. Here is the code I tried:
(echo "PPATIL"; sleep 1 ; echo "IDEA#2010" ; sleep 1;) |telnet 10.110.3.132 23<< EOF
ZAHO; > cat.txt
EOF
The following thing happens when I run the script:
Trying 10.110.3.132...
Connected to 10.110.3.132.
Escape character is '^]'
After this, I come back to my shell prompt without anything happening.
Now, even when I supply a wrong a password, I still get the same error.
P.S.: spawn/expect/send are not present and only option to login is through telnet.
I also tried this
(echo "PPATIL"; sleep 1 ; echo "IDEA#2010" ; sleep 1;echo "ZAHO;";) |telnet 10.110.3.132 23
In this case however, the remote machine gets connected, it shows me the username prompt, enters the username, then shows password prompt, then enters password, sleeps for a second and then appends "ZAHO;" to the password, and gets disconnected.
If I remove echo ZAHO; then also it gets disconnected after entering username and password.
In general:
cmd | cmd2 << EOF
EOF
is ill defined. (I'm not sure if the shell grammar clarifies this as undefined, or implementation defined, or what, but it certainly is not going to do what you want.) cmd2 can only have one input source, but you are trying to give it input both from the pipe and the heredoc. You need to pick one. Perhaps you can try:
{ cmd1; cat << EOF; } | cmd2
input
EOF
but that is pretty obfuscated, and you would be better off writing a script to generate the desired input.
If you are not restricted to shell script. Python has a
telnet interface in its standard library. Actually the example given on the library documentation page, is very close to what you want.
Python is perhaps not as ubiquitous as shell, but it is widely deployed and used.

Resources