count number of running process

Posted on 2005-04-14
Last Modified: 2008-01-25
i only learning unix, but have a very basic perl background.

i want to write a perl program that will count the number of processes the user is running?

in my perl file i have

$counter = "ps/wc -w";

but this dont work!

any help?

basically i want to pass the num of process to the $counter variable using a pipe...

Question by:ellandrd
    LVL 8

    Expert Comment

    print `ps/wc -w`

    use backticks
    LVL 8

    Expert Comment

    use backticks but the unix command is wrong
    LVL 8

    Expert Comment

    use this unix command
    ps -u <userid> | grep -v pid | wc -l
    LVL 48

    Expert Comment


    $counter = `ps | grep -cv PID` or die "Can not run ps $!\n";

    LVL 16

    Author Comment

    ive using this but its returning 2:

    $myProcessesCounter = `ps -ef | grep =~/3404/g | wc -l`;

    but if i try this:

    use Proc::ProcessTable;
    $t = new Proc::ProcessTable;
    foreach $p (@{$t->table})      
                      if ($p->uid =~ /3404/g)

    it returns 20, but when i print a  report of my process, it show only 19... somewhere its adding an extra 1...

    am confused, i just need to count number of process running where uid = 3404

    LVL 8

    Accepted Solution

    $myProcessesCounter = `ps -fu3404 | grep -cv PID`;

    ps -e will list all the processes which you donot want.You want the processes specific to the user so use -u option
    LVL 5

    Expert Comment


    a slight modification, replace UID with username:

     $myProcessesCounter = `ps -fu oracle | grep -c oracle`;

    Regard, Nisus

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Suggested Solutions

    Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
    Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
    Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
    In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now