Prevent Ghostscript from writing errors to standard output - stdout

I'm using Ghostscript to rasterize the first page of a PDF file to JPEG. To avoid creating tempfiles, the PDF data is piped into Ghoscripts's stdin and the JPEG is "drained" on stdout. This pipeline works like a charm until GS receives invalid PDF data: Instead of reporting all error messages on stderr as I would have expected, it still writes some of the messages to stdout instead.
To reproduce:
$ echo "Not a PDF" >test.txt
$ /usr/bin/gs -q -sDEVICE=jpeg -dBATCH -dNOPAUSE -dFirstPage=1 -dLastPage=1 \
-r300 -sOutputFile=- - < test.txt 2>/dev/null
Error: /undefined in Not
Operand stack:
Execution stack:
...
Note the 2>/dev/null above does not suppress the error messages. Ghostscript's documentation already warned that writing to stdout requires the -q flag to suppress messages on stdout, but I still seem to be missing something here.

If you want to really silence Ghostscript, modify your command line like this:
/usr/bin/gs -q \
-sstdout=%stderr \
-sDEVICE=jpeg \
-dBATCH \
-dNOPAUSE \
-dLastPage=1 \
-r300 \
-sOutputFile=- \
- < test.txt 2>/dev/null
The addition of -sstdout=%stderr allows Postscript stdout to be redirected, while still allowing drivers to write to stdout. (That patch is in Ghostscript since ~2001, Sept 22.)

Related

Tool for edit lvm.conf file

is there any lvm.conf editor?
I'm trying to set global_filter, use_lvmtad and some other options, currently using sed:
sed -i /etc/lvm/lvm.conf \
-e "s/use_lvmetad = 1/use_lvmetad = 0/" \
-e "/^ *[^#] *global_filter/d" \
-e "/^devices {/a\ global_filter = [ \"r|/dev/drbd.*|\", \"r|/dev/dm-.*|\", \"r|/dev/zd.*|\" ]"
but I don't like this too much, is there any better way?
I found only lvmconfig tool, but it can only display certain configuration sections, and can't edit them.
If you using Ubuntu variant then you can use the LVM GUI to configure and manage the LVM. Refer this link
It seems that augtool is exactly what I was looking for.
These two packages should be enough to proper processing lvm.conf file:
apt install augeas-tools augeas-lenses
Example usage:
augtool print /files/etc/lvm/lvm.conf
And you should get the whole parse tree on stdout.
If the parser fails you won’t get any output, print the error message using:
augtool print /files/etc/lvm/lvm.conf/error
The augtool equivalent for the sed command from the original question:
augtool -s <<EOT
set /files/etc/lvm/lvm.conf/global/dict/use_lvmetad/int "0"
rm /files/etc/lvm/lvm.conf/devices/dict/global_filter
set /files/etc/lvm/lvm.conf/devices/dict/global_filter/list/0/str "r|^/dev/drbd.*|"
set /files/etc/lvm/lvm.conf/devices/dict/global_filter/list/1/str "r|/dev/dm-.*|"
set /files/etc/lvm/lvm.conf/devices/dict/global_filter/list/2/str "r|/dev/zd.*|"
EOT

