BASH:Shell 脚本作为初始化脚本
我有一个 shell 脚本,它调用 java jar 文件并运行应用程序。没有办法解决这个问题,所以我必须利用我所拥有的一切。
当您执行此 shell 脚本时,它会输出应用程序状态并坐在那里(几乎是一个控制台);因此,当程序发生问题时,它会更新屏幕。这就像任何正常的非守护进程/后台进程一样。摆脱它的唯一方法是 ctrl-c,然后它会完全结束该过程。我确实知道我可以通过执行 path_to_shell_script/script.sh &
来解决这个问题,这将为我的会话提供背景(如果我想注销,我可以使用 nohup)。
我的问题是,我只是不知道如何将此脚本放入初始化脚本中。我已经编写了大部分初始化脚本,但是当我尝试对其进行守护进程时,它不起作用。我几乎已经让它工作了,但是,当我运行 initscript 时,它实际上跨越了脚本上相同的“控制台”,并且只是坐在那里直到我按下 ctrl-c。这是有问题的行:
daemon ${basedir}/$prog &&成功|| failure
问题是我无法仅将守护进程 ${basedir}/$prog 部分置于后台,我认为这就是我遇到问题的地方。有人成功地为 shell 脚本创建了 init 脚本吗?此外,这个 shell 脚本不可守护进程(您可以将其置于后台,但底层程序不支持守护进程选项,否则我只会让应用程序完成所有工作)。
I have a shell script that calls a java jar file and runs an application. There's no way around this, so I have to work with what I have.
When you execute this shell script, it outputs the application status and just sits there (pretty much a console); so when something happens to the program it updates the screen. This is like with any normal non daemonized/backgrounded process. Only way to get out of it is ctrl-c, which then ends the process altogether. I do know that I could get around this by doing path_to_shell_script/script.sh &
, which would background it for my session (I could use nohup if I wanted to logout).
My issue is, I just don't know how to put this script into a init script. I have most of the init script written, but when I try to daemonize it, it doesn't work. I've almost got it working, however, when i run the initscript, it actually spans the same "console" on the script, and just sits there until i hit ctrl-c. Here's the line in question:
daemon ${basedir}/$prog && success || failure
The problem is that I can't background just the daemon ${basedir}/$prog
part and I think that's where I'm running into the issue. Has anyone been successful at creating an init script FOR a shell script? Also this shell script is not daemonizable (you can background it, but the underlying program does not support a daemonize option, or else I would have just let the application do all the work).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要打开一个子 shell 来执行它。它还有助于将其输出重定向到文件,或者至少是 /dev/null。
类似的东西:
它的工作原理如下(列表)&在后台子 shell 中。 { list } 是一个组命令,它在这里用于捕获命令的所有输出并将其发送到 /dev/null。
You need to open a subshell to execute it. It also help to redirect its output to a file, or at least /dev/null.
Something like:
It work as follows ( list ) & in a background subshell. { list } is a group command, it's used here to capture all the output of your commands and send it to /dev/null.
我已经成功地使用最初分离的屏幕会话来运行半衰期服务器和我的自定义“尾部日志文件”bash 脚本等内容。
在后台启动某些操作:
查看进程:
希望您发现这很有用,gl!
I have had success with initially detached screen sessions for running things like the half life server and my custom "tail logfile " bash scripts.
To start something in the background:
To look at the process:
Hope you find this useful, gl!