Java 服务包装器和附加应用程序命令行参数

发布于 2024-09-05 09:49:34 字数 519 浏览 8 评论 0原文

我目前正在使用 java 服务包装器 来包装我已经安装的 java 应用程序发达。我需要能够通过 java 服务包装器将额外的命令行参数传递到我的应用程序。

假设我的应用程序称为 myapp,并且我已经设置了 java 服务包装器,以便我运行来启动的脚本称为 myapp。我希望能够执行以下操作:

./myapp start Parameter1parameter2

并将这些附加参数传递到我的应用程序中。有什么想法如何做到这一点?我发现谷歌搜索和查看文档只是拉出如何使用命令行参数来设置 java 服务包装器。除了将命令行参数硬编码到您的wrapper.conf 文件中之外,我很难找到有关将命令行参数传递给应用程序的任何内容。

现在我觉得我的选择是采用额外的命令行参数,将它们设置为环境变量并将它们硬编码到wrapper.conf中。但我不想走那条路,希望我忽略了一些事情。

I'm currently using java service wrapper to wrap a java application that I've developed. I'm needing to ability to pass in additional command line parameters to my application through the java service wrapper.

Pretend my app is called myapp and I've setup java service wrapper so that the script I run to start is called myapp. I'd like to be able to do something like this:

./myapp start Parameter1 parameter2

and have those additional parameters get passed into my application. Any ideas how to do this? I'm finding that googling and looking at the documentation is only pulling up how to use command line arguments to setup java service wrapper. I've had difficulty finding anything about passing command line arguments to your application except for having them hard coded in your wrapper.conf file.

Right now I feel like my option is to take the additional command line parameters, set them to environment variables and have those hard coded in the wrapper.conf. I'd prefer not to go down that road though and am hoping I've overlooked something.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光无声 2024-09-12 09:49:34

在包装器的 3.5.2 版本中,我们添加了一种实现您所要求的功能,通过使用“--”在 java 应用程序的参数前面:
https://sourceforge.net/tracker/? func=detail&aid=3017567&group_id=39428&atid=425190

这基本上可以直接调用包装器的二进制文件,但是对于 shell 脚本,您可以通过稍微修改它来轻松实现相同的效果:

打开脚本并在 console()、start()(以及可选的 launchdinternally())中将 command_line 设置为以下内容:

    COMMAND_LINE="$CMDNICE \"$WRAPPER_CMD\" \"$WRAPPER_CONF\" wrapper.syslog.ident=\"$APP_NAME\" wrapper.pidfile=\"$PIDFILE\" wrapper.name=\"$APP_NAME\" wrapper.displayname=\"$APP_LONG_NAME\" $ANCHORPROP $STATUSPROP $LOCKPROP $@"

注意末尾的 $@。

现在,转到脚本的最后,它决定应该调用什么函数(控制台、启动、停止、重新启动等),

在“console”、“start”(和“launchdinternal”)中设置一个班次并将参数从命令行传递到函数:

'console')
    checkUser touchlock $1
    shift
    console $@
    ;;

'start')
    if [ "$DIST_OS" = "macosx" -a -f "/Library/LaunchDaemons/${APP_PLIST}" ] ; then
        macosxstart
    else
        checkUser touchlock $1
        shift
        start $@
    fi
    ;;

..
'launchdinternal')

    shift
    launchdinternal $@
    ;;

之后,您可以像这样调用脚本:

./script start|console -- para1 para2 ...

希望这可以帮助您。

干杯,
基督教

in the 3.5.2 release of the wrapper, we added a possibility to achieve what you are asking for, by using "--" to precede parameters to the java application:
https://sourceforge.net/tracker/?func=detail&aid=3017567&group_id=39428&atid=425190

this is basically working for calling the binary of the wrapper directly, but for shell script you can easily achieve the same by modifying it a bit:

open the script and in the console(), start() ( and optionally launchdinternally()) set the command_line to the following:

    COMMAND_LINE="$CMDNICE \"$WRAPPER_CMD\" \"$WRAPPER_CONF\" wrapper.syslog.ident=\"$APP_NAME\" wrapper.pidfile=\"$PIDFILE\" wrapper.name=\"$APP_NAME\" wrapper.displayname=\"$APP_LONG_NAME\" $ANCHORPROP $STATUSPROP $LOCKPROP $@"

note the $@ at the end.

now, go to quite the end of the script, where it decides what function it should call (console, start, stop, restart, etc.)

in the 'console', 'start' (and 'launchdinternal') set a shift and pass over the parameters from the commandline to the function:

'console')
    checkUser touchlock $1
    shift
    console $@
    ;;

'start')
    if [ "$DIST_OS" = "macosx" -a -f "/Library/LaunchDaemons/${APP_PLIST}" ] ; then
        macosxstart
    else
        checkUser touchlock $1
        shift
        start $@
    fi
    ;;

..
'launchdinternal')

    shift
    launchdinternal $@
    ;;

after that, you can call the script like this:

./script start|console -- para1 para2 ...

hope this helps you out.

cheers,
christian

无畏 2024-09-12 09:49:34

杰克,
快速跟进。在 Java Service Wrapper 的 shell 脚本的 3.5.5 版本中,我们在顶部添加了一个新的 PASS_THROUGH 选项,现在可以立即将命令行参数传递到 JVM。

该脚本使用了 Christian 在 8 月份提到的“--”命令行参数。

干杯,
莱夫

Jake,
Quick followup. In version 3.5.5 of the Java Service Wrapper's shell script, we added a new PASS_THROUGH option towards the top which now makes passing command line arguments through to the JVM work out of the box.

The script makes use of the "--" command line parameter that Christian mentioned back in August.

Cheers,
Leif

相权↑美人 2024-09-12 09:49:34

您可以发布“myapp”脚本的示例吗?虽然不是全部,但了解启动 java 进程的部分是什么样子会很有用。

您想要完成的任务应该很简单,只需确保脚本将 $@ 传递到 java 进程即可。在 Bash 中,$@ 是一个数组,其中包含传递到脚本中的所有参数。如果 java 服务包装器生成的包装器需要您传入“start”作为参数之一,那么您可能需要添加一些脚本来将除第一个参数之外的所有参数传递给 java 过程。

Can you post an example of what the "myapp" script looks like? Not the whole thing but it'd be useful to see what the part that launches the java process looks like.

What you want to accomplish should be as simple as making sure that $@ is passed into the java process by the script. In Bash, $@ is an array that contains all of the arguments passed into the script. If the wrapper produced by java service wrapper needs you to pass in "start" as one of the arguments, then you might have to add a bit of scripting to pass all of the arguments but the first to the java process.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文