2

First of all sorry for my bad english and i am new at linux.

When i do this, it can write process id to pid file

start () {
    echo -n "Starting someserver.jar: "

    java -jar /home/someserver/someserver.jar &
    echo $! > /home/hcserver/hcserver.pid
}

And know i am using this command but it can't write to pid file

start () {
    echo -n "Starting someserver.jar: "

    screen -S someserver java -jar /home/someserver/someserver.jar
    echo $! > /home/someserver/someserver.pid
}

How can i get process id or fix this?

Edit:

root@server:/home/someserver# ps -ef | grep java
root      4332     1  0 22:03 ?         SCREEN -S someserver java -jar /home/someserver/someserver.jar
root      4333  4332  0 22:03 pts/0     java -jar /home/someserver/someserver.jar
root      4363  3222  0 22:04 pts/3     grep java

I want to write 4333 process id to pid file

0

2 Answers 2

2

Not 100% sure I understand your question, but lets try :

$! would work from the parentshell, not from inside the child process

$! is for a background process, screen is not exactly a background process

Also screen -S will not detach the screen, you probably need more screen options you probably want something like

screen -dmS

also from inside the screen you can try $$ and $BASHPID

you could find the pid of the screen using

screen -list | grep someserver | cut -f1 -d'.' | sed 's/\W//g'

killing the pid of the screen should also kill whatever is running inside the screen

if you re sure the pid of the detached screen is not enough for you try using pgrep to get the pid of the java stuff

0
4

So, you're trying to replace a script that starts a long-running java command in the background with one that runs it in screen?

I think there are actually two problems with your screen attempt. First off, that screen command won't exit/background until the java command is finished (or an interactive user types some things). Secondly, $! isn't really going to do quite what you want here.

In order to get screen to background properly, use:

screen -d -m -S someserver java -jar /home/someserver/someserver.jar

The -d -m combo will start screen in a detached (background) state, which is generally what you want for a startup script. It will automatically exit and cleanup if the java process exits.

So, now that you've got screen properly backgrounded at startup time, you still need to get the pid of the process so that you can control (or at least stop) it. But it's actually fairly tricky to capture that pid. I think you could do it by some combination of parsing screen -ls output, parsing /proc/ data and/or parsing ps output, but it would be tricky. By far, the easiest thing to do, instead, is to use screen itself to control that process. That is, in your stop() routine, instead of killing the pid of the java proccess, do something like this:

stop () {
  echo -n "Stopping someserver.jar: "
  screen -X -S someserver quit
}

That will tell the screen session (which you've already identified via -S earlier) to kill all processes under its control and exit.

If you really need the process id of the child process, something like this should do the trick:

cd /var/run/screen/S-$(whoami)
parentpid=$(ls *.someserver | cut -d. -f1)
pid=$(ps --ppid $parentpid -ho pid)
3
  • this is good solution and i get what you mean but still need process id and i also using monit and it needs process id
    – C.T
    Nov 22, 2014 at 20:40
  • I added a horrible trick to get the child pid
    – freiheit
    Nov 22, 2014 at 20:52
  • Thank you man you gave me to much idea and i appreciate it you are the best
    – C.T
    Nov 22, 2014 at 21:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .