Check whether the Script test. Sh is running.

Source: Internet
Author: User

#! /Bin/bash
# The preceding line must exist. Otherwise, the PS displays bash rather than test. Sh.

Isprocrunning (){
Echo "Check $1 is running or not"
Local p = 'ps-C $1-O pid = '# If multiple processes with the same name are running, $ P for example: "1111 2222"
For PID in $ P; do
If [$ PID-ne $]; then # Check whether the PS process is itself. This is required.
Echo "another proc $ PID is running"
Return 1
Else
Echo "Local PID is $ PID"
Fi
Done
Return 0
}

 

# Test
Isprocrunning 'basename $0'
If [$? -EQ 1]; then
Echo "is running"
Sleep 10
Else
Sleep 20
Fi

 

 

//////////////////////////////////////// /////////////////////////

# Another SectionCode,

Isprocrunning (){

Local user = 'whoam'

Echo 'ps-u root-f | grep $1 | grep-V grep'

Local procs = 'ps-U $ user-f | grep $1 | grep-V grep | gawk
'{Print $2 }''

For PID in $ procs; do

If [$ PID-ne $]; then

Echo $ PID

Return 1;

Fi

Done

Return 0

}

 

# However, the execution fails in the following way, and the script is always running.

# The reason is that the system starts a sub-shell to execute the functions in '', and the command parameters of the PS parent-child shell process are identical!

AA = 'iscrunning script_name'

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.