Jump to content

Linux Restarter


Guest Shin Darth

Recommended Posts

Create 2 files in the /mangos/bin folder (where there are "mangos-worldd" and "mangos-realmd"): "mangos-realmd.h" and "mangos-worldd.h".

In the mangos-worldd.h paste this:

while date;  
do MANGOS=`ps -el | grep mangos-worldd`;
if [ -z "$MANGOS" ]; 
then echo restarting server at time at `date +"%m-%d-%H:%M-%S"`>> worldd_restart_log.txt;  
nice -n 20 [b]/opt/MaNGOS/bin/mangos-worldd[/b];  
fi;  
sleep 5;  
done

and, if you need, replace the path "/opt/MaNGOS/bin/mangos-worldd" as your mangos-worldd path.

In the mangos-realmd.h paste this:

while date;  
do MANGOS=`ps -el | grep mangos-realmd`;
if [ -z "$MANGOS" ]; 
then echo restarting server at time at `date +"%m-%d-%H:%M-%S"`>> realmd_restart_log.txt;  
nice -n 20 [b]/opt/MaNGOS/bin/mangos-realmd[/b];  
fi;  
sleep 5;  
done

Now to setting up the server:

./mangos-realmd.h
./mangos-worldd.h

Link to comment
Share on other sites

Some suggestions:

- you aren't sending the process to the background, so there is no need for the if statement (which itself is also broken since the grep will see itself in the processlist and return true)

- shell scripts should generally have a .sh suffix

- the shebang is missing

- please use "while true" or "while :" instead of "while date" (date will generally return 0 so it will work. but it just doesn't make sense since the the loop isn't critically based on a working date binary ;), would be a shame if mangos doesn't start just because someone broke "date")

if all you want is a simple restarter that logs when it restarted, your script wound look fine like this:

#!/bin/bash

Daemon="worldd"

BinDir="/opt/MaNGOS/bin/"
LogDir="/opt/MaNGOS/log/"

if [[ $(ps -o command= -ae|egrep -c "[m]angos-${Daemon}") -gt 0 ]]
then   echo "mangos-${Daemon} is already running, exiting"
exit 1
fi

######### End of Config ##########

while true 
do # loop forever   echo "(re)starting server at $(date +"%m-%d-%H:%M-%S")>> ${LogDir}${Daemon}_restart_log.txt
nice -n 20 ${BinDir}mangos-${Daemon}
sleep 5
done

checking for running processes works a bit better if you have mangos configured to write pidfiles, then you just have to check if the pid in the file is actually running (e.g. by checking /proc/$pid/cmdline)

Link to comment
Share on other sites

Some suggestions:

- you aren't sending the process to the background, so there is no need for the if statement (which itself is also broken since the grep will see itself in the processlist and return true)

- shell scripts should generally have a .sh suffix

- the shebang is missing

- please use "while true" or "while :" instead of "while date" (date will generally return 0 so it will work. but it just doesn't make sense since the the loop isn't critically based on a working date binary ;), would be a shame if mangos doesn't start just because someone broke "date")

if all you want is a simple restarter that logs when it restarted, your script wound look fine like this:

#!/bin/bash

Daemon="worldd"

BinDir="/opt/MaNGOS/bin/"
LogDir="/opt/MaNGOS/log/"

if [[ $(ps -o command= -ae|egrep -c "[m]angos-${Daemon}") -gt 0 ]]
then
  echo "mangos-${Daemon} is already running, exiting"
exit 1
fi

######### End of Config ##########

while true 
do # loop forever
  echo "(re)starting server at $(date +"%m-%d-%H:%M-%S")>> ${LogDir}${Daemon}_restart_log.txt
nice -n 20 ${BinDir}mangos-${Daemon}
sleep 5
done

checking for running processes works a bit better if you have mangos configured to write pidfiles, then you just have to check if the pid in the file is actually running (e.g. by checking /proc/$pid/cmdline)

Question: are all shell scripts in Linux used with the .sh suffix? Or are there other ones I should know about?
Link to comment
Share on other sites

what about a simple

while true; do nice -n 20 ./path/to/bin; sleep 5; date; done

it doesn't look like your script has more functionality your script even has bad hacks and unneeded code

@sturm you don't need to use special file extensions

linux just looks at the first byte of a binary to find out how to handle it, and txt-files with a #!/bin/bla at the beginning, will be executed as /bin/bla my_txt_file

to use file-extensions is just your choice

btw (your question looks a bit like you asked for that)

linux itself also has many shellscripts without the .sh extension for example your daemon-startscripts, many git-tools which you use are also shellscripts, the makefiles are shellscripts too

Link to comment
Share on other sites

what about a simple

while true; do nice -n 20 ./path/to/bin; sleep 5; date; done

it doesn't look like your script has more functionality your script even has bad hacks and unneeded code

I use a simple "while true" oneliner for my daemons since they are running in a screen anyway, but the poster wanted to log restart times, and seemed to want a startscript for it, so I just rewrote his for him. your oneliner prints the date to stdout, but considerung the vast amount of output worldd also prints to stdout when started -> the date output is going to be hard to see

I'm curious about the bad hacks and uneeded code though. I'm never too old to learn something new :-)

Link to comment
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Privacy Policy Terms of Use