LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 10-24-2008, 11:17 AM   #1
blancs
Member
 
Registered: Mar 2005
Posts: 49

Rep: Reputation: 15
Question please help with simple shell script


Hey guys I am trying to write a simple shell script to ensure that my game server is up and running, and if not open the current screen and run it, then exit the screen. Right now it checks fine, but looks like it hangs up at screen -r. I figured this would be the best place to post this. any ideas?

Code:
#echo "hello"
#AC Check
AC="ac_server"
PGREP="/usr/bin/pgrep"

# find ac_server pid
$PGREP ${AC}

if [ $? -ne 1 ]
then
echo "nothing to be done"
fi

if [ $? -eq 0 ] # if ac not running
then
 # restart ac
echo "hello"
screen -r
/home/ASSAULTCUBE/assaultcube/run.sh
screen -d
echo "restarted server"
fi
 
Old 10-24-2008, 12:11 PM   #2
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 46
It's because "screen -r" reattaches the screen and waits for that screen program to terminate (either by detachment or by the session terminating). It looks like what you want is to run the script inside the screen session; try "screen -X screen /home/ASSAULTCUBE/assaultcube/run.sh".
 
Old 10-29-2008, 02:14 AM   #3
blancs
Member
 
Registered: Mar 2005
Posts: 49

Original Poster
Rep: Reputation: 15
That works; however, now I am trying to build on that. I want to label the screen "ac", and check that the screen is running, and if not create it, then run the command inside that particular screen. This is failing do to my lack of knowledge of screen, i've been over the man page and could not make certain of how to go about doing this correctly. I should have a few screens running, and want to make sure if for some reason the server crashes or what not the correct screen is made, and that the game server is run in the correct screen.

Code:
#AC Check
AC="ac_server"
PGREP="/usr/bin/pgrep"
SCREENS="screen -list|grep ac"

# find ac_server pid
$PGREP ${AC}

if [ $? -ne 1 ]
then
echo "nothing to be done"
fi

if [ $? -eq 0 ] # if ac not running
then
        #check screens, if no screen matchs ac create it and label it ac
        $SCREENS
        if [ $? -eq 0 ]
        then
        echo "made it"
        screen -R -D -S ac
        screen -d
        fi
#there should now be a screen labeled ac by now restarting ac in that screen
screen -X -S ac /home/ASSAULTCUBE/assaultcube/run.sh
echo "restarted server"
fi
 
Old 10-29-2008, 06:34 AM   #4
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 46
"screen -X" runs screen commands (such as "screen" and "detach), not shell commands.
That means you want "screen -X -S ac screen /home/ASS...sh"

"screen -R -D -S ac" still blocks, right? My short experiments says that one can run screen in the background.
Do that, and then you probably want to change "screen -d" into "screen -S ac -X detach".

If that fails (and this is ugly), run
$ screen -S foo sh -c "screen -S foo -X screen sleep inf; screen -S foo -X detach"
(assuming you have GNU sleep; run `sleep inf' and wait forever to test it )
The 'sleep inf' serves to have one window running when sh terminates, such that the screen session persists.

I think that should work. Hint: use grep -q, that turns off the output but still gives you the exit code.
I think you've reversed the polarity on the last-most "if [ $? ... ]". And I'm unsure whether $? is carried between "if [ $? ... ]"s the way you think it is. But hey, if it works...
 
Old 10-30-2008, 02:41 AM   #5
blancs
Member
 
Registered: Mar 2005
Posts: 49

Original Poster
Rep: Reputation: 15
awesome, that works. well i have to clean some logic like you said up though.

But now I've built on that and made it even more difficult. I am now working on a little php control panel for the server. I want to be able to run this little script whenever from the webpage. Without posting all the ugly and messy php code just the the exec() function should show:

exec("ssh -i ~/.ssh/id_rsa user@server /home/ASSAULTCUBE/assaultcube/run.sh");

well the www user runs that command fine. I see the www user login in the auth.log when running the php script, but it doesnt work.

when i drop down and su to the www user then do that command manually i get:
$ ssh -i ~/.ssh/id_rsa user@server /home/ASSAULTCUBE/assaultcube/run.sh
No Sockets found in /var/run/screen/S-assaultcube.

made it
Must be connected to a terminal.
No screen session found.
restarted server


"Must be connected to a terminal." Well i guess this wont work, trying to run screen as the www user sshing in as the actually user running the game server. Maybe I am approaching this the wrong way? Is there a better way of doing this?
 
Old 10-31-2008, 01:20 AM   #6
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 46
> Is there a better way of doing this?

Why do you need screen? Couldn't you just run the AC script in the background? If not, I'd probably try dtach ("the one-screen screen"). If that fails, I'd look into starting screen on a tty; you may want to look at inittab or /etc/event.d, the upstart config thing, and openvt.

hth
 
Old 11-02-2008, 05:10 AM   #7
nishamathew1980
Member
 
Registered: Oct 2008
Posts: 37

Rep: Reputation: 16
If you do not need to open a new window - simple execute the shell command in the same program itself.

Else from ur current script open a new xterm terminal (use the xterm command for that) and invoke a shell script which will do the restarting of your server and then terminate.

I hope this helps.

Linux Archive

Last edited by nishamathew1980; 11-09-2008 at 05:48 AM.
 
  


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
Need help getting started simple simple shell script dhonnoll78 Programming 6 12-17-2007 06:34 PM
simple shell script sharpie Programming 9 06-03-2004 12:47 AM
a simple shell script Warchief Programming 1 07-31-2003 06:01 AM
Very Simple Shell Script cli_man Linux - Software 5 04-24-2003 11:14 AM
Simple C Shell script is not so simple elconde Programming 2 09-17-2001 12:53 AM


All times are GMT -5. The time now is 10:51 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration