LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 08-05-2011, 09:14 AM   #1
redir
Member
 
Registered: May 2004
Location: Virginia USA
Distribution: Debian_Ubuntu_FreeBSD
Posts: 122

Rep: Reputation: 16
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.
 
Old 08-08-2011, 07:36 PM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
screen command leaves pid open on ssh server manwithaplan Linux - Software 2 08-19-2009 05:07 AM
Get PID of screen session started as daemon marsupillami Programming 4 06-08-2009 05:16 PM
kpowersave question - screen black when lid opened durrrty Linux - Laptop and Netbook 2 08-13-2008 09:31 AM
How to execute commands within a subshell opened by another command? plin Programming 3 02-11-2008 08:19 AM
How do I kill a screen from command-line if I know the screen name? CrewXp Linux - Software 6 08-27-2005 10:35 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 02:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration