perl: sleeping during a command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl: sleeping during a command
# 1  
Old 03-03-2005
perl: sleeping during a command

hello everyone,

i am attempting to run the sleep function (i've also tried select) during the execution of a command to mimic a status. for example:

# this is a terminal screen
# here the process is executed
# below this a status is displayed while the command executes like so:
..................................................done


however, when i use sleep or select, they delay the command. how can i separate the two?

i have done this in shell scripts before using ps, grep, and awk; however, i'd like to pull this off in perl if possible...

here is what i have:

Code:
open CMD "$cmd |";
while(<CMD>) {
	print '.';
	# future code to log the output of CMD
	sleep 1;
	# select(undef, undef, undef, 0.5);
}


Last edited by effigy; 03-03-2005 at 10:49 PM..
# 2  
Old 03-04-2005
Bug

i came up with this, which seems to work well:

Code:
        my $pid = fork;
        if($pid == 0) { # child
                open CMD, "$cmd 2> /dev/null |";
                while(<CMD>) { print LOG; }
                close CMD;
                print "\nprocess complete.";
                exit;
        } else { # parent
                while((my $running=`ps -p $pid`) !~ /defunct/) { print '.'; select(undef, undef, undef, 0.5); }
                waitpid($pid,0);
        }

# 3  
Old 03-04-2005
now my logging is whacky... hmmm...
# 4  
Old 03-04-2005
fixed.

Code:
use IO::Handle;
STDOUT->autoflush(1);
LOG->autoflush(1);

or (not tested, but as i understand it):
Code:
select STDOUT; $| = 1;
select LOG; $| = 1;

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Signal trapped during read resumes sleeping

Greetings. This is my first post in this forum; I hope y'all find it useful. One caveat: "Concise" is my middle name. NOT! :D I am almost done with a shell script that runs as a daemon. It monitors a message log that is frequently written to by a database server but it it works my client will... (2 Replies)
Discussion started by: jakesalomon
2 Replies

2. UNIX for Dummies Questions & Answers

GNU Linux sleeping processes in top command

hi all sleeping processes in the following output , are they doing anything , but consuming lot of sources, should I need to kill them , how to know , , what they are doing and the output says out of 260 processes only 9 are running , and 251 are sleeping , what does the sleeping means, can... (8 Replies)
Discussion started by: sidharthmellam
8 Replies

3. UNIX for Dummies Questions & Answers

Sleep command not sleeping for specified time.

Hi, I have ascript with a recursive funtion below. I have mentioned to sleep for 60minutes but it doesnt doing so. Its keep on running until if /elif conditions satiesfies. Can you pls help what is wrong here. funcstatus () { if then echo "`date` - Current status... (2 Replies)
Discussion started by: gaddamja
2 Replies

4. Shell Programming and Scripting

perl/unix: script in command line works but not in perl

so in unix this command works works and shows me a list of directories find . -name \*.xls -exec dirname {} \; | sort -u | > list.txt but when i try running a perl script to run this command my $query = 'find . -name \*.xls -exec dirname {} \; | sort -u | > list.txt';... (2 Replies)
Discussion started by: kpddong
2 Replies

5. Shell Programming and Scripting

Sleep process not sleeping!

I had a script executing every hour to kill a process. I used loop rather than cron to execute it periodically. But now when I am trying to kill that sleep process of 1 hour its not getting killed. it is taking a new PID everytime I kill. To disable the script commenting is the only option... (1 Reply)
Discussion started by: nixhead
1 Replies

6. Shell Programming and Scripting

Help on sleeping the script

Hi all, How can i specify to sleep for 24 hours in a script Thanks Firestar (5 Replies)
Discussion started by: firestar
5 Replies

7. Shell Programming and Scripting

[Perl] Accessing array elements within a sed command in Perl script

I am trying to use a script to replace the header of each file, whose filename are stored within the array $test, using the sed command within a Perl script as follows: $count = 0; while ( $count < $#test ) { `sed -e 's/BIOGRF 321/BIOGRF 332/g' ${test} > 0`; `cat 0 >... (2 Replies)
Discussion started by: userix
2 Replies

8. Shell Programming and Scripting

Unable to kill sleeping process

Hi, I'm trying to delete a sleeping process (parent ID is not 1) with "kill -9" command by the owner of the process (infodba) but it doesn't get killed. Is there any way of killing this process without killing the parent process or rebooting? (I'm using HP Unix B.11.11) $ ps -eflx | grep... (0 Replies)
Discussion started by: stevefox
0 Replies

9. Shell Programming and Scripting

How to wakeup sleeping processes

Hi, Could someone please tell me how to wakeup sleeping processes? (i.e. change the process status from "S" to "R" when viewing in ps command). I ran a few programs in the background by "&" which went into "sleep" mode and I want them to run. Any help will be greatly appreciated. Steve (11 Replies)
Discussion started by: stevefox
11 Replies
Login or Register to Ask a Question