开发者

BASH: Shell Script as Init Script

开发者 https://www.devze.com 2023-03-18 04:02 出处:网络
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.

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 inits开发者_运维知识库cript, 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).


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:

#!/bin/bash
( 
  { daemon ${basedir}/$prog && success || failure ; } &>/dev/null
) &
exit 0

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.


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:

screen -dmS arbitarySessionName /path/to/script/launchService.sh

To look at the process:

# screen -r arbitrarySessionName

Hope you find this useful, gl!

0

精彩评论

暂无评论...
验证码 换一张
取 消