[Tutorial] Server Restarter for linux
#1


Seeing this topic has been brought up countless of times, i decided to do a little tutorial.


Then lets make it restart itself!
It's actually simple, and easy to do.

You don't really need anything specific, except for some basic unix knowledge.
This guide is written for debian based distro, but should however work just fine on almost any other.



First of all, open up putty(or any similar terminal)
Go to the dir where you want your autorestart script placed(doing this tutorial i used the sa-mp server dir)

Now, open up your favorite editor (such as pico)

Use the following script, and modify it to fit your directories.
Код:
#!/bin/sh

sampdir="/home/samp"
process=`ps auxw | grep samp022svr | grep -v grep | awk '{print $11}'`

if [ -z "$process" ]; then

 echo "sa-mp server not running, restarting it."
 cd "$sampdir"
 nohup ./samp022svr &
 echo ""

fi
At the current stage, it should look similar to this:



If it does, and you're sure that the dir's are replaced with yours, then hit ctrl-x and save it (the file name is important, we need to use it later on, so choose something practical )

Alright, so far so good, lets continue.
It's around the time to finish it off, but first you need to figure out how often you'd like crontab to check whether the server is running or not. (in this tutorial I'm checking every 60 seconds. )

Now it's time to put the following line into a crontab using the command "crontab -e".
Код:
*/1 * * * * /home/samp/autorestart.sh >/dev/null 2>&1
But! before throwing it in, you need to change the "/home/samp/autorestart.sh" with the dir to where you placed the script you saved before.
You also need to decide what interval it should check whether the server is running or not, its pretty simple, let me show you a few examples on how to change the interval below.

Check every 1 minute
Код:
*/1 * * * * /home/samp/autorestart.sh >/dev/null 2>&1
Check every 2 minutes
Код:
*/2 * * * * /home/samp/autorestart.sh >/dev/null 2>&1
Check every 5 minutes
Код:
*/5 * * * * /home/samp/autorestart.sh >/dev/null 2>&1
As you can see, we're basically just changing the "*/1 *" (to learn more about crontab, ****** it).

Your crontab should look similar to this now:


If it does that, hit ctrl-x and save it.
Then you're done, and you're ready to test it out, by shutting down your server, and see how it reacts.



If you can't get it to work, simply just ask, and I'll try to help you out the best i can.
Enjoy

Reply
#2

Quote:
Originally Posted by Westie
I'm afraid that you're a bloody failure, because you use pico/nano. :P I don't get why you're using ps for your process ID, why don't you just use pidof instead?
Haha
Pico/nano simply is the best.

ps/pidof, just a matter of what you like, both would do just perfect. :P
Reply
#3

Quote:
Originally Posted by Westie
(I use vi btw)

BTW, is the $11 a typo? Shouldn't it be $1 with ps?
Ah yes, a typo.
My bad.
Reply
#4

I did:

*/1 * * * * /var/removed/ptp/autorestart/ohsamp.sh >/dev/null 2>&1

in putty and i got:

E486: Pattern not found: 1 * * * *

i using windows nano but i dont know if i supposed to connect with that, i am using putty, please help
Reply
#5

still get E486: Pattern not found: * * * * *

i did
*/ * * * * /var/www/vhosts/ptpserver.com/private/ptp/autorestart/ohsamp.sh >/dev/null 2>&1
then i tryed
1*/ * * * * /var/www/vhosts/ptpserver.com/private/ptp/autorestart/ohsamp.sh >/dev/null 2>&1
same error, and i cant use:
*/ * * * * /var/www/vhosts/ptpserver.com/private/ptp/autorestart/ohsamp.sh >/dev/null 2>&1
or
* * * * /var/www/vhosts/ptpserver.com/private/ptp/autorestart/ohsamp.sh >/dev/null 2>&1
putty doesn't read **** and it goes to /var/www/....

(using putty)
Reply
#6

Quote:
Originally Posted by Trucecity
* * * * * /var/www/vhosts/ptpserver.com/private/ptp/autorestart/ohsamp.sh >/dev/null 2>&1
putty doesn't read * * * * * and it goes to /var/www/....

(using putty)
same, i get:
E846: pattern not found: var
and * * * * * disapears and shows this var/www/vhosts/ptpserver.com/private/ptp/autorestart/ohsamp.sh >/dev/null 2>&1 and gives me errors... a bit weird on my end.

i will get my friend on this and see if he gets the same errors and i will report back here.
Reply
#7

Which distro are you using?
Reply
#8

Quote:
Originally Posted by Bry4n
Which distro are you using?
w/ crapy plesk...
CentOS 5
Operating system Linux 2.6.9-023stab048.4-enterprise
Reply
#9

anyone?
Reply
#10

try to create the crontab in a new, empty file

then do
Код:
crontab new_empty_file
now check with
Код:
crontab -d
if your cron occurs there

hint: you may use
Код:
-u someuser
with the crontab command to setup it for another user
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)