Write daemon with shell script

Source: Internet
Author: User

Recently in doing the server, because it is all-weather operation, the server is not said to be absolutely stable, never crash never crashed anything, so even if the downtime does not matter, the key is to be able to start in time, can not affect the player experience, so

Must have daemons guarding, if the server crashes, immediately reboot to ensure that the entire business can continue to run

My startup script start.sh

#!/bin/sh
killall-9 logserver sleep
1
killall-9 gameserver
mv logserver.log/mnt/share/log/ Logserver.log.bak
mv Gameserver.log/mnt/share/log/gameserver.log.bak
Ulimit-c Unlimited
nohup./ Logserver > Logserver.log 2>&1 & Sleep
5
nohup./gameserver > Gameserver.log 2>&1 &< C9/>sleep 5
Echo Start success

Daemon Script keep.sh

#########################################################################
# File Name:keep.sh
# Author: ma6174
# mail:ma6174@163.com
# Created Time:fri 2015 07:49:46 PM CST
############################### ##########################################
#!/bin/bash
num=1
inum=1
echo $$ while
($num < 5) do
sn= ' ps-ef | grep/gameserver | grep-v grep |awk ' {print $} '
echo $sn
if ["${sn}" = ""]< c14/> #如果为空, which indicates that the process did not start
then let
"inum++"
Echo $iNum
CP gameserver.log/mnt/share/log/gameserver_$ INum.log.bak
rm gameserver.log
nohup./gameserver > Gameserver.log 2>&1 & #后台启动进程
Echo Start OK!
Else
Echo running
fi
5 done


The whole process is

1./start.sh

2./keep.sh > Keep.log 2>&1 &

Note that in keep.sh echo $$ is the output current process number, because after the shell script starts, it is difficult to check its process number, without the process number, it is difficult to kill the shell script to start the corresponding

Process, find the process number from Keep.log, kill-s 9 pid can

./keep.sh > Keep.log 2>&1 & This must be so written, because most of them are connected to Ubuntu through the terminal, directly./keep.sh can not run on the back end, if the terminal is closed, the script corresponding process is automatically killed , the daemon will not exist.


Recently tried again, and found./keep.sh > Keep.log 2>&1 & can actually run in the background, but after you turn off the terminal, you receive the HUP signal, which is still in the

Broken, can not play a effect. With Nohup also prompts a lot of errors, keep.sh this script still can't run, it seems that can not be executed on the terminal./keep.sh > Keep.log 2>&1 & only in screen session mode, for screen you can see the following documents

https://www.ibm.com/developerworks/cn/linux/l-cn-nohup/

Install screen First

Apt-get Install screen

Build a conversation again

Screen-dms Urumchi to

see this conversation again is a success
Screen-list

connected to this conversation.
Screen-r Urumchi

Then execute the keep.sh script
./keep.sh > Keep.log 2>&1 &








Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.