I am trying to run screen
in a special way (I am making an unusual script), and it is not working correctly.
My script:
#!/bin/bash
#startserver
set -m
cd /home/USER/SERVER_FOLDER/
screen -Dm -S SERVER java -Xmx768M -Xms768M -jar ./JARFILE.jar $@ &
PID=$!
echo $PID > ./.mc.pid
(sleep 0.5; sudo /usr/bin/oom-priority $PID) &
(wait $PID; startserver_after) &
screen -r $PID.SERVER
/usr/bin/oom-priority
is a coommand I made that sets the priority of the pid to -16
.
startserver_after
is a command I want to run after java
exits.
This isn't working because I cannot resume the screen
. Looking at the screen
manpage:
-D -m This also starts screen in "detached" mode, but doesn't fork a new process. The command exits if the session terminates.
That should mean:
pid
of screen
should be the same as that of java
, however that works.screen
, so I should be able to get to it by screen -r SERVER
(but I can't).When I run the line without the ampersand putting it in the background, it just does nothing until java
exits. There is no output.
If you run screen with -Dm
, it will not return to you command line prompt (the "no fork" idea; that's why it doesn't do anything until your java exits if you have no ampersand). If you run it with the -dm
, it returns to your command line prompt immediately.
By putting the -Dm
in the background, you have caused it to fork and set $!
with the PID of your screen process. So that's good.
You are setting the oom-priority
of your screen (I doubt you want to do that; I think you were aiming at Java). You can get the pid of your child, like this:
pidchild=$(pgrep -P $PID)
Do you really want to startserver_after
after the screen exits? Maybe you want to wait
on the child, as well.
Regarding the screen -r
at the end: It should work, unless you don't have a tty as stdin of your script, or the java has already exited. Try a ps -p $pidchild
immediately after the screen -r
and see if the child is still alive. Also run tty
as the last command of your script, and make sure it does not return Not a tty
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With