How to make non-interactive Maple exit on assertion failure? - assert

My t.ms file contains:
interface(prettyprint=0): kernelopts(assertlevel=1):
ASSERT(1<1):
ASSERT(2<2):
When I run:
maple -q t.ms; echo $?
I get:
Error, assertion failed
Error, assertion failed
0
When I run:
maple -e 2 -q t.ms; echo $?
I get:
Error, assertion failed
0
I want to get:
Error, assertion failed
4
That is, I want Maple to exit with a nonzero exit status upon the first failing assertion. (I don't care if the exit code is 1 or anything else as long as it's nonzero. I've got the number 4 from the documentation, related to errorbreak) How do I get that?

The documentation doesn't make it very clear that one must use,
`quit`(n)
with name-quotes.
interface(prettyprint=0):
handler:=proc(e::uneval)
local failed;
printf("entered\n"); # remove this when satisfied
failed:=false;
try
if evalb(eval(e)) <> true then
error;
end if;;
catch:
failed:=true;
printf("Error, assertion failed\n");
finally;
if failed then
`quit`(5);
end if;
end try;
true;
end proc:
ASSERT( handler( 1<1 )):
ASSERT( handler( 2<2 )):
Now, saving this as file uh.mpl then using Maple 18.01 for Linux I see,
$ maple18.01 -q -A 2 ~/uh.mpl ; echo $?
entered
Error, assertion failed
5
And if run without the -A 2 then it doesn't run the asserted checks.
[edited] Here below is a slight modification, to process additional arguments as part of the printing.
handler:=proc(e::uneval)
local failed;
printf("entered\n"); # remove this when satisfied
failed:=false;
try
if evalb(eval(e)) <> true then
error;
end if;;
catch:
failed:=true;
printf("Error, assertion failed, %q\n", _rest);
finally;
if failed then
`quit`(5);
end if;
end try;
true;
end proc:

Related

in csh if-statement giving error as "if: Badly formed number."

when i run the below code in my csh shell it is giving Error as if: Badly formed number.
#!/bin/csh
setenv KERNEL_VER `uname -r`
if ( "$KERNEL_VER" ==*"el6"* ) then
echo "Kernel version is OEL6"
elif ( "$KERNEL_VER" == *"el7"* ) then
echo "Kernel version is OEL7"
else
echo "Only OEL6 or OEL7 are supported"
exit 1
fi
i OS is a OEL7 so the control should go inside the "elif" section.
Instead i am getting the below error.
if: Badly formed number.
Any help would be appreciated

Why [ ... ] syntax doesn't work in a Makefile while `test` does?

I am on Linux Mint 19. I am entirely new to Makefiles.
Here is the problematic part:
[ $(shell id --user) -eq 0 ] && ( echo && echo "distrib target has to be run as normal user" && echo && exit 1 )
which throws this error:
[ 1000 -eq 0 ] && ( echo && echo "distrib target has to be run as normal user" && echo && exit 1 )
Makefile:25: recipe for target 'distrib' failed
make: *** [distrib] Error 1
On the contrary, using test command directly proves to be working entirely:
if test $(shell id --user) -eq 0; then ( echo && echo "distrib target has to be run as normal user" && echo && exit 1 ) fi
I want to ask why that is, did I break some Makefile rule?
This doesn't have anything to do with makefiles, it has to do with shell scripting and the difference between using && vs. if in terms of the exit code. You are comparing apples and oranges here.
It's not related to test vs [. If you write the version using [ inside an if statement you'll get the same behavior as you do with test, and if you write the test version with the && model you'll get the same behavior as you do with [.
Run this in your shell:
[ 1000 -eq 0 ] && echo hi
echo $?
Now run this in your shell:
if [ 1000 -eq 0 ]; then echo hi; fi
echo $?
You'll see the former gives a non-0 exit code, while the latter gives a 0 (success) exit code. That's how if works; it "swallows" the exit code of the condition.
Make always looks at the exit code of the shell script to decide if it failed or not.
Generally in make scripting you want to re-arrange your expressions to use || rather than &&. That ensures that if the script exits early it exits with a success code not a failure code. You can write your script like this:
[ $$(id -u) -ne 0 ] || ( echo && echo "distrib target has to be run as normal user" && echo && exit 1 )
Note I use $$(id -u) not $(shell id --user); the recipe is run in the shell already and it's an anti-pattern to use the make shell function in a recipe. Also, the -u option is a POSIX standard option while --user is only available in the GNU utilities version of id.

powershell exit code implentation

Hi I am not a script expert and im looking for help:
I would like to have a exit code, im not sure how this works do...
(Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System).EnableLUA
if value = 0 the exit code should be
Write-Host "Success Message" Exit 0
if the value is 1 the exit code should be
Write-Host "Error Message" Exit 1001
but i have no clue how to implent this, could someone please help me, once i see the script working i can have an idea how this exactly works.
This cause were having MAX Remote,
here is some info:
https://dashboard.systemmonitor.co.uk/dashboard/helpcontents/index.html?script_guide.htm
if we can implent this correctly the dashboard would see the correct exit code.
Thank You
$enablelua = (Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System).EnableLUA
if ($enablelua -eq $0){
Write-Host "UAC Is Enabled"
Exit 0
}
else {
Write-Host "UAC Is Disabled"
Exit 1001
}
doesnt work
$enablelua = (Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System).EnableLUA
if ($enablelua -eq 0){
Write-Host "UAC Is Disabled"
Exit 1010
}
else {
Write-Host "UAC Is Enabled"
Exit 0
}
this works ;) just sharing

