Ok, so I am trying to execute the following code:

`#{@daemon_path} --name=#{@app_name} --command=#{@java_path} -- -jar #{jetty_jar} #{@war_path} #{random_port}`
sleep(10) #give war time to error out and die if its going to
`#{@daemon_path} --running --name=#{@app_name}`

The variables values are:

  • @daemon_path = path/to/daemon (correct for my system)
  • @app_name = foobarbazquux
  • @command = path/to/java (also correct for my system)
  • jetty_jar = a method that returns path to a custom jar that stands up a standalone jetty container (tested; works)
  • @war_path = /path/to/helloworld/war (tested in standalone jetty container; works)
  • random_port = a method that returns a random port number between 10000 and 65535 (temporarily changed it to return 8000 or 22 depending if i want to fail to start the war or not)

I get this error (checked commands in bash, they work fine):

Invalid arguments: no command supplied

usage: daemon [options] [--] [cmd arg...]

I fixed the above error by putting quotes around the above commands as follows:

"`#{@daemon_path} --name=#{@app_name} --command=#{@java_path} -- -jar #{jetty_jar} #{@war_path} #{random_port}`"

"`#{@daemon_path} --running --name=#{@app_name}`"

Ok, so after the code executes, i check the output with $? and notice a 0 return code. It should be 1. I ran it in bash and I get a 1. If I manually place in all of the correct values for each variable, it works correctly.

Furthermore, If i execute a script, passing in all of the values like so:

`./daemon_script_file #{@daemon_path} #{@app_name} #{@java_path} #{jetty_jar} #{@war_path} #{random_port}`

to the script daemon script file:

#!/bin/bash
set -x

d_bin=$1
name=$2
cmd=$3
jar=$4
war=$5
port=$6

$d_bin --name=$name --command=$cmd -- -jar $jar $war $port

sleep 10

$d_bin --name=$name --running

result=$?

exit $result

I get the following debug trace output:

+ d_bin=/usr/bin/daemon
+ name=
+ cmd=
+ jar=
+ war=
+ port=
+ /usr/bin/daemon --name= --command= -- -jar
+ sleep 10
+ /usr/bin/daemon --name= --running
+ result=1
+ exit 1
sh: 2: foobarbazquux: not found
invalid file (bad magic number): Exec format error

Does anyone have any clues as to why? Am i doing something incredibly stupid here?

just as a side note, the string:

"#{@daemon_path} --name=#{@app_name} --command=#{@java_path} -- -jar #{jetty_jar} #{@war_path} #{random_port}"

resolves to:

"/usr/bin/daemon  --name=foobarbazquux --command=/usr/java/jdk1.7.0_21/bin/java  -- -jar /home/nterry/JettyContainer-1.0.b4-jar-with-dependencies.jar /home/nterry/helloworld.war 8080"

Which is exactly correct

有帮助吗?

解决方案 2

@Casper: You were correct. @daemon_path ended with an invalid character Thank you so much.

其他提示

It can be a good idea when interpolating strings into commands to use system() with separate arguments instead of backticks. Add a new argument for each non-quoted space you had in the command in backticks. For example:

system(@daemon_path, "--name=#{@app_name}", "--command=#{@java_path}", '--', '-jar', jetty_jar, @war_path, random_port)
sleep(10) #give war time to error out and die if its going to
system(@daemon_path, '--running', "--name=#{@app_name}")

By sending separate arguments to system (which you can't do with backticks), it will ensure all shell special characters such as ;, #, ", etc. get passed directly to the command being run, rather than being interpreted by the shell. This is extremely important for security when any part of the command comes from user input.

It's also good that @Casper suggested checking for invalid characters in those variables, because you probably did not want to pass those characters along to your daemon.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top