CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Help for stopping a command in script

 



krish89
Novice

Aug 6, 2012, 5:40 AM

Post #1 of 13 (2087 views)
Help for stopping a command in script Can't Post

Hi

I'm a beginner in perl programing and got stuck at one point in my code.

The script is used to run a cpp code that will acquire data from an acquisition card.

The problem is I can't stop the script which calls the acquisition program to stop at some point of time unless I press ctrl C. So I tried with the script which I have given below to do the process, but it is not stopping. the condition which I used is also given in the shorter version of my code.

Please tell me what I can do to stop that command.
Thanks in advance,
kk.


Code
#!/usr/bin/perl 
($no_of_files, $sourcename) = @ARGV;

$no=0;

my $n_secs = 50;

my $start = time;

while($no < $no_of_files)
{

#..............................#


$rec="/home/test";
printf "$rec\n";
if (time-$start >= $n_secs){
kill($rec)
}
system($rec);


#..............................#



Laurent_R
Veteran / Moderator

Aug 6, 2012, 9:02 AM

Post #2 of 13 (2074 views)
Re: [krish89] Help for stopping a command in script [In reply to] Can't Post

The kill function uses two arguments: the signal to be issues et the process.

You probably something like "kill 9, $rec";


FishMonger
Veteran / Moderator

Aug 6, 2012, 11:41 AM

Post #3 of 13 (2068 views)
Re: [krish89] Help for stopping a command in script [In reply to] Can't Post

C:\>perldoc -q timeout


Quote
Found in C:\Perl\lib\pods\perlfaq8.pod
How do I timeout a slow event?
Use the "alarm()" function, probably in conjunction with a signal
handler, as documented in "Signals" in perlipc and the section on
"Signals" in the Camel. You may instead use the more flexible
"Sys::AlarmCall" module available from CPAN.

The "alarm()" function is not implemented on all versions of Windows.
Check the documentation for your specific version of Perl.



krish89
Novice

Aug 6, 2012, 11:57 AM

Post #4 of 13 (2065 views)
Re: [Laurent_R] Help for stopping a command in script [In reply to] Can't Post

Thanks for the reply. But that is not working still, I tried it before also.


krish89
Novice

Aug 6, 2012, 11:59 AM

Post #5 of 13 (2064 views)
Re: [FishMonger] Help for stopping a command in script [In reply to] Can't Post

I did not tried with the alarm() function. lemme try. By the way, I'm using Linux system, not windows, that will not affect the alarm() right?


Laurent_R
Veteran / Moderator

Aug 6, 2012, 2:02 PM

Post #6 of 13 (2055 views)
Re: [krish89] Help for stopping a command in script [In reply to] Can't Post


In Reply To
Thanks for the reply. But that is not working still, I tried it before also.


And what do you have un $rec?


krish89
Novice

Aug 6, 2012, 10:23 PM

Post #7 of 13 (2051 views)
Re: [Laurent_R] Help for stopping a command in script [In reply to] Can't Post

$rec is a cpp program which tells the card to record the data. So basically I want to control that program from this script. That's what I'm trying.


Laurent_R
Veteran / Moderator

Aug 6, 2012, 11:16 PM

Post #8 of 13 (2049 views)
Re: [krish89] Help for stopping a command in script [In reply to] Can't Post

What I meant is: is $rec the name of the program, the PID of the process or whatever?


krish89
Novice

Aug 6, 2012, 11:57 PM

Post #9 of 13 (2047 views)
Re: [Laurent_R] Help for stopping a command in script [In reply to] Can't Post

$rec refers to the object file (g++ compiled file) of the data acquisition code in cpp.


(This post was edited by krish89 on Aug 7, 2012, 1:26 AM)


Laurent_R
Veteran / Moderator

Aug 7, 2012, 10:44 AM

Post #10 of 13 (2030 views)
Re: [krish89] Help for stopping a command in script [In reply to] Can't Post

You should send the signal to the PID of the process.

An example under Cygwin:



Code
$ sleep 2000 & 
[1] 1744

Utilisateur@your-e9b092b536 ~
$ perl -de 42

Loading DB routines from perl5db.pl version 1.32
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

main::(-e:1): 42
DB<1> kill 9 => 1744;

DB<2> q
[1]+ Killed sleep 200

Utilisateur@your-e9b092b536 ~
$ ps
PID PPID PGID WINPID TTY UID STIME COMMAND
5984 4164 5984 632 pty0 1008 Jul 11 /usr/bin/bash
4164 1 4164 4164 ? 1008 Jul 11 /usr/bin/mintty
4272 5984 4272 3740 pty0 1008 19:39:28 /usr/bin/ps

Utilisateur@your-e9b092b536 ~
$


Note that you can send signals only to processes running under the same (real or saved) UID as your real or effective UID (unless you are root).


krish89
Novice

Aug 7, 2012, 10:13 PM

Post #11 of 13 (2026 views)
Re: [FishMonger] Help for stopping a command in script [In reply to] Can't Post

Both alarm() and AlarmCall() are not working for in my case although I implemented them as they say in the document. :(


FishMonger
Veteran / Moderator

Aug 8, 2012, 5:27 AM

Post #12 of 13 (2019 views)
Re: [krish89] Help for stopping a command in script [In reply to] Can't Post

Please post your code and any errors/warnings that it may be generating.


krish89
Novice

Aug 8, 2012, 7:30 AM

Post #13 of 13 (2014 views)
Re: [FishMonger] Help for stopping a command in script [In reply to] Can't Post

Ok. I used alarm() in an awkward way, i think. Anyways, this is my code.


Code
#!/usr/bin/perl 

($no_of_files, $filename) = @ARGV;

$no=0;
my $n_secs = 50;
my $start = time;

printf "$start\n";

while($no < $no_of_files)
{

$command="date >> timestamp";
printf "$command\n";
system($command);

eval {
local $SIG{ALRM} = { die "alarm\n" };
alarm $n_secs;

$rec="/home/test";
printf "$rec\n";
system($rec);
alarm 0;
};
if ($@) {
die unless $@ eq "alarm\n"
}
$command="mv stream_test.bin $so$filename.$no";
printf "$command\n";
system($command);

$command="date >> timestamp";
printf "$command\n";
system($command);

$command="sleep 10";
printf "$command\n";
system($command);

$no = $no+1;

}

$command="mv timestamp timestamp.$filename";
printf "$command\n";
system($command);



and the error msg is

[root@krish progs]# ./run_new
1344435721
mv timestamp timestamp.
mv: cannot stat `timestamp': No such file or directory
Observing Run over

It is not at all going to the program itself. What do I do? I used alarm in a bad way?

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives