我的脚本由Cron执行,每隔2分钟检查xxx是否正在运行。如果它不在进程中,那么脚本将运行它。问题是有时会多次运行它。
我的问题是如何检测程序运行多次? bash如何检测到pidof函数返回的是多个而不是一个pid?
#!/bin/bash
PID=`pidof xxx`
if [ "$PID" = "" ];
then
cd
cd /home/pi
sudo ./xxx
echo "OK"
else
echo "program is running"
fi
答案 0 :(得分:1)
您可以使用此脚本执行相同操作。它将确保脚本执行一次。
<?xml version = "1.0" ?>
<xs:schema id="ArrayOfBook" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="AuthorId">
<xs:restriction base="xs:string">
<xs:pattern value="[A-Za-z\s']+"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="ArrayOfBook">
<xs:complexType>
<xs:sequence>
<xs:element name="Book" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Author" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Initials" type="AuthorId" minOccurs="1"
maxOccurs="unbounded"/>
<xs:element name="Surname" type="AuthorId" minOccurs="1"
maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Title" type="xs:string"/>
</xs:sequence>
<xs:attribute name="id" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>