如何防止 ps -ef 的输出被截断?

如何防止 ps -ef 的输出被截断?

我必须检查 Linux 中当前正在执行的脚本数量,然后根据数量决定是否再次运行该脚本。问题是类路径太长,因此在执行时ps -ef我无法获取完整路径,只能获取部分路径。

例如:

00:04:43 /usr/java/jdk1.6/bin/java -classpath /usr/share/groovy-2.0.5/lib/groovy-2.0.5.jar -Dscript.name=/usr/share/groovy-2.0.5/bin/groovy -Dprogram.name=groovy -Dgroovy.starter.conf=/usr/share/groovy-2.0.5/conf/groovy-starter.conf -Dgroovy.home=/usr/share/groovy-2.0.5 -Dtools.jar=/usr/java/jdk1.6/lib/tools.jar org.codehaus.groovy.tools.GroovyStarter --main groovy.ui.GroovyMain --conf /usr/share/groovy-2.0.5/conf/groovy-starter.conf --classpath /usr/share/groovy-2.0.5/lib_novabill/mail.jar:/app/kenanfx/site_specific/apits/javaclient/java:/app/kenanfx/bsdm_site/config:/app/kenanfx/FXSecServer-6.0.20120520/server/config:/app/kenanfx/site_specific/apits/javaclient/java/aimw.jar:/app/kenanfx/site_specific/apits/javaclient/java/aimwDocs.jar:/app/kenanfx/site_specific/apits/javaclient/java/aruba.jar:/app/kenanfx/site_specific/apits/javaclient/java/arubaDocs.jar:/app/kenanfx/site_specific/apits/javaclient/java/bali.jar:/app/kenanfx/site_specific/apits/javaclient/java/bali_2tier.jar:/app/kenanfx/site_specific/apits/javaclient/java/baliDocs.jar:/app/kenanfx/site_specific/apits/javaclient/java/batch.jar:/app/kenanfx/site_specific/apits/javaclient/java/batchDocs.jar:/app/kenanfx/site_specific/apits/javaclient/java/cit.jar:/app/kenanfx/site_specific/apits/javaclient/java/citDocs.jar:/app/kenanfx/site_specific/apits/javaclient/java/commons-httpclient-2.0-alpha3.jar:/app/kenanfx/site_specific/apits/javaclient/java/commons-logging-1.0.3.jar:/app/kenanfx/site_specific/apits/javaclient/java/configurator.jar:/app/kenanfx/site_specific/apits/javaclient/java/configuratorDocs.jar:/app/kenanfx/site_specific/apits/javaclient/java/ContentCatalog.jar:/app/kenanfx/site_specific/apits/javaclient/java/ContentCatalogDocs.jar:/app/kenanfx/site_specific/apits/javaclient/java/customerudt.jar:/app/kenanfx/site_specific/apits/javaclient/java/customerudtDocs.jar:/app/kenanfx/site_specific/apits/javaclient/java/ducm.jar:/app/kenanfx/site_specific/apits/javaclient/java/ducmDocs.jar:/app/kenanfx/site_specific/apits/javaclient/java/FXSecBPRules.jar:/app/kenanfx/site_specific/apits/javaclient/java/FXSecFwkClient.jar:/app/kenanfx/site_specific/apits/javaclient/java/FXSecFwkDocs.jar:/app/kenanfx/site_specific/apits/javaclient/java/inventory.jar:/app/kenanfx/site_specific/apits/javaclient/java/inventoryDocs.jar:/app/kenanfx/site_specific/apits/javaclient/java/javaee-api-5.0-2.jar:/app/kenanfx/site_specific/apits/javaclient/java/jaxb-api.jar:/app/kenanfx/site_specific/apits/javaclient/java/jaxb-impl.jar:/app/kenanfx/site_specific/apits/javaclient/java/jaxb-libs.jar:/app/kenanfx/site_specific/apits/javaclient/java/jax-qname.jar:/app/kenanfx/site_specific/apits/javaclient/java/JNITuxedo.jar:/app/kenanfx/site_specific/apits/javaclient/java/JNITuxedo_2tier.jar:/app/kenanfx/site_specific/apits/javaclient/java/namespace.jar:/app/kenanfx/site_specific/apits/javaclient/java/ordering.jar:/app/kenanfx/site_specific/apits/javaclient/java/orderingDocs.jar:/app/kenanfx/site_specific/apits/javaclient/java/PayPerView.jar:/app/kenanfx/site_specific/apits/javaclient/java/PayPerViewDocs.jar:/app/kenanfx/site_specific/apits/javaclient/java/relaxngDatatype.jar:/app/kenanfx/site_specific/apits/javaclient/java/serviceability.jar:/app/kenanfx/site_specific/apits/javaclient/java/serviceabilityDocs.jar:/app/kenanfx/site_specific/apits/javaclient/java/sfq.jar:/app/kenanfx/site_specific/apits/javaclient/java/sfqDocs.jar:/app/kenanfx/site_specific/apits/javaclient/java/shieldware.jar:/app/kenanfx/site_specific/apits/javaclient/java/tuxedo.jar:/app/kenanfx/site_specific/apits/javaclient/java/udt.jar:/app/kenanfx/site_specific/apits/javaclient/java/udtDocs.jar:/app/kenanfx/site_specific/apits/javaclient/java/utility.jar:/app/kenanfx/site_specific/apits/javaclient/java/utilityDocs.jar:/app/kenanfx/site_specific/apits/javaclient/java/workflow.jar:/app/kenanfx/site_specific/apits/javaclient/java/workflowDocs.jar:/app/kenanfx/site_specific/apits/javaclient/java/workpoint.jar:/app/kenanfx/site_specific/apits/javaclient/java/workpointDocs.jar:/app/kenanfx/site_specific/apits/javaclient/java/xsdlib.jar:/app/