Unicorn doesn't pick up changes with new deploy of Rails app (Capistrano, Nginx)

I'm new to deploying, so this is probably a rookie mistake, but here it goes.
I have a Rails 4 app that I'm deploying to a Linux server using a combination of Capistrano, Unicorn, and Nginx. The deploy script runs fine and the app is now reachable at the desired IP, so that's great. The thing is, a) Unicorn doesn't restart upon deployment (at least, the PIDs don't change) and b) not surprisingly, the new changes aren't reflected in the available app. I don't seem to be able to do anything other than completely stopping and restarting unicorn in order to refresh it. If I do this, then the changes are picked up, but this process is obviously not ideal.
Manually, if I run kill -s HUP $UNICORN_PID then the pids of the workers change but not the master, and changes aren't picked up (which, apparently they are supposed to be); using USR2 appears to have no effect on the current processes.
Here's the unicorn init script I'm using, based on suggestions from other stack overflow questions with similar problems:
set -e
USAGE="Usage: $0 <start|stop|restart|upgrade|rotate|force-stop>"
# app settings
USER="deploy"
APP_NAME="app_name"
APP_ROOT="/path/to/$APP_NAME"
ENV="production"
# environment settings
PATH="/home/$USER/.rbenv/shims:/home/$USER/.rbenv/bin:$PATH"
CMD="cd $APP_ROOT/current && bundle exec unicorn -c config/unicorn.rb -E $ENV -D"
PID="$APP_ROOT/shared/pids/unicorn.pid"
OLD_PID="$PID.oldbin"
TIMEOUT=${TIMEOUT-60}
# make sure the app exists
cd $APP_ROOT || exit 1
sig () {
test -s "$PID" && kill -$1 `cat $PID`
}
oldsig () {
test -s $OLD_PID && kill -$1 `cat $OLD_PID`
}
case $1 in
start)
sig 0 && echo >&2 "Already running" && exit 0
echo "Starting $APP_NAME"
su - $USER -c "$CMD"
;;
stop)
echo "Stopping $APP_NAME"
sig QUIT && exit 0
echo >&2 "Not running"
;;
force-stop)
echo "Force stopping $APP_NAME"
sig TERM && exit 0
echo >&2 "Not running"
;;
restart|reload)
sig HUP && echo "reloaded $APP_NAME" && exit 0
echo >&2 "Couldn't reload, starting '$CMD' instead"
run "$CMD"
;;
upgrade)
if sig USR2 && sleep 2 && sig 0 && oldsig QUIT
then
n=$TIMEOUT
while test -s $OLD_PID && test $n -ge 0
do
printf '.' && sleep 1 && n=$(( $n - 1 ))
done
echo
if test $n -lt 0 && test -s $OLD_PID
then
echo >&2 "$OLD_PID still exists after $TIMEOUT seconds"
exit 1
fi
exit 0
fi
echo >&2 "Couldn't upgrade, starting '$CMD' instead"
su - $USER -c "$CMD"
;;
rotate)
sig USR1 && echo rotated logs OK && exit 0
echo >&2 "Couldn't rotate logs" && exit 1
;;
*)
echo >&2 $USAGE
exit 1
;;
esac
Using this script, start and stop work as expected, but reload/restart do nothing (they print the expected output but don't change the running pids) and upgrade fails. According to the error log, it's because the first master is still running (ArgumentError: Already running on PID: $PID).
And here's my unicorn.rb:
app_path = File.expand_path("../..", __FILE__)
working_directory "#{app_path}"
pid "#{app_path}/../../shared/pids/unicorn.pid"
# listen
listen "#{app_path}/../../shared/sockets/unicorn.sock", :backlog => 64
# logging
stderr_path "#{app_path}/../../shared/log/unicorn.stderr.log"
stdout_path "#{app_path}/../../shared/log/unicorn.stdout.log"
# workers
worker_processes 3
# use correct Gemfile on restarts
before_exec do |server|
ENV['BUNDLE_GEMFILE'] = "#{working_directory}/Gemfile"
end
# preload
preload_app false
before_fork do |server, worker|
old_pid = "#{app_path}/shared/pids/unicorn.pid.oldbin"
if File.exists?(old_pid) && server.pid != old_pid
begin
Process.kill("QUIT", File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH
# someone else did our job for us
end
end
end
after_fork do |server, worker|
if defined?(ActiveRecord::Base)
ActiveRecord::Base.establish_connection
end
end
Any help is very much appreciated, thanks!
It is hard to say for certain, since I haven't encountered this particular issue before, but my hunch is that this is your problem:
app_path = File.expand_path("../..", __FILE__)
working_directory "#{app_path}"
Every time you deploy, Capistrano creates a new directory for your app at the location releases/<timestamp>. It then updates a current symlink to point at this latest release directory.
In your case, you may mistakenly be telling Unicorn to use releases/<timestamp> as its working_directory. (SSH to the server and check the contents of unicorn.rb to be certain.) Instead, what you should do is point to current. That way you don't have to stop and cold start unicorn to get it to see the new working directory.
# Since "current" is a symlink to the current release,
# Unicorn will always see the latest code.
working_directory "/var/www/my-app/current"
I suggest rewriting your unicorn.rb so that you aren't using relative paths. Instead hard-code the absolute paths to current and shared. It is OK to do this because those paths will remain the same for every release.
The line
ENV="production"
looks extremely suspicious to me. I suspect that it wants to be
RAILS_ENV="production".
without this won't rails wake up not knowing which environment it is?

Program (nload) runs as a daemon when executed in shell but not in startup/automation script

I would like to run nload (a network throughput monitor) as a daemon on startup (or just automate in general). I can successfully run it as a daemon from the command line by typing this:
nload eth0 >& /dev/null &
Just some background: I modified the nload source code (written in C++) slightly to write to a file in addition to outputting to the screen. I would like to read the throughput values from the file that nload writes to. The reason I am outputting to /dev/null is so that I don't need to worry about the stdout output.
The weird thing is that, when I run it manually it runs just fine as a dameon and I am able to read throughput values from the file. But every attempt at automation has failed. I have tried init.d, rc.local, cron but no luck. The script I wrote to run this in automation is:
#!/bin/bash
echo "starting nload"
/usr/bin/nload eth0 >& /dev/null &
if [ $? -eq 0 ]; then
echo started nload
else
echo failed to start nload
fi
I can confirm that when automated, the script does run, since I tried logging the output. It even logs "started nload", but when I look at the list of processes running nload is not one of them. I can also confirm that when the script is run manually from the shell, nload starts up just fine as a daemon.
Does anyone know what could be preventing this program from running when run via an automated script?
looks like nload is crashing if it's not run from terminal.
viroos#null-linux:~$ cat /etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
strace -o /tmp/nload.trace /usr/bin/nload
exit 0
looks like HOME env var is missing:
viroos#null-linux:~$ cat /tmp/nload.trace
brk(0x1f83000) = 0x1f83000
write(2, "Could not retrieve home director"..., 34) = 34
write(2, "\n", 1) = 1
exit_group(1) = ?
+++ exited with 1 +++
lets fix this:
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
export HOME=/tmp
strace -o /tmp/nload.trace /usr/bin/nload
exit 0
we have another problem:
viroos#null-linux:~$ cat /tmp/nload.trace
read(3, "\32\1\36\0\7\0\1\0\202\0\10\0unknown|unknown term"..., 4096) = 320
read(3, "", 4096) = 0
close(3) = 0
munmap(0x7f23e62c9000, 4096) = 0
ioctl(2, SNDCTL_TMR_TIMEBASE or SNDRV_TIMER_IOCTL_NEXT_DEVICE or TCGETS, 0x7ffedd149010) = -1 ENOTTY (Inappropriate ioctl for device)
ioctl(2, SNDCTL_TMR_TIMEBASE or SNDRV_TIMER_IOCTL_NEXT_DEVICE or TCGETS, 0x7ffedd148fb0) = -1 ENOTTY (Inappropriate ioctl for device)
write(2, "Error opening terminal: unknown."..., 33) = 33
exit_group(1) = ?
+++ exited with 1 +++
I saw you mentioned that you modified nload code but my guess is you haven't removed handling missing termin. You can try further editing nload code or use screen in detached mode:
viroos#null-linux:~$ cat /etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
export HOME=/tmp
screen -S nload -dm /usr/bin/nload
exit 0