HappyTimeHarry has asked for the wisdom of the Perl Monks concerning the following question:

I have a simple but ugly script that reminds me of things I need help remembering.
#!/usr/bin/perl -w #reminder.plx use strict; use Data::Dumper; $/ = "#-"; open(INFILE, "/foo/reminder.txt") or die "Can't open reminders.txt: $! +"; my @reminders = <INFILE>; my $random = rand(@reminders); my $reminder = $reminders[$random]; $reminder =~ s/\d\. //g; chomp $reminder; my $pid; # From here on down is the part I don't like, although it doe +s do what I want it to do. defined ($pid = fork()) or die $!; if ($pid==0) { $pid and last; exec ("/usr/bin/zenity --display=:0.0 --info --title='Reminder' --text +='$reminder' &") or die "Something went wrong:$!"; } my $retval; sleep 100; $pid++; kill 15, $pid;

The part that bothers me is the bit that finds the pid of zenity and kills it. I couldn't figure out/find a better way of finding the pid. The method I used seems to find the pid just before (numerically speaking) my zenity dialog, so I used $pid++ to get the pid I wanted, but this seems dangerous for reasons you probably understand better than I do. I'd appreciate any helpful suggestions on how to improve this script, especially the part I've just described.

I'm using Ubuntu linux btw.

Replies are listed 'Best First'.
Re: Finding the PID of zenity in a perl script.
by Tanktalus (Canon) on Nov 11, 2010 at 00:05 UTC

    You have to stop using & in your exec, since that creates yet another pid.

    exec ('/usr/bin/zenity', '--display=:0.0', '--info', '--title=Reminder +', '--text=' . $reminder) or die ...
    I suspect that your $pid++ is assuming that nothing else gets launched between your fork/exec and the shell's parsing and fork/exec of zenity.

    That all said: formatting! And try Proc::Fork or something to simplify your child/parent handling.

      Perfect! Thank you very much. :)
Re: Finding the PID of zenity in a perl script.
by aquarium (Curate) on Nov 11, 2010 at 03:53 UTC
    i seem to be always taking the knife to the fork..so to speak. but it seems as overkill to fork and kill a process, just to get some output in X and time out. you could instead use a little wrapper called xmessage around xdialog, or use xdialog directly.
    the hardest line to type correctly is: stty erase ^H