0

I am new to php daemons. I am using the below script to fire Daemon.php script. But i am getting error while executing this below bash script via shell

The error is,

exit: 0RETVAL=0: numeric argument required 

Please help me resolve this error

#!/bin/bash
#
#   /etc/init.d/Daemon
#
# Starts the at daemon
#
# chkconfig: 345 95 5
# description: Runs the demonstration daemon.
# processname: Daemon

# Source function library.
#. /etc/init.d/functions

#startup values
log=/var/log/Daemon.log

#verify that the executable exists
test -x /home/godlikemouse/Daemon.php || exit 0RETVAL=0

#
#   Set prog, proc and bin variables.
#
prog="Daemon"
proc=/var/lock/subsys/Daemon
bin=/home/godlikemouse/Daemon.php
start() {
    # Check if Daemon is already running
    if [ ! -f $proc ]; then
        echo -n $"Starting $prog: "
        daemon $bin --log=$log
        RETVAL=$?
        [ $RETVAL -eq 0 ] && touch $proc
        echo
    fi

    return $RETVAL
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $bin
    RETVAL=$?
    [ $RETVAL -eq 0 ] && rm -f $proc
    echo
        return $RETVAL
}

restart() {
    stop
    start
}   

reload() {
    restart
}   

status_at() {
    status $bin
}

case "$1" in
start)
    start
    ;;
stop)
    stop
    ;;
reload|restart)
    restart
    ;;
condrestart)
        if [ -f $proc ]; then
            restart
        fi
        ;;
status)
    status_at
    ;;
*)

echo $"Usage: $0 {start|stop|restart|condrestart|status}"
    exit 1
esac

exit $?
exit $RETVAL
2
  • There is several syntax errors in this script presented. To highlight several: * echo $"Usage (should be just echo "Usage ..." since the string in ".." is not a variable * Double exit statements, the second one for $RETVAL is never ran. * exit 0RETVAL is not the same as exit $RETVAL, and one should just be using exit 1 instead to denote an error, exit 0 means the script ran correctly * $prog is defined but never used Commented Apr 20, 2015 at 16:09
  • @Dwight, whether the string "is a variable" is irrelevant. Red Hat ships init scripts with $"" strings for internationalization purposes. To be sure, if the OP is not building gettext tables for their own init scripts, there's little point to copying that practice here -- but it is something the OP presumably copied from a vendor script, and is not inherently an error in a script starting with #!/bin/bash. Commented Apr 20, 2015 at 18:02

2 Answers 2

1

This line produces the error:

test -x /home/godlikemouse/Daemon.php || exit 0RETVAL=0

If you want to set the the value of RETVAL to 0 you first need to remove the 0 as you can not have variables that start with a number.

Then you remove the value set from the second statement so it will exit in case Daemon.php does not exist.

test -x /home/godlikemouse/Daemon.php || exit

You can also remove the 2 empty echo statements inside the start and stop functions as the do nothing.

There are also errors in the case statement. You need to quote the case options and can remove the last exit block as the exit $? will trigger the exit before.

case "$1" in
"start")
    start
    ;;
"stop")
    stop
    ;;
"reload"|"restart")
    restart
    ;;
"condrestart")
        if [ -f $proc ]; then
            restart
        fi
        ;;
"status")
    status_at
    ;;
Sign up to request clarification or add additional context in comments.

1 Comment

RETVAL=0 && exit makes no sense -- the value of RETVAL is thrown away by the exit.
0

There is several syntax and logic errors in this script presented. To highlight several:

  • echo $"Usage (should be just echo "Usage ..." since the string in ".." is not a variable
  • Double exit statements, the second one for $RETVAL is never ran.
  • exit 0RETVAL is not the same as exit $RETVAL, and one should just be using exit 1 instead to denote an error, exit 0 means the script ran correctly
    • $prog is defined but never used
    • test -x is to check for executable bit enabled in the given path. test -f is safer when testing for a file, test -d safer for testing directories, and test -L is safer when testing symlinks. Combine the test -f and test -x to ensure there is no race conditions or worst. (example: (test -f /home/godlikemouse/Daemon.php && test -x /home/godlikemouse/Daemon.php) || exit 1))

Further details on creating sysv init scripts can be read at http://refspecs.linuxbase.org/LSB_3.0.0/LSB-generic/LSB-generic/iniscrptact.html and bash scripting can be read at http://www.tldp.org/LDP/abs/html/index.html. It is strongly encouraged to learn both before writing system control programs such as init scripts.

10 Comments

$"foo" is internationalization support; it has nothing to do with variables, but uses gettext() to look for a version of the string "foo" translated to the local language.
Also, boo, hiss re: linking to the ABS (which has an unfortunate tendency to showcase antipatterns in its example code without distinguishing them from good practices). mywiki.wooledge.org/BashGuide makes a much stronger effort to be maintained in a way that leads towards good practices, and the bash-hackers wiki (wiki.bash-hackers.org/doku.php) is likewise better maintained.
Oh so anti-pattterns like say bashisms? like the $"foo" sort? Sorry to say but that does not work on every system since bash is not default on every system only most linux desktop/servers and even then that is not reliable since again not every system is going to run bash 4.0. Examples: bash-4.3# tcsh [root@tspadevtapp01al ~]# echo $"fubar" Illegal variable name. -bash-4.3# zsh #busybox behaves the same [root@tspadevtapp01al]~# echo $"fubar" $fubar -bash-4.3# echo $"fubar" fubar LSB, freedesktop.org, and the Open Group all agree bashism are bad. mmkay
Of course $"foo" is a bashism -- but for a "bash scripting" guide to consider all bashisms bad would defeat its purpose; it might as well just be a "POSIX scripting" guide at that point. To be sure, there are bashisms that increase expressiveness enough to substantially increase the set of tools for which shell scripts are appropriate -- consider arrays. To not teach those would be to do the community a disservice.
Getting back on subject -- I was pointing out that a very specific claim you made was incorrect (in the context of a script starting with #!/bin/bash in a question tagged bash), and you've yet to correct it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.