A shell script

Source: Internet
Author: User

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

 

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.