LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   how do i script this (a challenge indeed) (https://www.linuxquestions.org/questions/linux-general-1/how-do-i-script-this-a-challenge-indeed-481089/)

mrgreaper 09-06-2006 07:56 PM

how do i script this (a challenge indeed)
 
ok first im a newb but this is not a newb question but please please explain anything fully please

i have been creating a counter strike source server on a linux box and with the help of the amazing people here i have managed to make it do the following

upon execution of its script it ;
1) ftps into my main pc and update any maps waypoints or sound files from my main pc to my server
2) scans all maps and creates a maplist for the ones with bot support
3) updates my website with the current maplist
4) saves a text file with the maps that need bot support added for my reference
5) loads up the seven day stat award calculation
6) Loads into a screen hlstats
7) loads the server into a screen using a random map from a predifend list of good ones

again NONE of that would of been possible with out the good people here and im hoping you guys can help a little further

Ok there are certain maps that seem to freeze up the server, if this happen going into the terminal on the server pc and typing "screen -x css-server" then typing "changelevel" fixes the problem till it happens again but that only helps if im at home and spot it

theres two ways i can think off to fix this neither i can figure out how to do right

1) make a cron job that does the above commands every 4 hours (no idea how exactly to implement that and it would cause a map change to be forced when not needed or cause up to 3 and half hour server downtime if the next map crashes) not ideal
2)create a button on my website (thats hosted on the same box) that executes a script said script could either do
2a) kill the hlstats(named stats) process and css-server (named css-server) process (though the process id always change the screens are named) then execute my start-up script for the server (named abc.sh in my home folder) this idea i like the most as it would refresh the default map to
2b)goes into screen -x css-server and types changelevel followed by return

is what im asking even possible? im guesing 1 is but 2 would be so much nicer specialy 2a

thank you in advance
dave

unSpawn 09-07-2006 03:55 AM

2a) kill the hlstats(named stats) process and css-server (named css-server) process (though the process id always change the screens are named) then execute my start-up script for the server (named abc.sh in my home folder) this idea i like the most as it would refresh the default map to
In it's most crude form something like:
Code:

#!/bin/sh
killall "/lo/ca/tion/of/stats"
killall "/lo/ca/tion/of/css-server"
~/abc.sh
exit 0

But better could be:
Code:

#!/bin/sh
# set -xe
bin_stats="/lo/ca/tion/of/stats"
bin_serv="/lo/ca/tion/of/css-server"
__killCS() {
 echo -en "Killing ${bin_stats//*\//}: "; pkill -KILL -f ${bin_stats} 2>/dev/null || echo FAILED && OK
 echo -en "Killing ${bin_serv//*\//}: "; pkill -KILL -f ${bin_serv} 2>/dev/null || echo FAILED && OK
 pids=( $(pgrep -d " " -f ${bin_stats}) $(pgrep -d " " -f ${bin_serv}) )
 [ "${#pids[@]}" -ne "0" ] && __killCS
}
__killCS && ~/abc.sh
exit 0

Save with a recognisable name like "~/cs_restart", change the /lo/ca/tion/of/ placeholders to be the real paths to the binaries, then run to test. If there's errors, run as "sh -x ~/cs_restart 2>&1 | tee ~/cs_restart.tee" and post contents of ~/cs_restart.tee. If "abc.sh" is the script you start your server with then this restart sequence could be incorporated, leaving you with one script to handle all CS-related tasks. More efficient.

Of course it would be more interesting to find out if we could test the server for a response to automate it and find out why certain maps hang the server and blacklist them if they cause too much trouble.

doc.nice 09-07-2006 05:12 AM

btw, if you want to run the script from a web interface, don't forget to run it using sudo and as the user running your cs-processes (or root, unsecure!!!), otherwise the process can't be killed.

mrgreaper 09-07-2006 11:24 AM

ok not sure have explained it right best bet i guess if i post the scripts used so far
abc.sh the start script
Code:

#!/bin/sh

echo scanning for new maps
./maps
echo done
echo starting stat server up
./hlstats.sh
echo stats up and running
echo starting cstrike server
./css.sh
echo all loaded
screen -x
echo you should see two screens with there ids

