I invoke a shell command by Process class from java and it prints
"stty: standard input: Invalid argument"
no matter whether the command is right or wrong (normal output of shell command is shown too). If I run the shell command in shell, no such error message is shown.
The command is something like this: {"/bin/csh", "-c", "echo hello"}
You are invoking the stty command from your .profile, or .bash_profile. You'll have to redirect its standard error to /dev/null.
stty blah blah blah 2>/dev/null
stty can't deal with the pseudo-tty that Java provides in shelling out.
Try using the -f option of csh to disable the reading of the .chsrc and .login files:
{"/bin/csh", "-cf", "echo hello"}
Quoth the documentation for java.lang.Process:
"The methods that create processes may
not work well for special processes on
certain native platforms, such as
native windowing processes, daemon
processes, Win16/DOS processes on
Microsoft Windows, or shell scripts.
The created subprocess does not have
its own terminal or console."
Perhaps you would like the java.lang.ProcessBuilder, instead.
Have you look here.
I managed to run some commands like this
Related
I researched this question and all answers suggest visudo to add:
Defaults:user !requiretty
It does not work!
I have two Linux boxes (RedHat). I have a Java class which uses ProcessBuilder/Process to execute commands. The class runs under a system account.
On both boxes, I run
su other-user -c 'some-command'
and provide the password for other-user in an input stream to the Process object.
On one box, the command succeeds and on the other I get the error above.
The sudoers files on both machines did not have exclusions for requiretty ('Defaults requiretty' is set on both files).
I tried adding the exclusion as suggested by the answers I found. I tried both the system user running the process and 'other-user'...
Does not work.
What else can be the issue? Do I need to restart the system after adding the requiretty exceptoin(s)?
sudoers is for sudo rather than su so you should use sudo.
According to su manual:
-c, --command COMMAND
Specify a command that will be invoked by the shell using its -c.
The executed command will have no controlling terminal. This option cannot be used to execute interactive programs which need a controlling TTY.
you can use a TTY spawning if you are trying to avoid using sudo or you don't have a sudo privileges.
Just invoke one of the following codes before running the code which giving you the error you mentioned.
here are some examples of codes you can use, depends on the code or the system you are using:
python -c 'import pty; pty.spawn("/bin/sh")'
echo os.system('/bin/bash')
/bin/sh -i
perl —e 'exec "/bin/sh";'
perl: exec "/bin/sh";
ruby: exec "/bin/sh"
lua: os.execute('/bin/sh')
(From within IRB)
exec "/bin/sh"
(From within vi)
:!bash
(From within vi)
:set shell=/bin/bash:shell
(From within nmap)
!sh
the first three choices up are my common used ones, and I am trusting their results.
I am using them while pentesting.
QUESTION: What does each element of the command:
pkill -INT -f '^php test_program.php$'
do when I run it in the linux terminal? I already know that the command kills the process called test_program.php, but I don't know what all the different elements of the command are doing. Please explain in as simple terminology as possible! I am new to linux commands and I prefer baby lingo to tech lingo at the moment :)
MY RESEARCH: By running man pkill in the linux terminal, a manual appears with the following pkill definition:
signal processs based on their name or other attributes.
which leads me to believe that pkill doesn't only kill a process, but rather can send a lot of different signals, one of which might kill the process. The structure/synopsis of the pkill command was displayed as: pkill [option] pattern
From the list of options in the same manual, -f, -full had the following definition:
The pattern is normally only matched against the process name. When -f is set, the full command line is used.
I didn't completely understand what that meant. Also, there is a -INT before the -f in the command, so that leads me to believe that more than one option can be joined together, however -INT was not displayed in the manual.
The other parts of the command seem to be identifying the program that is running: '^php test_program.php$', but why isn't that part of the command just 'test_program.php'? What does ^php at the beginning and $ and the end do?
You are looking at a Regular Expression. This expression looks for the string test_program.php anywhere in the process name. So if the process name would be something like
/var/php -runcommand test_program.php
it would find the process and kill it.
This also explains the -f, -full option. Not using a Regular Expression, you would have to take the full process name (the preceeding line) to match the process.
Finally, the -INT is usually used to send a runlevel to the task.
EDIT
I was wrong, the -INT option is not used for runlevels (which are for the Linux kernel) but to send signals to a task. This could be something like Term (terminate), Stop (shut down) or Cont (continue process). pkill sends by default the terminate signal to the process.
The /var/php -runcommand test_program.php was an example for a process. If you use the command ps ax, you get a list of all processes and which programs execute them. So I just assumed that the php interpreter resides in /var/php/ and the execution of the php file is a command.
We are trying to diagnose the cause of a problem with Java code (JRE7) run on an AIX6 system. High-level overview is that SCRIPTA.sh executes java, passing the class MyJava1. The script contains this:
exec 1>/our/log/file
exec 2>&1
echo "Java tool execution began at $(date '+%Y-%m-%d %T')"
/path/to/java -cp $CLASSPATH:support_stuff.jar the_tool > $TEMPFILE 2>&1
echo "Java tool execution ended at $(date '+%Y-%m-%d %T')"
...
The process runs for about 30 seconds, then returns, after which our log file contains the message "Java tool execution began...", and nothing after that. Notably, the "Java tool execution ended" message is never written to the log, and none of the other code after that is executed.
So with all that said, what might we look for in Java source that could cause such a catastrophic exit?
Thanks
I call a Java function using PHP. The code is:
exec('pushd d:\xampp\htdocs\file_excecute\class & java Autoingestion username password id Sales Daily Summary 20120902',$output,$return);
This code worked on a Windows machine but it is not working on a Linux server. The code is:
exec('pushd \var\www\domainname.com\itune_report\class & java Autoingestion username password id Sales Weekly Summary 20120901',$output,$return);
You are using the wrong kind of slash as a field separator, but that may not be your only problem.
The output of the command appears in $output, since you use the exec(command, output, return) form.
However, this only gives you stdout. The shell will send error messages to stderr.
Unfortunately there isn't a version of exec() that reads stderr.
You can merge both outputs to $output by adding 2>&1 at the end of your shell command:
exec("mycommand 2>&1", $output, $return);
Look at $output, and you will either find the output of your successful command or error messages which you can use to work out why it didn't work.
If you want to write something more rigorous that treats stdout and stderr separately, you'll need to use proc_open() instead: PHP StdErr after Exec()
There are (perhaps insurmountable) difficulties when trying to execute sudo commands from a PHP script and from an external script called by PHP on SELinux enabled machines.
Make sure you use Linux directory path in your command
Linux won't let apache change the group id of the process by default.
You may need to use another solution, like make the PHP script deposit a file in a directory which is monitored by cron or inotify and which will call another script with root privileges.
Obviously it does not work on Linux. Command pushd is defined in windows shell only. The path on linux must use forward and not back slashe as separator.
jps.exe which found on JDK 1.5 and later could monitor all Java process but is there a way to detect the specify command line and terminate the correct pid?
What if the user have JRE, is there a similar code allow us to terminate any process easily?
Prefer to keep the topic on Windows which I am working on.
The jps command supports a number of options that modify the output of the command. These options are subject to change or removal in the future.
-q Suppress the output of the class name, JAR file name, and arguments passed to the main method, producing only a list of local VM identifiers.
-m Output the arguments passed to the main method. The output may be null for embedded JVMs.
-l Output the full package name for the application's main class or the full path name to the application's JAR file.
-v Output the arguments passed to the JVM.
-V Output the arguments passed to the JVM through the flags file (the .hotspotrc file or the file specified by the -XX:Flags= argument).
Pipe the output of jps to grep or sed or awk or perl or even another Java program for further matching, parsing and action. On Windows, the easiest way to get those utilities is through Cygwin.
Here are some Microsoft downloadable command line utilities which are useful for working with processes on Windows:
pskill
pslist
and the rest of the Sysinternals Suite
If the user don't have jps, you can use ps. The command line options for ps differs between platforms, see man ps on you system. I use ps -C java -o pid,time,cmd to list java processes on a CentOS system. Then kill to terminate.