LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Open Screen as Daemon Will not Execute Command Unless Screen is Opened (https://www.linuxquestions.org/questions/linux-software-2/open-screen-as-daemon-will-not-execute-command-unless-screen-is-opened-895741/)

redir 08-05-2011 09:14 AM

Open Screen as Daemon Will not Execute Command Unless Screen is Opened
 
Hey all. Interesting problem here. In a nut shell, I have a program called OpenSim which is a 3D virtual world emulator. My ISP provides me with dynamic DNS so I have written a program that compares my external IP to the IP in a file called Regions.ini that OpenSim uses to go out to the Internet. If the IP's are different then I need to change the entry in the Regions.ini file and reboot OpenSim. The program works except for the fail safe I put in in case OpenSim crashes during shutdown which happens one in a while.

Here is the code:

Quote:

#!/bin/bash
EXCEED_TIME="N"
SERVICE='mono'
#Get the external IP address
ip=`wget -O - -q icanhazip.com`
echo $ip

#Get the IP address from the Regions.ini file
RegionsIP=`awk '{ if ($1 == "ExternalHostName") { print $3;exit } }' /home/jmckenna/opensim7.1/bin/Regions/Regions.ini`
echo $RegionsIP

function start_opensim {
echo "Starting OpenSim"
screen -S OPENSIM -X stuff 'mono /home/jmckenna/opensim7.1/bin/OpenSim.exe
'
}

function stop_opensim {
echo "stopping opensim"
screen -S OPENSIM -r -X stuff 'shutdown
'
}

function stop_screen {
echo "stopping screen"
screen -S OPENSIM -r -X stuff 'exit
'
}

function start_screen {
screen -dmS OPENSIM
screen -S OPENSIM -r -X stuff 'clear
'
}

function kill_mono{
ps -ef | sed -n '/mono/{/grep/!p;}' | awk '{print$2}' | xargs -i kill {}
}

#Compare the two and proceed as necessary
if [ "$ip" == "$RegionsIP" ]
then
echo "ip is equal to RegionsIP no need to do anything"
#DO NOTHING
else
echo "ip is NOT equal to RegionsIP edit Regions.ini"
#Edit the Regions.ini file with the new IP
sed -i "s/$RegionsIP/$ip/g" /home/jmckenna/opensim7.1/bin/Regions/Regions.ini

#Is OpenSim running?
if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
echo "Deteched OpenSim Running"
stop_opensim
EXCEED_TIME="Y"
for i in {1..12}; do
sleep 10
if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
echo "OpenSim still running, need to wait longer"
else
echo "OpenSim stopped lets restart it"
EXCEED_TIME="N"
break
fi
done
if [ $EXCEED_TIME = "Y" ]; then
echo "OpenSim didnt shutdown in time - assumed a crash"
kill_mono
stop_screen
start_screen
fi
start_opensim
fi
fi
As you can see, if the wait time is exceeded we assume a crash. Then I kill mono, start a new screen and then run 'mono OpenSim'. This is where the problem is. If I start screen as a daemon it will not run a command. In testing from the command line I run:

screen -dmS OPENSIM

I put this command in to try and 'wake' up screen

screen -S OPENSIM -r -X stuff 'clear
'

But it will not work until I open the screen

Screen -r

Then detach and I can run the command from the command line.

It's almost like screen is asleep until you actually go into it.

If I run screen without -d then I will enter the screen, is there a way to get back out and then run the commands from the CL? Something like this maybe?

Quote:

function start_screen {
screen -mS OPENSIM
screen -d ###will this work?
screen -S OPENSIM -r -X stuff 'clear
'
}


Thanks.

Regards.

theNbomr 08-08-2011 07:36 PM

I was plagued by the same problem for years. I contrived a monumental hack involving creation of a virtual X server in which to launch an xterm so that the screen session could be launched in an attached state. It turns out there is the little known '-p 0' option that solves the whole issue.
Code:

NL=$(echo -ne '\015')
$ screen -S TEST1 -d -m
$ screen -S TEST1 -p 0 -X stuff "ls -lash"$NL

--- rod.


All times are GMT -5. The time now is 05:26 PM.