那么我怎样才能检索所需的信息?

答案1

如何获取输出中的完整行

要获得完整的输出,您可以ww在命令后添加:

ps -ef ww

这样,输出中的行就不会被截断。
我经常在脚本中使用它来确保输出与正在运行的脚本完全匹配。

例子:

ps -ef

jacob    11173  5876  0 apr19 ?        00:00:08 /usr/bin/unity-scope-

ps -ef ww说:

jacob    11173  5876  0 apr19 ?        Sl     0:08 /usr/bin/unity-scope-loader applications/applications.scope applications/scopes.scope commands.scope

答案2

使用pgrep。例如,pgrep java -a

使用 Google Chrome 进程进行测试:

$ pgrep chrome -a 
3572 /opt/google/chrome/chrome       
3584 /opt/google/chrome/chrome-sandbox /opt/google/chrome/chrome --type=zygote
3586 /opt/google/chrome/chrome --type=zygote
3588 /opt/google/chrome/chrome-sandbox /opt/google/chrome/nacl_helper
3592 /opt/google/chrome/chrome --type=zygote
3689 /opt/google/chrome/chrome --type=gpu-process --channel=3572.0.1702347006 --window-depth=24 --supports-dual-gpus=false --gpu-driver-bug-workarounds=4,25,33,36,48,53,64,66 --disable-accelerated-video-decode --gpu-vendor-id=0x10de --gpu-device-id=0x11a1 --gpu-driver-vendor=NVIDIA --gpu-driver-version=364.16 --v8-natives-passed-by-fd --v8-snapshot-passed-by-fd
3739 /opt/google/chrome/chrome --type=gpu-broker                                                                                                                                                                                                                                                                                                                
3759 /opt/google/chrome/chrome --type=renderer --enable-features=LinuxObsoleteSystemIsEndOfTheLine<LinuxObsoleteSystemIsEndOfTheLine,WebFontsIntervention<WebFontsIntervention --lang=en-GB --force-fieldtrials=AppBannerTriggering/Aggressive/AutofillProfileOrderByFrecency/Enabled/BrotliEncoding/Default/CaptivePortalInterstitial/Enabled/*ChildAccountDetection/Disabled/*ClientSideDetectionModel/Model0/*CrossDevicePromo/28DaySingleProfile/*DataReductionProxyConfigService/Enabled/*ExtensionActionRedesign/Enabled/*ExtensionDeveloperModeWarning/Default/*GFE/Default/InstanceID/Enabled/LinuxObsoleteSystemIsEndOfTheLine/EndOfLine/MaterialDesignDownloads/Enabled/*OmniboxBundledExperimentV1/Unused_2/PasswordBranding/Disabled/*PasswordGeneration/Disabled/*QUIC/EnabledNoId/ReportCertificateErrors/ShowAndPossiblySend/*ResourcePriorities/Launch50pct_11011_1_1_10/SHA1IdentityUIWarning/Enabled/SHA1ToolbarUIJanuary2016/Warning/SHA1ToolbarUIJanuary2017/Error/SSLCommonNameMismatchHandling/Disabled/*SafeBrowsingIncidentReportingService/Default/SafeBrowsingIncidentReportingServiceFeatures/Default/SafeBrowsingUnverifiedDownloads/DisableByParameterMostSbTypes2/SafeBrowsingUpdateFrequency/Default/*UMA-Population-Restrict/normal/*UMA-Uniformity-Trial-1-Percent/group_32/*UMA-Uniformity-Trial-10-Percent/group_08/*UMA-Uniformity-Trial-100-Percent/group_01/*UMA-Uniformity-Trial-20-Percent/group_04/*UMA-Uniformity-Trial-5-Percent/group_13/*UMA-Uniformity-Trial-50-Percent/group_01/*UseDelayAgnosticAEC/DefaultEnabled/WebFontsIntervention/Enabled/WebRTC-LocalIPPermissionCheck/Enabled/WebRTC-PeerConnectionDTLS1.2/Enabled/ --extension-process --enable-webrtc-hw-h264-encoding --enable-offline-auto-reload --enable-offline-auto-reload-visible-only --blink-settings=fetchDeferLateScripts=true,fetchIncreaseFontPriority=true,fetchIncreasePriorities=true --enable-suggestions-with-substring-match --enable-pinch --num-raster-threads=4 --content-image-texture-target
3890 /opt/google/chrome/chrome --type=renderer --enable-features=LinuxObsoleteSystemIsEndOfTheLine<LinuxObsoleteSystemIsEndOfTheLine,WebFontsIntervention<WebFontsIntervention --lang=en-GB --force-fieldtrials=AppBannerTriggering/Aggressive/AutofillProfileOrderByFrecency/Enabled/BrotliEncoding/Default/CaptivePortalInterstitial/Enabled/*ChildAccountDetection/Disabled/*ClientSideDetectionModel/Model0/*CrossDevicePromo/28DaySingleProfile/*DataReductionProxyConfigService/Enabled/*ExtensionActionRedesign/Enabled/*ExtensionDeveloperModeWarning/Default/*GFE/Default/InstanceID/Enabled/LinuxObsoleteSystemIsEndOfTheLine/EndOfLine/MaterialDesignDownloads/Enabled/*OmniboxBundledExperimentV1/Unused_2/PasswordBranding/Disabled/*PasswordGeneration/Disabled/*QUIC/EnabledNoId/ReportCertificateErrors/ShowAndPossiblySend/*ResourcePriorities/Launch50pct_11011_1_1_10/SHA1IdentityUIWarning/Enabled/SHA1ToolbarUIJanuary2016/Warning/SHA1ToolbarUIJanuary2017/Error/SSLCommonNameMismatchHandling/Disabled/*SafeBrowsingIncidentReportingService/Default/SafeBrowsingIncidentReportingServiceFeatures/Default/SafeBrowsingUnverifiedDownloads/DisableByParameterMostSbTypes2/SafeBrowsingUpdateFrequency/Default/*UMA-Population-Restrict/normal/*UMA-Uniformity-Trial-1-Percent/group_32/*UMA-Uniformity-Trial-10-Percent/group_08/*UMA-Uniformity-Trial-100-Percent/group_01/*UMA-Uniformity-Trial-20-Percent/group_04/*UMA-Uniformity-Trial-5-Percent/group_13/*UMA-Uniformity-Trial-50-Percent/group_01/*UseDelayAgnosticAEC/DefaultEnabled/*WebFontsIntervention/Enabled/WebRTC-LocalIPPermissionCheck/Enabled/WebRTC-PeerConnectionDTLS1.2/Enabled/ --extension-process --enable-webrtc-hw-h264-encoding --enable-offline-auto-reload --enable-offline-auto-reload-visible-only --blink-settings=fetchDeferLateScripts=true,fetchIncreaseFontPriority=true,fetchIncreasePriorities=true --enable-suggestions-with-substring-match --enable-pinch --num-raster-threads=4 --content-image-texture-targe

考虑到最后两行的长度,我认为不会有问题。

相关内容