Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing shell script with system() returns 256. What does that mean?

Tags:

I've written a shell script to soft-restart HAProxy (reverse proxy). Executing the script from the shell works. But I want a daemon to execute the script. That doesn't work. system() returns 256. I have no clue what that might mean.

#!/bin/sh # save previous state mv /home/haproxy/haproxy.cfg /home/haproxy/haproxy.cfg.old mv /var/run/haproxy.pid /var/run/haproxy.pid.old  cp /tmp/haproxy.cfg.new /home/haproxy/haproxy.cfg kill -TTOU $(cat /var/run/haproxy.pid.old) if haproxy -p /var/run/haproxy.pid -f /home/haproxy/haproxy.cfg; then   kill -USR1 $(cat /var/run/haproxy.pid.old)   rm -f /var/run/haproxy.pid.old   exit 1 else   kill -TTIN $(cat /var/run/haproxy.pid.old)   rm -f /var/run/haproxy.pid   mv /var/run/haproxy.pid.old /var/run/haproxy.pid   mv /home/haproxy/haproxy.cfg /home/haproxy/haproxy.cfg.err   mv /home/haproxy/haproxy.cfg.old /home/haproxy/haproxy.cfg   exit 0 fi 

HAProxy is executed with user haproxy. My daemon has it's own user too. Both run with sudo.

Any hints?

like image 318
Jan Deinhard Avatar asked Sep 17 '10 14:09

Jan Deinhard


People also ask

What is exit status 256 Linux?

The Exit code [256] is a general error and not an error displayed by PowerCenter. The failure occurs when shell script has $PMSourceFileDir t on UNIX, while there is no shortcut defined as an environment variable for the same.

What does a shell script return?

return command is used to exit from a shell function. It takes a parameter [N], if N is mentioned then it returns [N] and if N is not mentioned then it returns the status of the last command executed within the function or script.

What is the return value of a shell script after successful execution?

Every command returns an exit status (sometimes referred to as a return status or exit code). A successful command returns a 0, while an unsuccessful one returns a non-zero value that usually can be interpreted as an error code.


2 Answers

According to this and that, Perl's system() returns exit values multiplied by 256. So it's actually exiting with 1. It seems this happens in C too.

like image 75
Skilldrick Avatar answered Oct 13 '22 20:10

Skilldrick


Unless system returns -1 its return value is of the same format as the status value from the wait family of system calls (man 2 wait). There are macros to help you interpret this status:

man 3 wait 

Lists these macros and what they tell you.

like image 42
nategoose Avatar answered Oct 13 '22 20:10

nategoose