Thread: Calling lockf from the shell line works fine, but in the script, it fails

  1. #1
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329

    Calling lockf from the shell line works fine, but in the script, it fails

    Everything seems to work fine when I do the following...

    Code:
    % lockf -t 30 /usr/party/log/party.log sleep 30
    Now when I add it to a perl script like in the following

    Code:
    #!/usr/bin/perl
    use warnings;
    
    my $value;
    while (True)
    {
        if(`w | grep cd | grep party`) {
            `lockf -t 30 /usr/party/log/party.log sleep 30`;
            sleep(30);
        }
        else {
            print "not logged in\n";
        }
        sleep(1);
    
    }
    It doesn't work even though lockf shows up on my ps table.

    cdalten 50123 0.0 0.2 3764 2580 pl IN 12:51PM 0:00.32 -zsh (zsh)
    cdalten 52997 0.0 0.2 2796 2276 pl SN+ 1:30PM 0:00.05 /usr/bin/perl ./scan.pl (perl5.8.8)
    cdalten 53078 0.0 0.1 1256 596 pl SN+ 1:31PM 0:00.00 lockf -t 30 /usr/party/log/party.log sleep 30
    cdalten 53079 0.0 0.0 1256 432 pl SN+ 1:31PM 0:00.00 sleep 30
    I don't get why it fails when I run it in a perl script.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    It seems silly to me to use a perl script just to issue shell commands; why don't you put them in a shell script?

    To lock a file in perl use flock().
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Quote Originally Posted by MK27 View Post
    It seems silly to me to use a perl script just to issue shell commands; why don't you put them in a shell script?

    To lock a file in perl use flock().
    I'd still get the same results and hence it wouldn't solve my current problem.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Overworked_PhD View Post
    I'd still get the same results and hence it wouldn't solve my current problem.
    are you saying that this
    Code:
    #!/bin/bash
    
    lockf -t 30 /usr/party/log/party.log sleep 30
    will not execute EXACTLY as if you had typed it into the command-line? Because I don't believe you!

    I do not have a "lockf" command on my system, so I can't help you with the syntax*, but shell scripts are executed by the shell one line at a time. If it works on the command line, it will work in a shell script. Here:
    Code:
    !/bin/bash
    
    if grep "bash" | grep "cdalten" > /dev/null
        then lockf -t 30 /usr/party/log/party.log sleep 30
        else echo "not logged in"
    fi
    The purpose of "> /dev/null" is to spare you the grep output on screen.

    *eg, I'm assuming "sleep 30" at the end is a parameter and not a typo for "&& sleep 30"
    Last edited by MK27; 04-12-2009 at 01:41 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Your script only works after I press ctrl-c. I'm assuming the process is doing some kind of forking that I'm not aware of.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Overworked_PhD View Post
    Your script only works after I press ctrl-c. I'm assuming the process is doing some kind of forking that I'm not aware of.
    Code:
    cat textfile | thatscript.sh
    or
    thatscript.sh < textfile
    Sorry, it was intended to process input, I thought that was clear.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. This works fine with IP 127.0.0.1 but fails with my REAL IP.
    By azjherben in forum Networking/Device Communication
    Replies: 15
    Last Post: 05-19-2009, 10:28 PM
  2. shell script basic question
    By PedroTuga in forum Linux Programming
    Replies: 1
    Last Post: 09-09-2006, 03:24 AM
  3. It works just fine but
    By jcmhex in forum C++ Programming
    Replies: 3
    Last Post: 04-24-2005, 06:53 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 06:04 AM