I have learned shell scripts before, but it has never been used in my work. To prevent repeated startup of PHP scripts, I need to write a shell,
What I learned was finally used. I wrote this script with the knowledge points I remembered. I want to record the remarks here to help you recall some related knowledge points.
The script content is as follows:
*************************************
#! /Bin/bash // declare the shell to use
Below is the remarks file, which is a good habit.
# Run the third-party sharing script Shell
# Copyright (c) 2012, jiehun.com.cn Inc. All Rights Reserved
# @ Author 77500933@qq.com
# @ Date 2012-7-18
# @ Version 1.0
// Set related variables
Filepath = '/home/zhanghaozheng/workspace/ihapn/trunk/tools/cooperation/crondshare. php'
Scriptname = 'crondshare. php'
Logfile = '/tmp/crond_assist.log'
Sitepath = '/home/zhanghaozheng/jiehun'
Declare-I logfilelines = 0 // defines the integer variable
# Overwrite if the maximum number of files exceeds
Declare-I maxfileline = 2000 // defines the integer variable
// Receives the parameters passed by the sh command.
# Maximum number of rows allowed Parameter Control
If [-n "$1"]; then // determines whether the parameter is null.
Maxfileline = $1
Fi
// Define a function
Function getlogfilelines ()
{
File = $1 // accept parameter 1
If [-F "$ file"]; then // determines whether the file exists
Echo 'cat "$ file" | WC-l' // obtain the number of file lines
Else
Echo 0
Fi
}
Logfilelines = 'getlogfilelines "$ logfile" '; // call the function and receive the returned value to logfilelines.
# If the script is started, it will not start again
S = 'ps aux | grep "$ scriptname" '; // query the script name in the process to determine whether the script process is started.
Mark = 'echo $ S | grep "/usr/bin/PHP" '// check whether it is a PHP process
Query = 'CD "$ sitepath" & amp;/usr/bin/PHP tools/start. php crondshare = "$ filepath"-d'
If ["$ mark" = ""]; then // if the process does not exist, start the process
# Prevent the record file from being too large
If ["$ logfilelines"-GT "$ maxfileline"]; then compares the two parameters
Eval $ query> "$ logfile" 2> & 1 // execution string
Else
Eval $ query> "$ logfile" 2> & 1 // execution string
Fi
Else
Echo 'script is runing'> "$ logfile"
Fi
Exit 0 // exit after normal execution