Why shell_exec executing more than 1 process? - linux

I dont understand why there is more than 1 process when I run run.php once from a browser
In the PHP code, I have the following:
run.php
<?php
shell_exec("php theprocess.php > /dev/null 2>&1 &");
?>
theprocess.php
<?php
$z = 1;
while ($z <= 20) {
echo $z . "\n";
$z++;
sleep(3);
}
?>
I execute run.php from the browser (eg: http://localhost/run.php)
Then I typed: ps aux | grep php
username# [~]# ps aux | grep php
username 27272 0.0 1.5 89504 64468 ? R 17:33 0:00 php theprocess.php
username 27274 0.0 1.2 89504 49872 ? R 17:33 0:00 php theprocess.php
username 27276 0.0 0.6 89504 28676 ? R 17:33 0:00 php theprocess.php
username 27278 0.0 0.0 22280 3704 ? R 17:33 0:00 php theprocess.php
username 27280 0.0 0.0 1940 508 ? S+ 17:33 0:00 grep php
I dont understand why is it showing more than 1 theprocess.php process?
Also why it still running at the background? it should terminate theprocess.php finish the task. How can that be done?

I have fixed the problem!
When running script from a webpage, it does not treat as PHP cli.
Replace
shell_exec("/usr/bin/php theprocess.php > /dev/null 2>&1 &");
To
shell_exec("/usr/bin/php-cli theprocess.php > /dev/null 2>&1 &");
I no longer have multiple procress running in the background.

Related

How to narrow down the process listing output below UNIX/LINUX

FROM:
noaccess 7491 6817 0 Jul 19 ? 281:19 /usr/java/bin/java -
server -Xmx128m -XX:+UseParallelGC -XX:ParallelGCThreads=4
oracle 1715 11577 0 Nov 23 ? 32:33
/appl/oracle/product/10.2.0/db_1/jdk/bin/java -server -Xmx256M -
XX:MaxPermSize=
virtuo 21844 21814 0 17:27:27 pts/3 0:00 grep java
TO:
noaccess 7491 6817 0 Jul 19 ? 281:19 /usr/java/bin/java -
server -Xmx128m -XX:+UseParallelGC -XX:ParallelGCThreads=4
oracle 1715 11577 0 Nov 23 ? 32:33
/appl/oracle/product/10.2.0/db_1/jdk/bin/java -server -Xmx256M -
XX:MaxPermSize=
I need to remove virtuo 21844 21814 0 17:27:27 pts/3 0:00 grep java line
I am really new to UNIX/LINUX command.
Assuming that your FROM output was generated from an 'X' command, so:
X | sed '$d'
That will remove the last line of the output. To remove a specific line, you could use something like:
X | sed '/regex/d'
That will remove every line that has a match on the passed regex.

pgrep in linux only identify 15 bytes proc name

in my Linux , while I run shell : ps -ef | grep Speed , I got the following :
myid 143410 49092 0 10:21 pts/12 00:00:00 ./OutSpeedyOrderConnection
myid 145492 49053 0 10:35 pts/11 00:00:00 ./SpeedyOrderConnection
That means , the pid of these 2 process are 143410 and 145492 .
Then I run shell : pgrep -l Speed , I got the following :
143410 OutSpeedyOrderC
145492 SpeedyOrderConn
and I run shell : pgrep OutSpeedyOrderC , I got :
143410
pgrep OutSpeedyOrderCo will get nothing !!!!!
look like pgrep will only identify 15 bytes of processname ,
anything I can do to get the right answer while I run
pgrep OutSpeedyOrderConnection ?!

not running cron with casperjs

sh_run.sh file..
#!/bin/bash
PHANTOMJS_EXECUTABLE=/usr/local/bin/phantomjs /usr/local/bin/casperjs /home/test/html/run/site_check.js
Setting in crontab..
# cat /etc/crontab
45 0 * * * root sh /home/test/html/run/sh_run.sh
but casperjs not running..
crontab status is Rl..
what is Rl ??
# ps ax|grep phantomjs
28155 ? Rl 0:18 /usr/local/bin/phantomjs /usr/local/casperjs/bin/bootstrap.js --casper-path=/usr/local/casperjs --cli /home/test/html/run/site_check.js
of course..
# casperjs site_check.js
is running..
add comment...
# sh sh_run.sh &
# ps ax|grep phantomjs
1625 pts/0 Sl 0:01 /usr/local/bin/phantomjs /usr/local/casperjs/bin/bootstrap.js --casper-path=/usr/local/casperjs --cli /home/test/html/run/site_check.js
is running...
if ps status is Sl, data change.. (namely running..)
but ps status is Rl, data not change.. run by cron, status is always Rl.
Rl status does not change.
what problem? plz..
help me..

find all users who has over N process and echo them in shell

I'm writing script is ksh. Need to find all users who has over N process and echo them in shell.
N reads from ksh.
I know what I should use ps -elf but how parse it, find users with >N process and create array with them. Little troubles with array in ksh. Please help. Maybe simple solutions can help me instead of array creating.
s162103#helios:/home/s162103$ ps -elf
0 S s153308 4804 1 0 40 20 ? 17666 ? 11:03:08 ? 0:00 /usr/lib/gnome-settings-daemon --oa
0 S root 6546 1327 0 40 20 ? 3584 ? 11:14:06 ? 0:00 /usr/dt/bin/dtlogin -daemon -udpPor
0 S webservd 15646 485 0 40 20 ? 2823 ? п╪п╟я─я ? 0:23 /opt/csw/sbin/nginx
0 S s153246 6746 6741 0 40 20 ? 18103 ? 11:14:21 ? 0:00 iiim-panel --disable-crash-dialog
0 S s153246 23512 1 0 40 20 ? 17903 ? 09:34:08 ? 0:00 /usr/bin/metacity --sm-client-id=de
0 S root 933 861 0 40 20 ? 5234 ? 10:26:59 ? 0:00 dtgreet -display :14
...
when i type
ps -elf | awk '{a[$3]++;}END{for(i in a)if (a[i]>N)print i, a[i];}' N=1
s162103#helios:/home/s162103$ ps -elf | awk '{a[$3]++;}END{for(i in a)if (a[i]>N)print i, a[i];}' N=1
root 118
/usr/sadm/lib/smc/bin/smcboot 3
/usr/lib/autofs/automountd 2
/opt/SUNWut/lib/utsessiond 2
nasty 31
dima 22
/opt/oracle/product/Oracle_WT1/ohs/ 7
/usr/lib/ssh/sshd 5
/usr/bin/bash 11
that is not user /usr/sadm/lib/smc/bin/smcboot
there is last field in ps -elf ,not user
Something like this(assuming 3rd field of your ps command gives the user id):
ps -elf |
awk '{a[$3]++;}
END {
for(i in a)
if (a[i]>N)
print i, a[i];
}' N=3
The minimal ps command you want to use here is ps -eo user=. This will just print the username for each process and nothing more. The rest can be done with awk:
ps -eo user= |
awk -v max=3 '{ n[$1]++ }
END {
for (user in n)
if (n[user]>max)
print n[user], user
}'
I recommend to put the count in the first column for readability.
read number
ps -elfo user= | sort | uniq -c | while read count user
do
if (( $count > $number ))
then
echo $user
fi
done
That is best solution and it works!

linux and perl script input

Alright, some of you might have noticed I've been working on this problem off and on for about 3 weeks. I cannot figure out for the life of me whats going on.. Below is the perl script that saves input from USB card reader which acts like a keyboard. The machine is an embedded system running off of a compact flash drive, using voyage linux.
use strict;
use Time::Local;
open(MATCH,'swipe_match.txt');
my #matches = <MATCH>;
close(MATCH);
my $error = qr/[+%;]E\?/;
while(1) {
my $text = <STDIN>;
my $text1 = <STDIN>;
my $text2 = <STDIN>;
if (($text && $text1 && $text2) or ($text && $text1) or $text) {
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = localtime(); $year += 1900;
$mon+=1;
my $timestamp = "$mon/$mday/$year $hour:$min:$sec";
chomp $text;
chomp $text1;
chomp $text2;
# my $matched = 0;
# foreach my $test (#matches) {
# chomp $test;
# $matched = 1 if ($text =~ /$test/i);
# }
# if ($matched) {
# system("aplay /SWIPE/good.wav >/dev/null 2>/dev/null");
# } else {
# system("aplay /SWIPE/bad.wav >/dev/null 2>/dev/null");
# }
# write out the swipe even if its bad...
open(LOG,'>>/DATA/SWIPES.TXT');
print LOG $text."\t".$text1."\t".$text2."\t".$timestamp."\n";
close(LOG);
if ($text =~ $error or $text1 =~ $error or $text2 =~ $error) {
system("aplay /SWIPE/bad.wav >/dev/null 2>/dev/null");
}
else {
system("aplay /SWIPE/good.wav >/dev/null 2>/dev/null");
}
}
}
exit;
I did not write this script, and the person who did write it, is long gone. Currently I have 2 machines. One of which is working and the other is the one Im trying to get to work. Im trying to figure out how this script gets input (on the machine that is working). I can open the log file /DATA/SWIPES.TXT and view the actual swipes. Currently there are no running processes on the machine that would affect the script, here are the processes:
PID TTY STAT TIME COMMAND
1 ? Ss 0:29 init [2]
2 ? S< 0:00 [kthreadd]
3 ? S< 0:04 [ksoftirqd/0]
4 ? S< 3:21 [events/0]
5 ? S< 0:00 [khelper]
44 ? S< 0:00 [kblockd/0]
46 ? S< 0:00 [kacpid]
47 ? S< 0:00 [kacpi_notify]
94 ? S< 0:00 [kseriod]
134 ? S 0:00 [pdflush]
135 ? S 0:06 [pdflush]
136 ? S< 0:00 [kswapd0]
137 ? S< 0:00 [aio/0]
138 ? S< 0:00 [nfsiod]
795 ? S< 0:00 [kpsmoused]
800 ? S< 0:00 [rpciod/0]
1627 ? S< 0:00 [ksuspend_usbd]
1631 ? S< 0:00 [khubd]
1646 ? S< 0:00 [ata/0]
1648 ? S< 0:00 [ata_aux]
1794 ? S<s 0:00 udevd --daemon
2913 ? Ss 0:00 pump -i eth0
2979 ? Ss 0:00 /usr/sbin/rpc.idmapd
3060 ? S 0:01 /usr/sbin/syslogd --no-forward
3083 ? Ss 0:00 /usr/sbin/sshd
3099 ? S 0:00 /usr/sbin/inetutils-inetd
3122 ? Ss 0:00 /usr/sbin/pptpd
3138 ? Ss 0:00 /usr/sbin/cron
3149 ? SLs 0:33 /usr/sbin/watchdog
3167 tty2 Ss+ 0:00 /sbin/mingetty tty2
3169 tty3 Ss+ 0:00 /sbin/rungetty tty3
3170 tty4 Ss+ 0:00 /sbin/rungetty tty4
3173 tty5 Ss+ 0:00 /sbin/getty 38400 tty5
3175 tty6 Ss+ 0:00 /sbin/getty 38400 tty6
15677 ? Ss 0:00 sshd: root#pts/0
15679 pts/0 Ss 0:00 -bash
15710 ? Z 0:00 [watchdog] <defunct>
15711 pts/0 R+ 0:00 ps x
So, from there, I don't know where to go. Can anyone give me any suggestions or hints as to how this script is actually receiving the input from the usb reader. Also, it some how receives the input while not being logged in. The machine is an embedded machine, I turn it on, and it accepts swipes and saves them, using the perl script.
Take a look at udev, among other things it can: "Launch a script when a device node is created or deleted (typically when a device is attached or unplugged)"
http://www.reactivated.net/writing_udev_rules.html
The key bits are here:
while(1) {
my $text = <STDIN>;
The USB card reader is set up to direct its input to STDIN, since it's acting like a keyboard. When it finishes reading a card it sends a carriage return. The "input" then gets read by Perl and stuck into $text, then it waits for the next swipe. Once three swipes are done (the three <STDIN> lines) then it processes the information and writes it to the file. Then, since you're in a while(1) loop, it just loops back to the top of the loop and waits for more input.
You can simulate this on a different computer by running the program, then when it's waiting for input you type in some text and finish it with the Enter key. Do that three times to simulate the three swipes, and the program should process it.
The script is reading from stdin, so you need to find where/who is calling this script and see what is being piped in on stdin.
Have you checked the system's cron jobs? You might find a hint by looking at the timestamp and ownership of the /DATA/SWIPES.TXT file.

Resources