I am working on a PHP back that uses python scripts. In PHP, queries and processes are carried out that at the end are passed as arguments to the script, a string is created with the command that is finally executed with exec ()
$arg4Json= str_replace("'", "´", json_encode($arg4));
$command = "python ../path/of/file"
. "/script.py {$arg1} "
. "{$arg2} {$arg3} '{$arg4Json}'";
$output = null;
$returnVal = null;
exec($command, $output, $returnVal);
This was working perfectly. However, in a certain query the arg4Json turns out to be too long and the service crashes. Try to run the command directly in the console with the same arguments and it returns the message:
python: Argument list too long
I have investigated about it and apparently it is a matter related to the maximum length allowed by the console to execute the command and that it is necessary to increase the limits. In the end I was able to increase the 'stack size' and leave it unlimited to test if it worked but it keeps throwing me the same error.
ulimit_list
I would appreciate if someone can guide me on how I can solve this problem. The project runs on a linux server with ubuntu on Apache2.
why not put arguments in a file. and change the behaviour of your program to read arguments from the file instead of passing them as arguments. it's more efficient i think
Related
In my python script I need to execute a command over SSH that also takes a heredoc as an argument. The command calls an interactive script that can be also called as follows:
dbscontrol << EOI
HELP
QUIT
EOI
I also found this Q&A that describes how to do it using subprocess but I really like pexpect.pxssh convenience.
Code example would be greatly appreciated
I don't have pexpect handy to test my answer to your question, but I have a suggestion that should work and, if not, may at least get you closer.
Consider this command:
$ ssh oak 'ftp << EOF
lpwd
quit
EOF'
Local directory: /home/jklowden
$
What is happening? The entire quoted string is passed as a single argument to ssh, where it is "executed" on the remote. While ssh isn't explicit about what that means, exactly, we know what execv(2) does: if execve(2) fails to execute its passed arguments, the execv function will invoke /bin/sh with the same arguments (in this case, our quoted string). The shell then evaluates the quoted string as separate arguments, detects the HereDoc redirection, and executes per usual.
Using that information, and taking a quick look at the pexpect.pxssh documentation, it looks like you want:
s = pxssh.pxssh()
...
s.sendline('ftp << EOF\nlpwd\nquit\nEOF')
If that doesn't work, something is munging your data. Five minutes with strace(1) will tell you what happened to it, and you can start pointing fingers. ;-)
HTH.
I am trying to use Python to run an executable (Windows 7) with parameters. I have been able to make the program run, but the amount of parameters I can use that will prove the Python script worked with parameters is limited. The best one is formatted like so:
-debugoutput debug.txt
I have tested this using a windows shortcut with an edited target and it works, it creates a debug output in the program directory.
Here is the code I am using:
import subprocess
args = [r"C:\Users\MyName\LevelEditor\LevelEditor.exe", "-debugoutput debug.txt"]
subprocess.call(args)
This does run the program, but the debug output is not created. I have tried putting an "r" in front of the parameter but this made no difference. I assume it is a simple formatting error but I can't find any examples to learn from that are doing the same thing.
UPDATE:
Thanks for the answers everyone, all the same, simple formatting error indeed.
In-code definition results in invocation of shell command line:
C:\Users\MyName\LevelEditor\LevelEditor.exe "-debugoutput debug.txt"
As you can see, by merging -debugoutput debug.txt to single list element, you explicitly stated that space between them shouldn't be parsed as command line argument separator.
To achieve expected behavior put file name string as separate element to argument list.
[r"C:\Users\MyName\LevelEditor\LevelEditor.exe", "-debugoutput", "debug.txt"]
As far as I know you need to split the arguments by the space, so your args would look like:
args = [r"C:\Users\MyName\LevelEditor\LevelEditor.exe", "-debugoutput", "debug.txt"]
Does that work?
I do not know if it works, but
import subprocess
args = [r"C:\Users\MyName\LevelEditor\LevelEditor.exe", "-debugoutput", "debug.txt"]
subprocess.run(args)
Following the docs
I have a problem I am hoping someone can help with
I am running a perl script that calls a python script with a set of arguments as below
my $outText=`sudo /usr/bin/python /usr/local/bin/tacms/scriptname.py $pNumber $crnNumber`
The scriptname is supposed to process information based on the arguments passed and then give a terminal output which is saved in the variable, outText.
There have been instances where this has failed and I suspect it is a timeout. So how would I increase the timeout period of the backticks
yes, it was the script I was calling so what I did was say that if the results of $outText are null then repeat the whole process else continue...ie
my $outText=`sudo /usr/bin/python /usr/local/bin/tacms/scriptname.py $pNumber $crnNumber`
if ($outText eq ""){
my $outText=`sudo /usr/bin/python /usr/local/bin/tacms/scriptname.py $pNumber $crnNumber`;
//use variable here;
}
else{
//use variable here;
}
At least this way my system would retry at least once before it fails. That was the best solution I could come up with. The python script being called was calling a web service in SOAP so sometimes, it would timeout.
I am executing exec_command in Python. The command is : find -path . -mmin -$time -print. And I am assigning time=100. But while executing the command, exec_command does not picks up the variable value.
or even I do exec_command('echo $time'). It does not picks up the value.
Python and the shell have separate variables. Setting time in your Python script does not magically create a variable called $time that you can then use in shell commands. It especially does not magically create a shell variable on some other computer which is what it appears you're trying to do.
Instead, put the Python value into the string you're passing. For example:
command = 'find -path . -mmin -%d -print' % time
print(command) # shows the exact command that will be executed
exec_command(command)
Be careful with this. It's very easy to create security holes if you don't know what you're doing, especially when you take strings from user input or other data sources you don't control. In this example I've used %d to substitute the value into the command, and this will keep anything but a number from being used, which should be safe.
I have a script which takes in few arguments
./hal --runtest=example
where example = /home/user/example.py
how can I pass these arguments in shell script?
I'm having trouble figuring out what you're asking, but assuming your question is "How can a shell script pass dynamic arguments to a command that happens to be written in Python" and you are using a Bourne-family shell (very likely), the simplest correct answer would be
example=/home/user/example.py
./hal "--runtest=$example"
The shell will resolve the quoting and the script will see --runtest=/home/user/example.py without breaking if you later decide to pass in a path containing spaces.
Take a look a the following:
http://lowfatlinux.com/linux-script-variables.html
It is Bash specific though and as per comments above not sure which shell you're using.
Here you'll find all you need in terms of how to pass an argument to a shell script.