linux and perl script input - linux

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.

Related

Self duplicate background script

This is a background script test.
When run it launch two processes and I don't understand why.
One stop after sleep 20. And other forgets.
#!/bin/bash
back(){
n=0
while [ 1 ]
do
echo $n
n=$(($n+1))
sleep 5
done
}
back &
sleep 20
exit
command "ps -a" in call:
PID TTY TIME CMD
8964 pts/2 00:00:00 backgroundtest
8965 pts/2 00:00:00 backgroundtest
8966 pts/2 00:00:00 sleep
8982 pts/2 00:00:00 sleep
after sleep 20:
PID TTY TIME CMD
8965 pts/2 00:00:00 backgroundtest
9268 pts/2 00:00:00 sleep
then run forever...
why?
while [ 1 ] is an infinite loop. [ 1 ] is always true.
So back & is an infinite loop, started in background (&), then execution continues with sleep 20, which does end after 20 seconds, leaving you with two processes for 20 seconds (& starts a new process in background), then the infinite one after that.

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.

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!

How can I see which CPU core a thread is running in?

In Linux, supposing a thread's pid is [pid], from the directory /proc/[pid] we can get many useful information. For example, these proc files, /proc/[pid]/status,/proc/[pid]/stat and /proc/[pid]/schedstat are all useful. But how can I get the CPU core number that a thread is running in? If a thread is in sleep state, how can I know which core it will run after it is scheduled again?
BTW, is there a way to dump the process(thread) list of running and sleeping tasks for each CPU core?
The "top" command may help towards this, it does not have CPU-grouped list of threads but rather you can see the list of threads (probably for a single process) and which CPU cores the threads are running on by
top -H -p {PROC_ID}
then pressing f to go into field selection, j to enable the CPU core column, and Enter to display.
The answer below is no longer accurate as of 2014
Tasks don't sleep in any particular core. And the scheduler won't know ahead of time which core it will run a thread on because that will depend on future usage of those cores.
To get the information you want, look in /proc/<pid>/task/<tid>/status. The third field will be an 'R' if the thread is running. The sixth from the last field will be the core the thread is currently running on, or the core it last ran on (or was migrated to) if it's not currently running.
31466 (bc) S 31348 31466 31348 34819 31466 4202496 2557 0 0 0 5006 16 0 0 20 0 1 0 10196934 121827328 1091 18446744073709551615 4194304 4271839 140737264235072 140737264232056 217976807456 0 0 0 137912326 18446744071581662243 0 0 17 3 0 0 0 0 0
Not currently running. Last ran on core 3.
31466 (bc) R 31348 31466 31348 34819 31466 4202496 2557 0 0 0 3818 12 0 0 20 0 1 0 10196934 121827328 1091 18446744073709551615 4194304 4271839 140737264235072 140737264231824 4235516 0 0 0 2 0 0 0 17 2 0 0 0 0 0
Currently running on core 2.
To see what the rest of the fields mean, have a look at the Linux kernel source -- specifically the do_task_stat function in fs/proc/array.c or Documentation/filesystems/stat.txt.
Note that all of this information may be obsolete by the time you get it. It was true at some point between when you made the open call on the file in proc and when that call returned.
You can also use ps, something like this:
ps -mo pid,tid,%cpu,psr -p `pgrep BINARY-NAME`
The threads are not necessary to bound one particular Core (if you did not pin it). Therefore to see the continuous switching of the core you can use (a modified answer of Dmitry):
watch -tdn0.5 ps -mo pid,tid,%cpu,psr -p \`pgrep BINARY-NAME\`
For example:
watch -tdn0.5 ps -mo pid,tid,%cpu,psr -p \`pgrep firefox\`
This can be done with top command. The default top command output does not show these details. To view this detail you will have to press f key while on top command interface and then press j(press Enter key after you pressed j). Now the output will show you details regarding a process and which processor its running. A sample output is shown below.
top - 04:24:03 up 96 days, 13:41, 1 user, load average: 0.11, 0.14, 0.15
Tasks: 173 total, 1 running, 172 sleeping, 0 stopped, 0 zombie
Cpu(s): 7.1%us, 0.2%sy, 0.0%ni, 88.4%id, 0.1%wa, 0.0%hi, 0.0%si, 4.2%st
Mem: 1011048k total, 950984k used, 60064k free, 9320k buffers
Swap: 524284k total, 113160k used, 411124k free, 96420k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ P COMMAND
12426 nginx 20 0 345m 47m 29m S 77.6 4.8 40:24.92 7 php-fpm
6685 mysql 20 0 3633m 34m 2932 S 4.3 3.5 63:12.91 4 mysqld
19014 root 20 0 15084 1188 856 R 1.3 0.1 0:01.20 4 top
9 root 20 0 0 0 0 S 1.0 0.0 129:42.53 1 rcu_sched
6349 memcache 20 0 355m 12m 224 S 0.3 1.2 9:34.82 6 memcached
1 root 20 0 19404 212 36 S 0.0 0.0 0:20.64 3 init
2 root 20 0 0 0 0 S 0.0 0.0 0:30.02 4 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:12.45 0 ksoftirqd/0
The P column in the output shows the processor core number where the process is currently being executed. Monitoring this for a few minutes will make you understand that a pid is switching processor cores in between. You can also verify whether your pid for which you have set affinity is running on that particular core only
top f navigation screen ( a live system example ) :
Fields Management for window 1:Def, whose current sort field is forest view
Navigate with Up/Dn, Right selects for move then <Enter> or Left commits,
'd' or <Space> toggles display, 's' sets sort. Use 'q' or <Esc> to end!
* PID = Process Id
* USER = Effective User Name
* PR = Priority
* NI = Nice Value
* VIRT = Virtual Image (KiB)
* RES = Resident Size (KiB)
* SHR = Shared Memory (KiB)
* S = Process Status
* %CPU = CPU Usage
* %MEM = Memory Usage (RES)
* TIME+ = CPU Time, hundredths
* COMMAND = Command Name/Line
PPID = Parent Process pid
UID = Effective User Id
RUID = Real User Id
RUSER = Real User Name
SUID = Saved User Id
SUSER = Saved User Name
GID = Group Id
GROUP = Group Name
PGRP = Process Group Id
TTY = Controlling Tty
TPGID = Tty Process Grp Id
SID = Session Id
nTH = Number of Threads
* P = Last Used Cpu (SMP)
TIME = CPU Time
SWAP = Swapped Size (KiB)
CODE = Code Size (KiB)
DATA = Data+Stack (KiB)
nMaj = Major Page Faults
nMin = Minor Page Faults
nDRT = Dirty Pages Count
WCHAN = Sleeping in Function
Flags = Task Flags <sched.h>
CGROUPS = Control Groups
SUPGIDS = Supp Groups IDs
SUPGRPS = Supp Groups Names
TGID = Thread Group Id
ENVIRON = Environment vars
vMj = Major Faults delta
vMn = Minor Faults delta
USED = Res+Swap Size (KiB)
nsIPC = IPC namespace Inode
nsMNT = MNT namespace Inode
nsNET = NET namespace Inode
nsPID = PID namespace Inode
nsUSER = USER namespace Inode
nsUTS = UTS namespace Inode
Accepted answer is not accurate. Here are the ways to find out which CPU is running the thread (or was the last one to run) at the moment of inquiry:
Directly read /proc/<pid>/task/<tid>/stat. Before doing so, make sure format didn't change with latest kernel. Documentation is not always up to date, but at least you can try https://www.kernel.org/doc/Documentation/filesystems/proc.txt. As of this writing, it will be the 14th value from the end.
Use ps. Either give it -F switch, or use output modifiers and add code PSR.
Use top with Last Used Cpu column (hitting f gets you to column selection)
Use htop with PROCESSOR column (hitting F2 gets you to setup screen)
To see the threads of a process :
ps -T -p PID
To see the thread run info
ps -mo pid,tid,%cpu,psr -p PID
Example :
/tmp # ps -T -p 3725
PID SPID TTY TIME CMD
3725 3725 ? 00:00:00 Apps
3725 3732 ? 00:00:10 t9xz1d920
3725 3738 ? 00:00:00 XTimer
3725 3739 ? 00:00:05 Japps
3725 4017 ? 00:00:00 QTask
3725 4024 ? 00:00:00 Kapps
3725 4025 ? 00:00:17 PTimer
3725 4026 ? 00:01:17 PTask
3725 4027 ? 00:00:00 RTask
3725 4028 ? 00:00:00 Recv
3725 4029 ? 00:00:00 QTimer
3725 4033 ? 00:00:01 STask
3725 4034 ? 00:00:02 XTask
3725 4035 ? 00:00:01 QTimer
3725 4036 ? 00:00:00 RTimer
3725 4145 ? 00:00:00 t9xz1d920
3725 4147 ? 00:00:02 t9xz1d920
3725 4148 ? 00:00:00 t9xz1d920
3725 4149 ? 00:00:00 t9xz1d920
3725 4150 ? 00:00:00 t9xz1d920
3725 4865 ? 00:00:02 STimer
/tmp #
/tmp #
/tmp # ps -mo pid,tid,%cpu,psr -p 3725
PID TID %CPU PSR
3725 - 1.1 -
- 3725 0.0 2
- 3732 0.1 0
- 3738 0.0 0
- 3739 0.0 0
- 4017 0.0 6
- 4024 0.0 3
- 4025 0.1 0
- 4026 0.7 0
- 4027 0.0 3
- 4028 0.0 7
- 4029 0.0 0
- 4033 0.0 4
- 4034 0.0 1
- 4035 0.0 0
- 4036 0.0 2
- 4145 0.0 2
- 4147 0.0 0
- 4148 0.0 5
- 4149 0.0 2
- 4150 0.0 7
- 4865 0.0 0
/tmp #

Why shell_exec executing more than 1 process?

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.

Resources