maps
Code:

#!/bin/sh

mapdir=/home/mrgreaper/srcds_1/cstrike/maps
maps="$(find $mapdir -type f -name "*.bsp" -exec basename "{}" .bsp \;)"
#echo $maps
navs="$(find $mapdir -type f -name "*.nav" -exec basename "{}" .nav \;)"
#echo $navs

list3="$(echo -e "$maps$navs"|sort -d|uniq -d)"

list4="$(echo -e "$maps$navs"|sort -d|uniq -u)"

echo -e "$list3" > /home/mrgreaper/test.txt
echo -e "$list3" > /home/mrgreaper/srcds_1/cstrike/maplist.txt
echo -e "$list3" > /home/mrgreaper/srcds_1/cstrike/mapcycle.txt
echo -e "$list3" > /home/mrgreaper/srcds_1/cstrike/cfg/mani_admin_plugin/votemaplist.txt
echo -e "$list3" > /srv/www/htdocs/mapcycle.txt
echo -e "$list4" > /home/mrgreaper/share/navsneeded.txt
#echo -e "$list3"

hlstats.sh
Code:

#!/bin/sh

cd /srv/www/htdocs/tr/perl/
screen -A -m -d -S awards perl hlstats-awards.pl
screen -A -m -d -S stats perl hlstats.pl

css.sh
Code:

#!/bin/sh

cd /home/mrgreaper/srcds_1/

array=("cs_office" "de_dust" "cs_office_unlimited_FIXED" "fy_aspire" "cs_piranesi" "de_nuke" "de_deathcookin" "de_rats" "de_spirits" "de_fortknox" "as_crisis" "bestbuy-mds" "cs_assault" "cs_evilisland" "cs italy" "cs_office_unlimited_FIXED" "cs_office_unlimited_FIXED")
random_number=$(($RANDOM%17))

screen -A -m -d -S css-server ./srcds_run -priority 1 -game cstrike -ip 192.168.1.66 -port 27015 -rcon_port 27015 +map ${array[$random_number]} +maxplayers 32 -nomaster -insecure +sv_lan 1

