Press "Enter" to skip to content

Linux command and PHP

In Operating systems either it is linux, unix or windows system, there are programs which are running on background to keep system functional, these programs are called processes. in linux or unix like systems we have ps command to view processes running on system.
ps command is used to check currently running process status on linux or unix systems. To execute this command just type ps and enter you will have all the currently running process.

$ ps
1175 v0 Is+ 0:00.00 /usr/local/man/ps
1176 v1 Is+ 0:00.00 /usr/bin/prox
63552 0 R+ 0:00.00 ps


If you have something wrong or problem from any process you can kill that process from knowing his process id and type command

$ kill 1175
$ kill 1175 1176 # for multiple killing process

All linux command has many options to display the description that we can view by man command.
Suppose we have to view the process for some specific user.

$ man ps # display command description
$ ps –auwx | grep xyz_user | grep php
xyz_user 1302 0.0 1.0 60804 10140 ?? I 4Apr12 0:01.65 /usr/local/bin/php /usr/local/www/apache/phpCli.php
xyz_user 7471 0.0 1.0 60804 10516 ?? I Wed11AM 0:01.12 /usr/local/bin/php /usr/local/www/apache2/commxyz.php

Above ps command show how can we do pattern searching with the help of grep command, here we search all the currently running process for specific user and having matched string ‘php’ . In result we get PHP files for that specific user running on system along with process id, time and path of that file.

Let’s try now second command to view process, top: its show all running top process like in windows we watch process in “Task manager” process. Simple we can get this by top in linux.

$ top
last pid: 72169; load averages: 0.00, 0.01, 0.00 up 12+22:19:36 10:29:31
59 processes: 1 running, 58 sleeping
CPU: 0.0% user, 0.0% nice, 0.0% system, 0.0% interrupt, 100% idle
Mem: 114M Active, 524M Inact, 216M Wired, 41M Cache, 111M Buf, 56M Free
Swap: 2011M Total, 220K Used, 2011M Free
70726 www 1 45 0 131M 18188K select 1 0:02 0.29% httpd
1025 pgsql 1 44 0 23820K 4444K select 0 2:11 0.00% postgres
1022 pgsql 1 44 0 64132K 16032K select 1 1:17 0.00% postgres
1053 root 1 44 0 129M 12848K select 1 0:26 0.00% httpd

This top command result so many information like last processed id, load average of system, uptime, CPU States, memory and swap. If you watch out the screen, you must view the processes running currently and all his information as PID, username time CPU usage etc.

Now if you want these commands to run from php script, there are many php inbuilt function to execute linux commands like exec, system, passthru, shell_exec , backtick operator (` `) etc. we can try any of these to execute linux command by php.
Suppose we have to execute above process command by command line script

// create a variable
$ps_cmd = "ps -auwx | grep xyz_user| grep php";
// execute by backtick operator
$ps_exec = `$ps_cmd`;
// output
$psexc = exec($ps_cmd, $ps_output, $ps_result);

This will output as same above all the currently running process for specific user and having matched string ‘php’. In case of using backtick operator we get above result. Using exec we get output in two parameter: output and result. output gives an array and result give either command executes successfully or not.


  1. parveen
    parveen April 18, 2012

    Dear Alok,

    I would like to know whether the process is running in background or foreground.

    • Alok Sah
      Alok Sah April 18, 2012

      these all process run on foreground what i describe. but for background we have to set end less session that is done by nohup.

  2. raj
    raj July 24, 2012

    can u post more command that is running in background………….

    if u post such kind of information. i’ll explore your site again…………


Leave a Reply

Your email address will not be published. Required fields are marked *