#! /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'