Daemon in Linux

Source: Internet
Author: User

Daemon in Linux

The role of resident processes in Linux cannot be ignored, but the problems here cannot be ignored. It is reasonable to design how to start the process, how to end the process, and how to restart the process after the process fails. The following is an example of a shell-controlled php resident process.

It is easier to understand the following:

#! /Bin/sh
# Filename test. sh
# Absolute location of the file, which does not change with the execution directory
Cd $ (cd "$ (dirname" $0 ")"; pwd)
Readonly path = $ (pwd )/
File = $1;
RunFile = "$ {path} data/$ {file}. run"
DieFile = "$ {path} data/$ {file}. die"
Readonly file = "$ {path }$ {file}. php"
If [! -F "$ file"]; then
Echo "please select a exists file"
Elif [! -F "$ RunFile"]; then
# If the RunFile file does not exist, it indicates that the process does not exist. The following processes start echo $ >$ {RunFile}
While true
Do
If [! -F $ DieFile]; then
# If the DieFile file does not exist, the program continues to run. Otherwise, the program enters else and exits.
/Usr/bin/php-f $ {file}
Touch $ RunFile
Sleep 1
Else
# Clear RunFile and DieFile and exit if DieFile exists.
If rm-rf $ RunFile & rm-rf $ DieFile; then
Exit
Fi
Fi
Done
Else
# Here we try to start this process when RunFile exists.
Oldpid = 'cat $ runfile'
Newpid = 'ps aux | grep "process. sh $1" | grep-v grep | grep "$ oldpid" | awk '{print $2 }''
If [[$ oldpid-eq $ newpid]; then
# If the process number in RunFile is the same as the target process number in running, it indicates that everything is safe.
Echo "the process is runing now"
Exit
Else
# If the process number in RunFile does not match the running target process, it indicates that the process is faulty. You can directly Delete the RunFile and end the running process.
Echo "error situation, kill the run process and delete the run file"
Ps aux | grep "process. sh $1 "| grep-v 'grep' | awk' {print $2} '| grep-v $ | xargs -- no-run-if-empty kill
If [$? -Eq 0]; then
Rm-f $ RunFile
Else
Echo $?> $ {Path}/data/error
Fi
Fi
Fi

There are only a few points to emphasize:

  1. I use this shell to call the php program, which has no limitations. Here I want to explain this method for running resident processes.
  2. When RunFile exists, but the process number cannot be used to kill the process (that is, the place where the red else executes), be sure to 'grep-v $ ', the function is to filter out the currently running processes. Otherwise, all processes will be killed and the subsequent processes will not be executed.
  3. Another thing to note is about Automatic restart.

Automatic Restart can be placed in the crontab and executed every other time.

Crontab-e
# Open the current user's calendar and add Mode

# The calendar has five asterisks, f1, f2, f3, f4, f5,
# F1 indicates the minute, f2 indicates the day, f3 indicates the day, f4 indicates the month, and f5 indicates the day of the week.
# * Indicates every minute, hour, day, month, week, and day. */n indicates every n minutes, hour, or hour.

*/2 */root/test. sh
# Once every 2 minutes

This completes a complete resident process function. to terminate a process, you only need to perform touch $ {DieFile} in the corresponding directory.

This article permanently updates the link address:

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.