Imagemagick FailedToExecuteCommand `"gswin32c.exe"

I am testing my file conversions on the test server and I got this error below. I tested the case on my local and it worked without a problem. On the server it gives me some trouble.
FailedToExecuteCommand `"gswin32c.exe" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pngalpha" -dTextAlphaBits=4 -dGraphicsAlphaBits=4 "-r72x72" -g350x350 -dEPSCrop "-sOutputFile=C:/Windows/TEMP/magick-1520z9_ZtBD3O2dg%d" "-fC:/Windows/TEMP/magick-15203XcyD8hja0LJ" "-fC:/Windows/TEMP/magick-1520fnhOiV0JVLNJ"' (Le fichier spécifié est introuvable.) # error/delegate.c/ExternalDelegateCommand/475.convert:
Any help/tips would be greatly appreciated.
EDIT 2:
I also find a link where the imagemagick admin suggests to change the gs path in the delegates.xml. This solution looks promising but the delegate file has one entry for eps to ps conversions as below:
<delegate decode="eps" encode="ps" mode="bi" command=""#PSDelegate#" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 -sDEVICE=ps2write "-sOutputFile=%o" -- "%i""/>
And I can't seem to find how imagemagick refers to the GS location. I added gs to the path variable and/or modified the delegates.xml with the location to fix this.
EDIT 3:
I have a error "no images defined" from the imagemagick convert command as below. Do I need to set up some permissions for gs because I don't find the temporary folders in the Temp directory.These seems to work magically on my local, but hesitates to function on the test server.
convert: Postscript delegate failed [ghostscript library 9.22] -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pngalpha" -dTextAlphaBits=4 -dGraphicsAlphaBits=4 "-r72x72" -g350x350 -dEPSCrop "-sOutputFile=C:/Windows/TEMP/magick-110207SjtG6NIBllO%d" "-fC:/Windows/TEMP/magick-1102057mSI5SPZ919" "-fC:/Windows/TEMP/magick-11020HeIOX7qThedE" -c showpage: (null)' # error/ps.c/ReadPSImage/884.convert: no images definedD:\inetpub\IndependenceAWS\Conversion_DEV_DCHI\78\20190208-121606-797-QSAX2N0OVB\JPG\icam_logo_ToJPG.JPG' # error/convert.c/ConvertImageCommand/3300
For the first problem,
FailedToExecuteCommand `"gswin32c.exe"
Configure the path of the ghostscript bin folder on the system variables and/or configure the delegates.xml which can be found in the folder path C:\Program Files\ImageMagick-7.0.8-Q16
Modify the delegate as below:
delegate decode="eps" encode="ps" mode="bi" command=""C:\Program
Files\gs\gs9.26\bin\gswin64.exe#" -q -dQUIET -dSAFER -dBATCH
-dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 -sDEVICE=ps2write "-sOutputFile=%o" -- "%i""
For the second problem where Imagemagick has problems to access the Temp folder of the C drive.
Add or uncomment the policy for the temporary file path as below
policy domain="resource" name="temporary-path"
value="D:/inetpub/IndependenceAWS/Conversion-Temp"/
Another edit in the same xml as below solves the problems and allows imagemagick to use the new folder for temporary conversions
policy domain="module" rights="read|write" pattern="{PS,PDF,XPS}"
A great thanks to all the people who have commented on my question and helped me solve these issues.
You need to install ghostscript.
You can download it here and install it and the error goes away.
For example, download the 64bit version, and double click it and go by its installation instructions. Then rerun your magick command, the FailedToExecuteCommand "gswin32c.exe" error will go away.
Note: as a comment mentioned: GS 9.26 has some bugs -- try downgrading to 9.23, 9.24 or 9.25. 3, using 9.27 produces neither errors nor output, but after changing to 9.24, all things are fine.
If anyone is having this issue with the MSYS2 build of ImageMagick, you need to install GhostScript through MSYS2: run either of:
on MSYS2 MinGW 64 bits: pacman -S mingw-w64-x86_64-ghostscript
on MSYS2 MinGW 32 bits: pacman -S mingw-w64-i686-ghostscript
Changed gs to full path.
Problem is here....
I know this is an old thread but I'd like to add my experience as it may help someone.
I was getting the same error as you, but for me the issue turned out to be not enough RAM.
This was running on a VM with not much RAM allocated, upgraded to 4GB and everything works great.

clang: Remove comments in preprocessed files?

is there a way to tell clang to remove comment lines when generating preprocessed output?
Apparently, this is a difference between gcc and clang, and I have not found an option to do so
Thanks
Alex
To be more precise: It seems that I have an issue with input from file, please see this sample:
CONTENT="// \$REMOVE BEFORE FLIGHT\$\r\nprintf(\"HelloWorld!\");"
echo "Original:\n$CONTENT"
echo "-------------------------"
echo "From stdin"
echo $CONTENT | cpp -P -E -nostdinc -xc++ -
echo "-------------------------"
echo "From file"
echo $CONTENT > foo.sample
cpp -P -E -nostdinc -xc++ foo.sample
echo "\n What gives?"
Addition: It appears, that the -x Argument is changed. Using -v, I can see that on the command line, actually -x c++ is passed, whereas when using a file as input, it is changed to -x c.
Any idea why?

How can I have grep not print out 'No such file or directory' errors?

I'm grepping through a large pile of code managed by git, and whenever I do a grep, I see piles and piles of messages of the form:
> grep pattern * -R -n
whatever/.git/svn: No such file or directory
Is there any way I can make those lines go away?
You can use the -s or --no-messages flag to suppress errors.
-s, --no-messages suppress error messages
grep pattern * -s -R -n
If you are grepping through a git repository, I'd recommend you use git grep. You don't need to pass in -R or the path.
git grep pattern
That will show all matches from your current directory down.
Errors like that are usually sent to the "standard error" stream, which you can pipe to a file or just make disappear on most commands:
grep pattern * -R -n 2>/dev/null
I have seen that happening several times, with broken links (symlinks that point to files that do not exist), grep tries to search on the target file, which does not exist (hence the correct and accurate error message).
I normally don't bother while doing sysadmin tasks over the console, but from within scripts I do look for text files with "find", and then grep each one:
find /etc -type f -exec grep -nHi -e "widehat" {} \;
Instead of:
grep -nRHi -e "widehat" /etc
I usually don't let grep do the recursion itself. There are usually a few directories you want to skip (.git, .svn...)
You can do clever aliases with stances like that one:
find . \( -name .svn -o -name .git \) -prune -o -type f -exec grep -Hn pattern {} \;
It may seem overkill at first glance, but when you need to filter out some patterns it is quite handy.
Have you tried the -0 option in xargs? Something like this:
ls -r1 | xargs -0 grep 'some text'
Use -I in grep.
Example: grep SEARCH_ME -Irs ~/logs.
I redirect stderr to stdout and then use grep's invert-match (-v) to exclude the warning/error string that I want to hide:
grep -r <pattern> * 2>&1 | grep -v "No such file or directory"
I was getting lots of these errors running "M-x rgrep" from Emacs on Windows with /Git/usr/bin in my PATH. Apparently in that case, M-x rgrep uses "NUL" (the Windows null device) rather than "/dev/null". I fixed the issue by adding this to .emacs:
;; Prevent issues with the Windows null device (NUL)
;; when using cygwin find with rgrep.
(defadvice grep-compute-defaults (around grep-compute-defaults-advice-null-device)
"Use cygwin's /dev/null as the null-device."
(let ((null-device "/dev/null"))
ad-do-it))
(ad-activate 'grep-compute-defaults)
One easy way to make grep return zero status all the time is to use || true
→ echo "Hello" | grep "This won't be found" || true
→ echo $?
0
As you can see the output value here is 0 (Success)

Paperclip.run 'jpegoptim' - problem with params

In my model, that has_attached_file, I have the following code:
before_post_process :jpeg_optim_compression
...
def jpeg_optim_compression
path = attachment.queued_for_write[:original].path
Paperclip.run("jpegoptim", "-o --strip-all #{path}")
end
And locally it works fine giving a following output:
[paperclip] jpegoptim -o --strip-all temporary_path_to_attachment 2>/dev/null
But deployed on the staging server it raises an error:
[paperclip] jpegoptim '-o --strip-all temporary_path_to_attachment' 2>/dev/null
Paperclip::PaperclipCommandLineError (Error while running jpegoptim. Expected return code to be 0 but was 3):
It took me some time to realize that these quotations were the cause (I supposed some file access problem and others). After change to
Paperclip.run("jpegoptim -o --strip-all #{path}")
OR
Paperclip.run("jpegoptim -o --strip-all", "#{path}")
it works. Does anybody have an idea why is that?
Thanks for any suggestions!

Resources