now that creas the two screens so unfortuly no binery locations to aim at
the scripts are in my home folder (i`ve left the update ftp script out as its not really relevent)

is it possible to kill the screens without knowing the id or reserving a process id for each screen

the other problem is i dont know how to run a script from a webpage so kinda need help with that too, "how i want to do it is to have a button on my webpage (which already is secure only friends can get to the page)just click it and the script runs i know i may be asking much of linux and much of you guys just hope you can help

unSpawn 09-08-2006 05:34 AM

now that creas the two screens so unfortuly no binery locations to aim at
"pgrep -lf hlstats-awards" should do I think.


is it possible to kill the screens without knowing the id or reserving a process id for each screen
"screen -list" will list running sessions.

mrgreaper 09-08-2006 02:02 PM

as i say im a newb i really need step by step instructions lol sorry to be a pain

mrgreaper 09-10-2006 06:09 AM

am i asking the imposible ?
or is i something that just cant be explained easierly?

jonaskoelker 09-10-2006 07:50 AM

Here's a fairly easy way to do it: save the following to /somewhere/restart.php
Code:

<? system("/path/to/script/which/restarts/counterstrike"); ?>
<html>
<body>You have now restarted counter-strike.  <a href="restart.php">Restart it again</a>.</body>
</html>

Then, when people visit restart.php, it'll run the script.

If you need to run it suid, it can't be a bash script. It, however, can be a perl script which runs a bash script.

doc.nice 09-10-2006 09:20 AM

NACK, it can be a script if you use sudo instead of setting the suid bit.
and if you replace restart.php by <?php echo $PHP_SELF ?>, you can rename the file without loosing the link...

mrgreaper 09-10-2006 08:27 PM

ok so if i understand this correctly since the script needs to be run as usser mrgreaper not su

i create a page on my website called restart.php with the contents of
Code:

? system("./restart.sh"); ?>
<html>
<body>You have now restarted counter-strike.  <a href="restart.php">Restart it again</a>.</body>
</html>

then in the same folder a script called restart.sh created by mrgreaper so it gets him as owner
Code:

kill x
kill x
cd /home/mrgreaper/
./abc.sh

if thats correct all im stuck on is how to set the screen id`s to a set one
screen -list gives a list of them and shows there ids but there ids constantly change which causes no end of grief

i guess i could have the script as
Code:

screen -x css-server
quit
WHATEVER_COMMAND_TO_DISENGAGE_FROM_SCREEN

ahhhh just cant get my head round it

jonaskoelker 09-11-2006 01:11 AM

To give a screen session a particular name,
Code:

$ screen -S session-name
[screen is terminating]
$

To close it again, do
Code:

$ screen -S session-name -X quit
which will close all programs inside the screen session and terminate the session.

Also, to run a bash script as a particular user, you need to wrap it inside something, because bash doesn't like suid. Here's a perl script to wrap it in:
Code:

#!/usr/bin/perl
$ENV{'PATH'} = '';
exit system "/path/to/my/shell/script.sh";

Make this script suid (chmod u+s perl-script.pl), and run this from php instead of the bash script.

Or, as has been suggested, use sudo. Can't really help you with that, since I don't have experience with it.

doc.nice 09-11-2006 02:57 AM

Quote:

created by mrgreaper so it gets him as owner
just to mention: you can change the owner of a file with "chown"
to make the script executable, you must set the X bit in the file permissions, this can be done with "chmod 755 restart.sh" (allow owner to read, write and execute, all others may only read and execute it.)


simply edit your sudo file (as root) with the command visudo and create an entry to allow the user running apache (or everyone if you like) to call your script and not having to specify its password ("NOPASSWORD" statement) (see man sudo for the syntax of the sudo file).
Then execute "sudo restart.sh" with the php system command.
if you don't use sudo, keep in mind that apache normally is running as nobody or wwwrun user or something like that, not as your user or even root!

mrgreaper 09-11-2006 10:28 AM

ok i cant understand above sorry perhaps im not bright enough, i tried to try it and hoped any error messages would help me to fix it but i got this when i go to restart.php

Code:

Warning: Unknown(/srv/www/htdocs/restart.php): failed to open stream: Permission denied in Unknown on line 0

Warning: (null)() [function.include]: Failed opening '/srv/www/htdocs/restart.php' for inclusion (include_path='/usr/share/php') in Unknown on line 0

these are my scripts and there locations

/srv/www/htdocs/restart.php
Code:

? system("./restart.sh"); ?>
<html>
<body>You have now restarted the server.</body>
</html>

/srv/www/htdocs/restart.sh
Code:

#!/usr/bin/perl
$ENV{'PATH'} = '';
exit system "/home/mrgreaper/restart.sh";

/home/mrgraper/restart.sh
Code:

$ screen -S css-server -X quit
$ screen -S stats -X
cd /home/mrgreaper/
./abc.sh


logged in as mrgreaper i chmod 755 the last restart.sh and attempted to run it from the console type programme built into linux
it says
./restart.sh: line1: $: command not found
./restart.sh: line2: $: command not found

if i do screen -x
it lists
31864 stats (detached)
31868 css-server (detached)

doc.nice 09-11-2006 10:54 AM

first problem: use <?php ...?> instead of <? ... ?> (some configs don't like the abbrev. php start tag)
$ problem: the dollar sign was only there to represent a shell prompt,
like your "mrgreaper@yourmachine:~#" command prompt. So simply delete "$ " from these lines...

your turn again ;)

mrgreaper 09-11-2006 11:21 AM

Quote:

Originally Posted by doc.nice
first problem: use <?php ...?> instead of <? ... ?> (some configs don't like the abbrev. php start tag)
$ problem: the dollar sign was only there to represent a shell prompt,
like your "mrgreaper@yourmachine:~#" command prompt. So simply delete "$ " from these lines...

your turn again ;)

the first bit i dont follow <? ... ?> isnt in any of my scripts?

deleted the $ symbols
it closed one screen left the second open and said please specify command
is it trying to close them too fast ? is it possible to inserert a 5 second pause between the commands ?

your help is greatly greatly appreciated


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