PHP CLI doesn't use stderr to output errors - stdout

I'm running the PHP CLI through a NSTask in MacOS, but this question is more about the CLI itself.
I'm listening to the stderr pipe, but nothing is output there no matter what file I try to run:
If the file type is not a plain text, stdout sets to ?.
If the file is a php script with errors, the error messages are still printed to stdout.
Is there a switch to the interpreter to handle errors through stderr? Do I have an option to detect errors other than parsing stdout?

The display_errors directive (can be set everywhere) takes optionally the parameter "stderr" for it to report errors to stderr instead of stdout or completely disabled error output. Quoting from the PHP manual entry:
Value "stderr" sends the errors to stderr instead of stdout. The value is available as of PHP 5.2.4.
Alternatively if you're using the commandline interface and you want to output the errors your own you can re-use the command-line nput/output streams:
fwrite(STDERR, 'error message');
Here STDERR is an already opened stream to stderr.
Alternatively if you want to do it just for this script and not in CLI you can open a filed handler to php://stderr and write the error messages there.
$fe = fopen('php://stderr', 'w');
fwrite($fe, 'error message');

If you want the error messages sent by the php interpreter should go to the stderr-pipe, you must set display_errors to stderr

This is required to return from PHP realm into shell environment in order to parse properly error message. You still need to exit(1) or whatever integer in order to return exit status code from PHP to shell.
fwrite(STDERR, 'error message'); //output message into 2> buffer
exit(0x0a); //return error status code to shell
Then, your crontab entry will look like:
30 3 * * * /usr/bin/php /full/path/to/phpFile.php >> /logdir/fullpath/journal.log 2>> /logdir/fullpath/error_journal.log

You can also use file_put_contents() with "php://stderr" to output to standard error, like:
php -r 'file_put_contents("php://stderr", "Hiya, PHP!\n"); echo "Bye!\n";' 1>/dev/null
which outputs "Hiya, PHP!\n" to standard error and nothing to standard output when executed in a Bash shell.

Related

Powershell: Issue redirecting output from error stream when using docker

I am working on a set of build scripts which are called from a ubuntu hosted CI environment. The powershell build script calls jest via react-scripts via npm. Unfortunately jest doesn't use stderr correctly and writes non-errors to the stream.
I have redirected the error stream using 3>&1 2>&1 and this works fine from just powershell core ($LASTEXITCODE is 0 after running, no content from stderr is written in red).
However when I introduce docker via docker run, the build script appears to not behave and outputs the line that should be redirected from the error stream in red (and crashes). i.e. something like: docker : PASS src/App.test.js. Error: Process completed with exit code 1..
Can anyone suggest what I am doing wrong? because I'm a bit stumped. I include the sample PowerShell call below:-
function Invoke-ShellExecutable
{
param (
[ScriptBlock]
$Command
)
$Output = Invoke-Command $Command -NoNewScope | Out-String
if($LASTEXITCODE -ne 0) {
$CmdString = $Command.ToString().Trim()
throw "Process [$($CmdString)] returned a failure status code [$($LASTEXITCODE)]. The process may have outputted details about the error."
}
return $Output
}
Invoke-ShellExecutable {
($env:CI = "true") -and (npm run test:ci)
} 3>&1 2>&1

Vitis IDE invalid arguments

