我是Perl的新手并尝试编写代码以继续执行操作,直到找到匹配项,否则会出错。
我正在尝试执行命令ps -ef
并检查是否有任何进程以"框"的名义运行,如果没有名为&#34的进程;框&#34 ;发现,我想重复ps -ef
命令执行,直到它获得"框"过程然后继续下一步。
#!/usr/bin/perl -w
open (FH, "ps -ef |") or die "Cannot run the command:$!\n";
$line = "box";
while (<FH>) {
if (/$line/i) { next; }
else {
print ("ps -ef |") or die "Cannot run the command:$!\n");
}
}
close (FH);
答案 0 :(得分:1)
您需要使用无限循环和退出条件。您的条件是ps -ef
命令包含单词 box 。没有必要open
显式管道到该命令,您可以使用qx
operator作为系统调用运行它(与反引号相同)。
use strict;
use warnings;
my $ps;
PS: while (1) {
$ps = qx/ps -ef/;
last PS if $ps =~ m/box/i;
print '.'; # do something in every run
}
print $ps;
正如评论中以及AdrianHHH的答案中所提到的那样:每次运行后sleep
可能有意义,以确保you don't hog the CPU。根据您要查找的流程的性质,来自sleep
builtin的Time::HiRes或usleep
可能是合适的。后者让你的程序休息几毫秒,而内置程序只能使用整秒。如果目标框进程非常快,这些可能会太长。
您的代码说明: 请注意,您的实施中存在一些问题。我将解释你的代码的作用。这是从问题中得出的,评论是我的。
#!/usr/bin/perl -w # open a filehandle to the ps command open (FH, "ps -ef |") or die "Cannot run the command:$!\n"; $line = "box"; # read the output of one run line by line, for each line execute # the block while (<FH>) { # if there is 'box' case-insensitive, skip the line if (/$line/i) { next; } else { # else output (not run!) the command print ("ps -ef |") or die "Cannot run the command:$!\n"); } } close (FH);
一旦它停止,它会经过命令输出的所有行。
答案 1 :(得分:1)
我建议使用pgrep(1)
代替<button id="close">close</button>
<div id="stage">
<div id="container" class="container"></div>
</div>
,因为它可以让您进行更精细的搜索。使用ps
,您可能需要处理以下情况:
ps -ef
很难判断您是否将用户在其用户名中运行的流程与boxford 6254 6211 0 08:23 pts/1 00:00:00 /home/boxford/box --bounding-box=123
进行匹配,该流程在其路径中的某个位置box
,名为box
的进程,或其参数列表中某处box
的进程。
box
允许您仅匹配流程名称或完整路径,特定用户或用户等。当出现名为pgrep
的进程时,以下内容会打印一条消息(这会查找完全匹配,因此它不会匹配名为box
的进程):
dropbox
请注意,use strict;
use warnings;
use 5.010;
use String::ShellQuote qw(shell_quote);
sub is_running {
my ($proc) = @_;
my $cmd = 'pgrep -x ' . shell_quote($proc) . ' >/dev/null 2>&1';
system($cmd);
if ($? == -1) {
die "failed to execute pgrep: $!";
}
elsif ($? & 127) {
die "pgrep died with signal ", $? & 127;
}
else {
my $status = $? >> 8;
die "pgrep exited with error: exit status $status" if $status > 1;
return $status == 0;
}
}
my $proc = 'box';
until ( is_running($proc) ) {
sleep 1;
}
say "Process '$proc' is running";
没有不区分大小写的标志,可能是因为* nix中的进程名称几乎总是小写的。如果您确实需要进行不区分大小写的匹配,可以将pgrep
传递给[Bb][Oo][Xx]
函数。
答案 2 :(得分:0)
14:08:46,592 INFO [stdout] (Periodic Recovery) DEBUG [Periodic Recovery] (HornetQXAResourceWrapper.java:79) - looking for recover at DelegatingSession [session=ClientSessionImpl [name=ca333fe5-417a-11e5-a05f-61cd0d0f3dc4, username=null, closed=false, factory = ClientSessionFactoryImpl [serverLocator=ServerLocatorImpl [initialConnectors=[TransportConfiguration(name=7ea47211-417a-11e5-a05f-61cd0d0f3dc4, factory=org-hornetq-core-remoting-impl-invm-InVMConnectorFactory) ?server-id=0], discoveryGroupConfiguration=null], connectorConfig=TransportConfiguration(name=7ea47211-417a-11e5-a05f-61cd0d0f3dc4, factory=org-hornetq-core-remoting-impl-invm-InVMConnectorFactory) ?server-id=0, backupConfig=null], metaData=()]@1c9794aa] configuration [XARecoveryConfig [transportConfiguration = [TransportConfiguration(name=7ea47211-417a-11e5-a05f-61cd0d0f3dc4, factory=org-hornetq-core-remoting-impl-invm-InVMConnectorFactory) ?server-id=0], discoveryConfiguration = null, username=null, password=****]]
14:08:46,916 DEBUG [org.jboss.as.clustering.web.impl] (http-/xx.xxx.xxx.xx:8080-9) endBatch(): rolling back batch
14:08:46,918 INFO [stdout] (http-/xx.xxx.xxx.xx:8080-9) DEBUG [http-/xx.xxx.xxx.xx:8080-9] (TransactionBatchingManager.java:77) - endBatch(): rolling back batch
14:08:46,923 DEBUG [org.jboss.as.clustering.web.impl] (http-/xx.xxx.xxx.xx:8080-9) endBatch(): rolling back batch
14:08:46,923 INFO [stdout] (http-/xx.xxx.xxx.xx:8080-9) DEBUG [http-/xx.xxx.xxx.xx:8080-9] (TransactionBatchingManager.java:77) - endBatch(): rolling back batch
14:08:46,928 DEBUG [org.jboss.as.clustering.web.impl] (http-/xx.xxx.xxx.xx:8080-9) endBatch(): rolling back batch
14:08:46,929 INFO [stdout] (http-/xx.xxx.xxx.xx:8080-9) DEBUG [http-/xx.xxx.xxx.xx:8080-9] (TransactionBatchingManager.java:77) - endBatch(): rolling back batch
14:08:46,934 DEBUG [org.jboss.as.clustering.web.impl] (http-/xx.xxx.xxx.xx:8080-9) endBatch(): rolling back batch
14:08:46,935 INFO [stdout] (http-/xx.xxx.xxx.xx:8080-9) DEBUG [http-/xx.xxx.xxx.xx:8080-9] (TransactionBatchingManager.java:77) - endBatch(): rolling back batch
14:08:46,940 INFO [stdout] (http-/xx.xxx.xxx.xx:8080-9) INFO [http-/xx.xxx.xxx.xx:8080-9] (ParamFilter.java:32) - Inside Param Filter Class
14:08:46,945 INFO [stdout] (http-/xx.xxx.xxx.xx:8080-9) INFO [http-/xx.xxx.xxx.xx:8080-9] (ParamFilter.java:56) - Suspicious Param name found :false
14:08:46,950 INFO [stdout] (http-/xx.xxx.xxx.xx:8080-9) INFO [http-/xx.xxx.xxx.xx:8080-9] (ParamFilter.java:58) - Request forwarding to further Process
14:08:46,956 DEBUG [org.jboss.as.clustering.web.impl] (http-/xx.xxx.xxx.xx:8080-9) endBatch(): rolling back batch
14:08:46,960 INFO [stdout] (http-/xx.xxx.xxx.xx:8080-9) DEBUG [http-/xx.xxx.xxx.xx:8080-9] (TransactionBatchingManager.java:77) - endBatch(): rolling back batch
14:08:46,967 DEBUG [org.jboss.as.clustering.web.impl] (http-/xx.xxx.xxx.xx:8080-9) endBatch(): rolling back batch
14:08:46,967 INFO [stdout] (http-/xx.xxx.xxx.xx:8080-9) DEBUG [http-/xx.xxx.xxx.xx:8080-9] (TransactionBatchingManager.java:77) - endBatch(): rolling back batch
14:08:46,973 DEBUG [org.jboss.as.clustering.web.impl] (http-/xx.xxx.xxx.xx:8080-9) endBatch(): rolling back batch
14:08:46,974 INFO [stdout] (http-/xx.xxx.xxx.xx:8080-9) DEBUG [http-/xx.xxx.xxx.xx:8080-9] (TransactionBatchingManager.java:77) - endBatch(): rolling back batch
14:08:46,979 DEBUG [org.jboss.as.clustering.web.impl] (http-/xx.xxx.xxx.xx:8080-9) endBatch(): rolling back batch
14:08:46,980 INFO [stdout] (http-/xx.xxx.xxx.xx:8080-9) DEBUG [http-/xx.xxx.xxx.xx:8080-9] (TransactionBatchingManager.java:77) - endBatch(): rolling back batch
14:08:46,986 DEBUG [org.jboss.as.clustering.web.impl] (http-/xx.xxx.xxx.xx:8080-9) endBatch(): rolling back batch
14:08:46,986 INFO [stdout] (http-/xx.xxx.xxx.xx:8080-9) DEBUG [http-/xx.xxx.xxx.xx:8080-9] (TransactionBatchingManager.java:77) - endBatch(): rolling back batch
14:08:46,992 DEBUG [org.jboss.as.clustering.web.impl] (http-/xx.xxx.xxx.xx:8080-9) endBatch(): rolling back batch
14:08:46,993 INFO [stdout] (http-/xx.xxx.xxx.xx:8080-9) DEBUG [http-/xx.xxx.xxx.xx:8080-9] (TransactionBatchingManager.java:77) - endBatch(): rolling back batch
14:08:46,999 INFO [stdout] (http-/xx.xxx.xxx.xx:8080-9) INFO [http-/xx.xxx.xxx.xx:8080-9] (ParamFilter.java:85) - In getParameterMethodNames method
14:08:47,005 INFO [stdout] (http-/xx.xxx.xxx.xx:8080-9) INFO [http-/xx.xxx.xxx.xx:8080-9] (ParamFilter.java:93) - Param : password
14:08:47,010 INFO [stdout] (http-/xx.xxx.xxx.xx:8080-9) INFO [http-/xx.xxx.xxx.xx:8080-9] (ParamFilter.java:93) - Param : username
14:08:47,015 INFO [stdout] (http-/xx.xxx.xxx.xx:8080-9) INFO [http-/xx.xxx.xxx.xx:8080-9] (ParamFilter.java:93) - Param : LoginButton
14:08:47,020 INFO [stdout] (http-/xx.xxx.xxx.xx:8080-9) INFO [http-/xx.xxx.xxx.xx:8080-9] (ParamFilter.java:93) - Param : y
14:08:47,025 INFO [stdout] (http-/xx.xxx.xxx.xx:8080-9) INFO [http-/xx.xxx.xxx.xx:8080-9] (ParamFilter.java:93) - Param : x
14:08:47,030 INFO [stdout] (http-/xx.xxx.xxx.xx:8080-9) DEBUG [http-/xx.xxx.xxx.xx:8080-9] (LoginAction.java:95) - User <PCB_Admin> is from IP <10.252.103.77>, and IIS authentication is <false>
14:08:47,036 DEBUG [org.jboss.jca.core.connectionmanager.pool.strategy.OnePool] (http-/xx.xxx.xxx.xx:8080-9) pcbdb: getConnection(null, WrappedConnectionRequestInfo@23d59f48[userName=cbsmsbldev]) [0/12]
14:08:47,037 INFO [stdout] (http-/xx.xxx.xxx.xx:8080-9) DEBUG [http-/xx.xxx.xxx.xx:8080-9] (SemaphoreArrayListManagedConnectionPool.java:279) - pcbdb: getConnection(null, WrappedConnectionRequestInfo@23d59f48[userName=cbsmsbldev]) [0/12]
14:08:47,045 INFO [stdout] (http-/xx.xxx.xxx.xx:8080-9) DEBUG [http-/xx.xxx.xxx.xx:8080-9] (UmUserDaoImpl.java:928) - Executing SELECT USER_ID, GROUP_ID, STATUS, CREATE_DATE, USER_NAME, PASSWORD, FIRST_NAME, LAST_NAME, EMAIL, LAST_LOGIN_DATE, LAST_PASSWORD_CHANGE_DATE, FAILED_LOGIN_ATTEMPTS, IP_ALLOWED FROM UM_USER WHERE UPPER(USER_NAME) = UPPER(?) and status <> 'C'
14:08:47,062 INFO [stdout] (http-/xx.xxx.xxx.xx:8080-9) DEBUG [http-/xx.xxx.xxx.xx:8080-9] (UmUserGroupDaoImpl.java:814) - Executing SELECT GROUP_ID, GROUP_NAME, DISPLAY_NAME, PASSWORD_AGING, USER_NAME_FORMAT, PASSWORD_FORMAT, MAX_LOGIN_ATTEMPTS, SESSION_TIMEOUT, INTRANET_ACCESS, CHANGE_PASSWORD_FIRST_LOGIN FROM UM_USER_GROUP WHERE GROUP_ID = ?
14:08:47,084 INFO [stdout] (http-/xx.xxx.xxx.xx:8080-9) DEBUG [http-/xx.xxx.xxx.xx:8080-9] (UmTransactionDaoImpl.java:738) - Executing SELECT TRANSACTION_TYPE, NAME, OPERATION, DISPLAY_NAME, URL, TRANSACTION_GROUP_ID, DISPLAY_ORDER FROM UM_TRANSACTION WHERE TRANSACTION_TYPE = ?
14:08:47,095 INFO [stdout] (http-/xx.xxx.xxx.xx:8080-9) DEBUG [http-/xx.xxx.xxx.xx:8080-9] (UmTransactionDaoImpl.java:738) - Executing SELECT TRANSACTION_TYPE, NAME, OPERATION, DISPLAY_NAME, URL, TRANSACTION_GROUP_ID, DISPLAY_ORDER FROM UM_TRANSACTION WHERE TRANSACTION_TYPE = ?
14:08:47,112 INFO [stdout] (http-/xx.xxx.xxx.xx:8080-9) DEBUG [http-/xx.xxx.xxx.xx:8080-9] (Login.java:342) - Total time for user <PCB_Admin> executing login service = 76
14:08:47,119 DEBUG [org.jboss.jca.core.connectionmanager.pool.strategy.OnePool] (http-/xx.xxx.xxx.xx:8080-9) pcbdb: returnConnection(2fc388dc, false) [0/12]
14:08:47,119 INFO [stdout] (http-/xx.xxx.xxx.xx:8080-9) DEBUG [http-/xx.xxx.xxx.xx:8080-9] (SemaphoreArrayListManagedConnectionPool.java:467) - pcbdb: returnConnection(2fc388dc, false) [0/12]
14:08:47,127 DEBUG [org.jboss.jca.core.connectionmanager.pool.strategy.OnePool] (http-/xx.xxx.xxx.xx:8080-9) pcbdb: getConnection(null, WrappedConnectionRequestInfo@23d59f48[userName=cbsmsbldev]) [0/12]
14:08:47,127 INFO [stdout] (http-/xx.xxx.xxx.xx:8080-9) DEBUG [http-/xx.xxx.xxx.xx:8080-9] (SemaphoreArrayListManagedConnectionPool.java:279) - pcbdb: getConnection(null, WrappedConnectionRequestInfo@23d59f48[userName=cbsmsbldev]) [0/12]
14:08:47,137 INFO [stdout] (http-/xx.xxx.xxx.xx:8080-9) DEBUG [http-/xx.xxx.xxx.xx:8080-9] (UmTransactionLogDaoImpl.java:243) - Executing INSERT INTO UM_TRANSACTION_LOG (TRANSACTION_TYPE, KEY_TYPE, KEY_VALUE, USER_NAME, DATE_TIME, STATUS, PARAMETERS) VALUES (?,?,?,?,?,?,?) with values: com.singtel.um.dto.TransactionLogDto: transactionType='3001', keyType='2', keyValue='PCB_Admin', userName='PCB_Admin', dateTime='Thu Aug 13 14:08:47 SGT 2015', status='O', parameters='User ID: PCB_Admin
14:08:47,151 INFO [stdout] (http-/xx.xxx.xxx.xx:8080-9) ', remark='null'
14:08:47,164 INFO [stdout] (http-/xx.xxx.xxx.xx:8080-9) DEBUG [http-/xx.xxx.xxx.xx:8080-9] (UmTransactionLogDaoImpl.java:249) - 1 rows affected (37 ms)
14:08:47,169 DEBUG [org.jboss.jca.core.connectionmanager.pool.strategy.OnePool] (http-/xx.xxx.xxx.xx:8080-9) pcbdb: returnConnection(482cbb51, false) [0/12]
14:08:47,170 INFO [stdout] (http-/xx.xxx.xxx.xx:8080-9) DEBUG [http-/xx.xxx.xxx.xx:8080-9] (SemaphoreArrayListManagedConnectionPool.java:467) - pcbdb: returnConnection(482cbb51, false) [0/12]
14:08:47,177 INFO [stdout] (http-/xx.xxx.xxx.xx:8080-9) DEBUG [http-/xx.xxx.xxx.xx:8080-9] (LoginAction.java:120) - === in else when session is null
14:08:47,182 INFO [stdout] (http-/xx.xxx.xxx.xx:8080-9) UserSession::setUserName::PCB_Admin session::org.apache.catalina.session.StandardSessionFacade@65b015bc
14:08:47,188 INFO [stdout] (http-/xx.xxx.xxx.xx:8080-9) UserSession::validate:session::org.apache.catalina.session.StandardSessionFacade@65b015bc
14:08:47,366 DEBUG [org.hornetq.core.server] (hornetq-expiry-reaper-thread) Cannot expire from jms.queue.ExpiryQueue into jms.queue.ExpiryQueue
14:08:47,367 INFO [stdout] (hornetq-expiry-reaper-thread) DEBUG [hornetq-expiry-reaper-thread] (QueueImpl.java:1470) - Cannot expire from jms.queue.ExpiryQueue into jms.queue.ExpiryQueue
14:08:47,373 INFO [stdout] (http-/xx.xxx.xxx.xx:8080-13) Menu.jsp:session::org.apache.catalina.session.StandardSessionFacade@65b015bc
14:08:56,618 DEBUG [org.jboss.ejb.client.txn] (Periodic Recovery) Send recover request for transaction origin node identifier 1 to EJB receiver with node name bdk-dev-cbsmsbl-app-01
14:09:03,238 DEBUG [org.jboss.jca.core.connectionmanager.pool.strategy.OnePool] (http-/xx.xxx.xxx.xx:8080-12) pcbdb: getConnection(null, WrappedConnectionRequestInfo@23d59f48[userName=cbsmsbldev]) [0/12]
14:09:29,165 DEBUG [org.jboss.jca.core.connectionmanager.pool.idle.IdleRemover] (IdleRemover) Notifying pools, interval: 150000
14:09:39,356 DEBUG [org.jboss.jca.core.connectionmanager.pool.strategy.OnePool] (http-/xx.xxx.xxx.xx:8080-2) pcbdb: getConnection(null, WrappedConnectionRequestInfo@23d59f48[userName=cbsmsbldev]) [0/12]
14:09:52,366 DEBUG [org.jboss.jca.core.connectionmanager.pool.strategy.OnePool] (http-/xx.xxx.xxx.xx:8080-9) pcbdb: getConnection(null, WrappedConnectionRequestInfo@23d59f48[userName=cbsmsbldev]) [0/12]
14:09:54,736 DEBUG [org.jboss.jca.core.connectionmanager.pool.strategy.OnePool] (http-/xx.xxx.xxx.xx:8080-3) pcbdb: getConnection(null, WrappedConnectionRequestInfo@23d59f48[userName=cbsmsbldev]) [0/12]
14:10:08,606 DEBUG [org.jboss.jca.core.connectionmanager.pool.strategy.OnePool] (http-/xx.xxx.xxx.xx:8080-10) pcbdb: getConnection(null, WrappedConnectionRequestInfo@23d59f48[userName=cbsmsbldev]) [0/12]
14:12:41,925 DEBUG [org.jboss.jca.core.connectionmanager.pool.strategy.OnePool] (http-/xx.xxx.xxx.xx:8080-15) pcbdb: getConnection(null, WrappedConnectionRequestInfo@23d59f48[userName=cbsmsbldev]) [0/12]
14:14:21,394 DEBUG [org.jboss.as.clustering.web.impl] (http-/xx.xxx.xxx.xx:8080-16) endBatch(): rolling back batch
命令输出当前进程列表,然后完成。问题中的代码读取输出。假设执行的第一个ps
命令不包含所需行,那么问题代码中没有任何内容可以再次运行ps
命令。
问题中的ps
语句使脚本移至next
输出中的下一行,而不是重新运行命令。可能会为ps
的输出的第一行执行else print ...
之后的next
。结果是,ps
输出中的每一行都运行print
,该行没有所需文本,并且ps
命令没有显着影响。在代码next
中,print ... or die "..."
部分不是很有用,打印不太可能失败,即使它发出or die "..."
消息也是错误的。
也许您应该用以下样式编写一些代码。这里重复运行die
,直到找到想要的文本。请注意ps
调用,如果不这样,脚本将继续运行而不会暂停,可能会阻止实际工作或至少减慢速度。
sleep