Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

listening to a process in Perl.

Status
Not open for further replies.

bcdixit

Technical User
Nov 11, 2005
64
US
Hello,
I have a perl script in which i need run some other perl script.
The way I am doing this is through the system command i.e. something like this.

while(<read script names from a file>)
{
system("$script");
}

now, what I want to do is on the screen (STDOUT), I want to display something like

running my_script1.pl..........done!
running my_script2.pl.....failed!

I believe that when the system() function is called in a Perl script, the control is handed over to the script that is called within the system function and control is handed over to the parent only when the script is completed

what i need is when the script my_script1.pl is executed, then my script should print those dots (....) on the terminal screen every 2 seconds until i get a status from pass/fail status from my_script1.pl (or my_script2.pl)


can anybody help me with this.?

thanks
bcd
 
Hi, i once had a similar problem. maybe this will help:
Another option could be to fork the system calls. i think you could also jerry-rig a workaround by appending "&" to the command, which starts a new process and returns control immediately, at least on linux-like environments.
cheers!
michael :)
 
btw...on windows running active perl you may want to check out Win32::process (if i remember the name correctly)
 
sorry , I did not mention this earlier but this is on unix.
 
The following solution is inspired by a thread solved by Kirsle: concurrent processes. from 1 May 07.

Code:
[gray][i]# name: scratch.pl[/i][/gray]

[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]Time::HiRes[/green] [red]qw([/red][purple]sleep gettimeofday[/purple][red])[/red][red];[/red]
[black][b]use[/b][/black] [green]threads[/green][red];[/red]
[black][b]use[/b][/black] [green]threads::shared[/green][red];[/red]

[gray][i]# Constants[/i][/gray]
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$timer_delta[/blue] = [fuchsia]1[/fuchsia][red];[/red] [gray][i]# in Seconds[/i][/gray]
[black][b]my[/b][/black] [blue]@commands[/blue] = [red]([/red]
	[red]'[/red][purple]perl pause.pl 5[/purple][red]'[/red],
	[red]'[/red][purple]perl pause.pl 3[/purple][red]'[/red],
	[red]'[/red][purple]perl pause.pl 6[/purple][red]'[/red],
[red])[/red][red];[/red]

[blue]$|[/blue] = [fuchsia]1[/fuchsia][red];[/red] [gray][i]# Unbuffer STDOUT[/i][/gray]

[olive][b]foreach[/b][/olive] [black][b]my[/b][/black] [blue]$command[/blue] [red]([/red][blue]@commands[/blue][red])[/red] [red]{[/red]
	[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]Running '[blue]$command[/blue]' [/purple][red]"[/red][red];[/red]
	
	[black][b]my[/b][/black] [blue]$running[/blue] : shared = [fuchsia]1[/fuchsia][red];[/red]
	[black][b]my[/b][/black] [blue]$timer_thr[/blue] = threads->[maroon]new[/maroon] [red]([/red][url=http://perldoc.perl.org/functions/sub.html][black][b]sub[/b][/black][/url] [red]{[/red]
		[black][b]my[/b][/black] [blue]$next[/blue] = gettimeofday[red];[/red]
		[olive][b]while[/b][/olive] [red]([/red][blue]$running[/blue][red])[/red] [red]{[/red]
			[blue]$next[/blue] += [blue]$timer_delta[/blue][red];[/red]
			[black][b]print[/b][/black] [red]"[/red][purple].[/purple][red]"[/red][red];[/red]
			[olive][b]while[/b][/olive] [red]([/red][blue]$running[/blue] && [blue]$next[/blue] > gettimeofday[red])[/red] [red]{[/red]
				[url=http://perldoc.perl.org/functions/sleep.html][black][b]sleep[/b][/black][/url][red]([/red][fuchsia].05[/fuchsia][red])[/red][red];[/red]
			[red]}[/red]
		[red]}[/red]
	[red]}[/red][red])[/red][red];[/red]

	[black][b]my[/b][/black] [blue]$process_thr[/blue] = threads->[maroon]new[/maroon] [red]([/red][black][b]sub[/b][/black] [red]{[/red]
		[url=http://perldoc.perl.org/functions/system.html][black][b]system[/b][/black][/url] [blue]$command[/blue][red];[/red]
		[blue]$running[/blue] = [fuchsia]0[/fuchsia][red];[/red]
	[red]}[/red][red])[/red][red];[/red]

	[gray][i]# rejoin the threads when they finish[/i][/gray]
	[blue]$process_thr[/blue]->[maroon]join[/maroon][red];[/red]
	[blue]$timer_thr[/blue]->[maroon]join[/maroon][red];[/red]

	[black][b]print[/b][/black] [red]"[/red][purple] done![purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]threads - Perl extension allowing use of interpreter based threads from perl[/li]
[li]threads::shared - Perl extension for sharing data structures between threads[/li]
[/ul]
Core (perl 5.8.8) Modules used :
[ul]
[li]Time::HiRes - High resolution alarm, sleep, gettimeofday, interval timers[/li]
[/ul]
[/tt]

Code:
[gray][i]# name: pause.pl[/i][/gray]

[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]Time::HiRes[/green] [red]qw([/red][purple]sleep[/purple][red])[/red][red];[/red]

[url=http://perldoc.perl.org/functions/sleep.html][black][b]sleep[/b][/black][/url][red]([/red][url=http://perldoc.perl.org/functions/shift.html][black][b]shift[/b][/black][/url][red])[/red][red];[/red]

The output is the following:

Code:
>perl scratch.pl
Running 'perl pause.pl 5' ...... done!
Running 'perl pause.pl 3' .... done!
Running 'perl pause.pl 6' ....... done!

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top