I'm trying to create a boot image with TCL script on xsct console. But getting error. I couldn't find where I made a mistake. I couldn't find any results in Xilinx's documents and other forums.
ERROR: source /home/nmi/Desktop/load.tcl
Invalid arguments, name or processor not specified
setws /home/nmi/workspace
platform active zc702
app create -name fsbl -hw /home/nmi/Desktop/projeHDF/base_zynq_wrapper.xsa proc ps7_cortexa9_0 -os standalone -template {Zynq FSBL}
app build -name fsbl
exec bootgen -arch zynq -image /home/nmi/workspace/FSBL_system/_ide/bootimage/FSBL_system.bif -w -o BOOT.bin
Assuming you've given us the content of the file in question, the initial issue is that we don't know which command is throwing the error. Tcl does know that information and reports it in the error-info stack trace, but the code that runs the source is ignoring that stuff and only saying the immediate error message.
We can fix that.
What we want to do is to wrap some extra code around the outside. If you're using Tcl 8.6 inside there, you can do this easily:
try {
# The original code in the file goes in here
} on error {msg opts} {
puts stderr "ERROR: $msg"
# Let's put a nice double underline next
puts stderr [string repeat "=" [string length "ERROR: $msg"]]
# And now, we print out the error info stack trace
puts stderr [dict get $opts -errorinfo]
# Raise the error again; this isn't the properly right way to do it, but it works
# and the stuff that gets lost is about to be thrown away by the caller
error $msg
}
With 8.5, you'd instead do:
if {[catch {
# The original code in the file goes in here
} msg]} then {
puts stderr "ERROR: $msg"
# Let's put a nice double underline next
puts stderr [string repeat "=" [string length "ERROR: $msg"]]
# And now, we print out the error info stack trace
puts stderr $::errorInfo
# Raise the error again
error $msg
}
Once you know which command is failing, you can look up what the command requires and figure out how things are going wrong.
Here's the full example of what I mean.
try {
setws /home/nmi/workspace
platform active zc702
app create -name fsbl -hw /home/nmi/Desktop/projeHDF/base_zynq_wrapper.xsa proc ps7_cortexa9_0 -os standalone -template {Zynq FSBL}
app build -name fsbl
exec bootgen -arch zynq -image /home/nmi/workspace/FSBL_system/_ide/bootimage/FSBL_system.bif -w -o BOOT.bin
} on error {msg opts} {
puts stderr "ERROR: $msg"
puts stderr [string repeat "=" [string length "ERROR: $msg"]]
puts stderr [dict get $opts -errorinfo]
error $msg
}

Installing a Tcl application as a Windows Service error

I'm trying to install a Tcl program as a service on my Windows machine using the TclDevKit's TclServiceManager. I'm following the guide here step by step and yet I am experiencing a lot of issues.
If I try to use my raw .tcl file to create the service, I get the following error:
Error 1053: The service did not respond to the start or control request in a timely fashion.
I've followed a solution for this issue here to give the program more time to start up before the Service Control Manager terminates it; to no avail.
Then I decided to try and wrap the program using TclApp and see if that worked. Like the guide says, I used the base-tclsvc-win32-ix86.exe prefix file located in my TclDevKit bin directory. Installing the service that way, and then trying to run it resulted in the following error:
Windows could not start the <service name> service on Local Computer.
Error 1067: The process terminated unexpectedly.
There wasn't much information at all that I could find googling this error. The only Stackoverflow post on it is this one. So I tried installing the service manually through the command prompt using <TheProgram>.exe <Service Name> -install and tried running it - still gave me the same error.
Then I tried to see if I could get any useful information by running <TheProgram>.exe <Service Name> -debug and interestingly enough I got the following output:
Debugging <Service Name>.
InitTypes: failed to find the DictUpdateInfo AuxData type
abnormal program termination
Googling InitTypes: failed to find the DictUpdateInfo AuxData type leads me nowhere, however it seems to be something Tcl related.
Finally, if it means anything, the source code for the program I was trying to install as a service is some simple web server code:
proc Serve {chan addr port} {
fconfigure $chan -translation auto -buffering line
set line [gets $chan]
set path [file join . [string trimleft [lindex $line 1] /]]
if {$path == "."} {set path ./index.html}
if {[catch {
set f1 [open $path]
} err]} {
puts $chan "HTTP/1.0 404 Not Found"
} else {
puts $chan "HTTP/1.0 200 OK"
puts $chan "Content-Type: text/html"
puts $chan ""
puts $chan [read $f1]
close $f1
}
close $chan
}
if {![info exists reload]} {
set sk [socket -server Serve 3000]
puts "Server listening on port 3000"
vwait forever
} else {
unset reload
}
To check and see if the source code was the problem, I tried another, simpler example that simply created a file in a particular directory:
set filePath "C:/some/path/here";
set fileName "Test.txt";
set file [open [file join $filePath $fileName] w];
puts $file "Hello, World";
close $file;
Both programs work if you simply source them from tclsh86.exe, but give the above errors if you try and run them as services unwrapped and wrapped respectively.
Any ideas?

Using an expect Script to send the output of a command and store in a file

Hi I am trying to store the output of a command run through a spawn ssh remote window into my local host, I am new to expect and am not able to figure out where I am wrong.
My Code:
#!/bin/bash
while read line
do
/usr/bin/expect <<EOD
spawn ssh mininet#$line
expect "assword:"
send -- "mininet\r"
set output [open "outputfile.txt" "a+"]
expect "mininet#mininet-vm:*"
send -- "ls\r"
set outcome $expect_out(buffer)
send "\r"
puts $output "$outcome"
close $output
expect "mininet#mininet-vm:*"
send -- "exit\r"
interact
expect eof
EOD
done <read_ip.txt
I am getting the error
expect: spawn id exp6 not open
while executing
"expect "mininet#mininet-vm:*""
Please can any body help me on this code.
You have your expect program in a shell heredoc. The shell will expand variables in the heredoc before launching expect. You have to protect expect's variables from the shell.
One way is to use a 'quoted' heredoc, and pass the shell variable to expect through the environment:
#!/bin/bash
export host ## an environment variable
while read host
do
/usr/bin/expect <<'EOD' ## note the quotes here
spawn ssh mininet#$env(host) ## get the value from the environment
expect "assword:"
send -- "mininet\r"
set output [open "outputfile.txt" "a+"]
expect "mininet#mininet-vm:*"
send -- "ls\r"
set outcome $expect_out(buffer)
send "\r"
puts $output "$outcome"
close $output
expect "mininet#mininet-vm:*"
send -- "exit\r"
expect eof ## don't want both "interact" and "expect eof"
EOD
done <read_ip.txt
Putting single quotes around the heredoc terminator means the whole heredoc acts like a single quoted string, and expect's variables are left for expect to handle.
You might also investigate the expect log_file command: you can enable and disable logging at will, much as you are doing manually here.

error while executing lua script for redis server

I was following this simple tutorial to try out a simple lua script
http://www.redisgreen.net/blog/2013/03/18/intro-to-lua-for-redis-programmers/
I created a simple hello.lua file with these lines
local msg = "Hello, world!"
return msg
And i tried running simple command
EVAL "$(cat /Users/rsingh/Downloads/hello.lua)" 0
And i am getting this error
(error) ERR Error compiling script (new function): user_script:1: unexpected symbol near '$'
I can't find what is wrong here and i haven't been able to find someone who has come across this.
Any help would be deeply appreciated.
Your problem comes from the fact you are executing this command from an interactive Redis session:
$ redis-cli
127.0.0.1:6379> EVAL "$(cat /path/to/hello.lua)" 0
(error) ERR Error compiling script (new function): user_script:1: unexpected symbol near '$'
Within such a session you cannot use common command-line tools like cat et al. (here cat is used as a convenient way to get the content of your script in-place). In other words: you send "$(cat /path/to/hello.lua)" as a plain string to Redis, which is not Lua code (of course), and Redis complains.
To execute this sample you must stay in the shell:
$ redis-cli EVAL "$(cat /path/to/hello.lua)" 0
"Hello, world!"
If you are coming from windows and trying to run a lua script you should use this format:
redis-cli --eval script.lua
Run this from the folder where your script is located and it will load a multi line file and execute it.
On the off chance that anyone's come to this from Windows instead, I found I had to do a lot of juggling to achieve the same effect. I had to do this:
echo “local msg = 'Hello, world!'; return msg” > hello.lua
for /F "delims=" %i in ('type hello.lua') do #set cmd=%i
redis-cli eval "%cmd%" 0
.. if you want it saved as a file, although you'll have to have all the content on one line. If you don’t just roll the content into a set command
set cmd=“local msg = 'Hello, world!'; return msg”
redis-cli eval "%cmd%" 0

Resources